QTP/UFT Identifying objects using Xpath the easy way!

Hi. Thursdays can be rather boring. But not today! Today, I have an awesome tutorial for you that will teach you how to use xpath with QTP/UFT in a few simple steps!

1. Open up firefox, click on the menu tab in the upper right hand corner of the screen, and click "add ons"

2. Search for xpath checker, and install it.

3. Restart your firefox and you will be ready to go. Now we have a tool to extract the xpath for any object on a web page.

We will be using the below sign up form to test out your xpath skills!

Registration Options
Enjoy access to our amazing QTP/UFT tutorials
Create Profile
password strength indicator
Complete Registration

4. Right click on the email input field and click "view xpath". Copy the value that you receive.

5. You will be using the standard way of setting a webedit value.

Browser("hwnd:=" & hwindow).Page("title:="& page_title).WebEdit("xpath:=").Set

Copy and paste your xpath value below into the code above. It should look like this:
Browser("hwnd:=" & hwindow).Page("title:="& page_title).WebEdit("xpath:=id('s2member-pro-paypal-registration-email')").Set "youremail@mail.com"

Now, let's adjust the syntax of the xpath so QTP/UFT can understand it.

  1. Add //* before the id tag. This lets QTP/UFT know that the path before this id tag doesn't matter, just find this id tag and anything after it.
  2. Add a brackets [ ] around the id and the id value.
  3. Delete the ( )
  4. Add @ before the id. When identifying a tag in xpath, add a @ before it.
  5. Use the = sign after the id to let QTP/UFT know what the id tag should equal to. Your code should look like the code below.

Browser("hwnd:=" & hwindow).Page("title:="& page_title).WebEdit("xpath:=//*[@id='s2member-pro-paypal-registration-email']").Set "youremail@mail.com"

You are all set to use xpath to identify the email edit field. Repeat the steps for all of the objects in the form.
Browser("hwnd:=" & hwindow).Page("title:="& page_title).WebEdit("xpath:=//*[@id='s2member-pro-paypal-registration-email']").Set "youremail@mail.com"
Browser("hwnd:=" & hwindow).Page("title:="& page_title).WebEdit("xpath:=//*[@id='s2member-pro-paypal-registration-username']").Set "YourName"
Browser("hwnd:=" & hwindow).Page("title:="& page_title).WebEdit("xpath:=//*[@id='s2member-pro-paypal-registration-password1']").Set "password"
Browser("hwnd:=" & hwindow).Page("title:="& page_title).WebEdit("xpath:=//*[@id='s2member-pro-paypal-registration-password2']").Set "password"
Browser("hwnd:=" & hwindow).Page("title:="& page_title).WebButton("xpath:=//*[@id='s2member-pro-paypal-registration-submit']").Click

Now you just need to identify the browser and page. I've done this for you in tutorial 2.

Tutorial 2

Copy the functions from tutorial 2 and paste them in your code. Call the identify browser and page function to identify your browser and page. Then paste your xpath code afterwards. Your final code should look like:

fnIdentifyBrowserPage
Browser("hwnd:=" & hwindow).Page("title:="& page_title).WebEdit("xpath:=//*[@id='s2member-pro-paypal-registration-email']").Set "youremail@mail.com"
Browser("hwnd:=" & hwindow).Page("title:="& page_title).WebEdit("xpath:=//*[@id='s2member-pro-paypal-registration-username']").Set "YourName"
Browser("hwnd:=" & hwindow).Page("title:="& page_title).WebEdit("xpath:=//*[@id='s2member-pro-paypal-registration-password1']").Set "password"
Browser("hwnd:=" & hwindow).Page("title:="& page_title).WebEdit("xpath:=//*[@id='s2member-pro-paypal-registration-password2']").Set "password"
Browser("hwnd:=" & hwindow).Page("title:="& page_title).WebButton("xpath:=//*[@id='s2member-pro-paypal-registration-submit']").Click

Function fnReplaceRegExp(StrLabel)
    StrLabel = replace(StrLabel, "|", "\|")
    StrLabel = replace(StrLabel, "(", "\(")
    StrLabel = replace(StrLabel, ")", "\)")
    fnReplaceRegExp = StrLabel
End Function

Function fnIdentifyBrowserPage()
    hwindow = Browser("creationTime:=0").GetROProperty("hwnd")
    page_title = Browser("hwnd:=" & hwindow).Page("title:=.*").GetROproperty("title")
    page_title = fnReplaceRegExp(page_title)
End Function

Congratulations! You did it! Easy right? This is the easiest and quickest way I found to using xpath with QTP/UFT. Not only did you learn xpath, but you signed up for our site at the same time!!!! Leave your comments and questions below.

 

 

 

 

 

Pin It on Pinterest

Clef two-factor authentication