Having problems with your account or logging in?
A lot of changes are happening in the community right now. Some may affect you. READ MORE HERE
Highlighted
HenryH Absent Member.
Absent Member.
1551 views

.NET script: Check if object exists with If statement?

Jump to solution

I'm testing an application where I am supposed to input an organization into a combo box.  Once I click to another field, the system will then apply this logic: If the organization I entered does not exist in the application's database, a popup window will ask if I want to save it.  If the organization was already present in the DB, no popup window displays.

My .NET script behaves correctly when I enter a new organization and click away; the popup opens and the script clicks 'Yes' in that window.  But whenever the organization is already in the system, I get the playback error "XPath expression '@caption='New organization'' is invalid."

Can anybody determine what is wrong with my syntax for the If statement, or suggest a better alternative?  I thought the script would begin at the next line after "End If" if the object doesn't exist. 

Here are the relevant areas of the script:

 

Imports SilkTest.Ntf.Swt
Public Module Main
    Dim _desktop As Desktop = Agent.Desktop

    Public Sub Main()

    With _desktop.Shell("@caption='Add Record'")

        .ComboBox("[1]").TypeKeys("<OrganizationName>")

        .TextField("[2]").Click

        If .Shell("@caption='New organization'").Exists Then
            With .Shell("@caption='New organization'")
                .SetActive()
                .PushButton("@caption='Yes'").Select()
            End With
        End If

       .PushToolItem("@caption='Save and Close'").Select()

    End With

    End Sub
End Module

 

0 Likes
1 Solution

Accepted Solutions
AndySchott Absent Member.
Absent Member.

RE: .NET script: Check if object exists with If statement?

Jump to solution

You might try searching for the popup window by calling Exists() off of its parent window:

    If _desktop.Exists("//Shell[@caption='Add Record']") Then
        ' This code will execute when the popup window exists
    Else
        ' This code will execute when the popup window does not exist
    End If

Andy

0 Likes
8 Replies
Micro Focus Expert
Micro Focus Expert

RE: .NET script: Check if object exists with If statement?

Jump to solution
Hi Henry

It seems that you are missing the square brackets in your locators. Can you try the below:

If .Shell("[@caption='New organization']").Exists Then
With .Shell("[@caption='New organization']")
.SetActive()
.PushButton("[@caption='Yes']").Select()
End With
End If

.PushToolItem("[@caption='Save and Close']").Select()
0 Likes
HenryH Absent Member.
Absent Member.

RE: .NET script: Check if object exists with If statement?

Jump to solution
Thanks for the reply Colin. Adding the brackets had no effect. The script was still able to recognize the object when it opened, but in the cases where the object did not open, the script did not proceed to the line after the If/End If section.
0 Likes
Micro Focus Expert
Micro Focus Expert

RE: .NET script: Check if object exists with If statement?

Jump to solution
Is the .Shell("@caption='New organization'") a direct child of _desktop.Shell("@caption='Add Record'")?

What is full xpath locator for the .Shell("@caption='New organization'")?
0 Likes
HenryH Absent Member.
Absent Member.

RE: .NET script: Check if object exists with If statement?

Jump to solution
It is a direct child, the full xpath is: /Shell[@caption='Add Record']//Shell[@caption='New organization']
0 Likes
Micro Focus Expert
Micro Focus Expert

RE: .NET script: Check if object exists with If statement?

Jump to solution
Can you try using the full locator in the controls e.g.

With _desktop.Shell("/Shell[@caption='Add Record']")

.ComboBox("[1]").TypeKeys("<OrganizationName>")

.TextField("[2]").Click

If .Shell("//Shell[@caption='Add Record']").Exists Then
With .Shell("/Shell[@caption='Add Record']")
.SetActive()
.PushButton("//PushButton[@caption='Yes']").Select()
End With
End If

.PushToolItem("[//PushToolItem[@caption='Save and Close']").Select()

End With
0 Likes
HenryH Absent Member.
Absent Member.

RE: .NET script: Check if object exists with If statement?

Jump to solution
The locators aren't the issue, as the script is able to find the xpath when the condition is true (if the window exists, it's able to click the button in that window). The problem is that the script is evaluating the condition when it is false (the window does not exist, but the script is still trying to locate the non-existent window instead of moving past the If statement).
0 Likes
AndySchott Absent Member.
Absent Member.

RE: .NET script: Check if object exists with If statement?

Jump to solution

You might try searching for the popup window by calling Exists() off of its parent window:

    If _desktop.Exists("//Shell[@caption='Add Record']") Then
        ' This code will execute when the popup window exists
    Else
        ' This code will execute when the popup window does not exist
    End If

Andy

0 Likes
HenryH Absent Member.
Absent Member.

RE: .NET script: Check if object exists with If statement?

Jump to solution
Thank you very much Andy. The format you suggested helped, though I had to search the child window instead. Here's what worked, both the If statement and If/Else statement:

If _desktop.Exists("//Shell[@caption='New organization']") Then
.PushButton("[@caption='Yes']").Select()
Else
.PushToolItem("[//PushToolItem[@caption='Save and Close']").Select()
End If



If _desktop.Exists("//Shell[@caption='New organization']") Then
.PushButton("[@caption='Yes']").Select()
End If

.PushToolItem("[//PushToolItem[@caption='Save and Close']").Select()
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.