QTP – What is XPath? XML and XMEN

Automation Guild Online Conference Top ad

“Mutation: it is the key to our evolution. It has enabled us to evolve from a single-celled organism into the dominant species on the planet. This process is slow, and normally taking thousands and thousands of years. But every few hundred millennia, evolution leaps forward. Prof.” – Charles Francis Xavier

QTP XMEN ARISE — What is XPATH?

XPath allows you to find different parts of XML documents. This is powerful, because it enables you to write expressions that return specific info in an XML document. Using Vbscript in QTP will unleash this new power for you.

[smartads]

Xpath and Trees

The classic method for visualizing an XML document is to imagine an upside down tree. The trunk is considers the ROOT, and the branches are nodes. Location paths are a way to select the node(s) that you’re interested in. To try these examples download the xmen.xml

1. The Simplest Location Path

To return all the text values found in the XML’s nodes you can use a Root location path, which is a forward slash (/). For example, to return all the values in our XMEN XML we would use:

strXPath = "/ "
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.Async="false"
xmlDoc.Load("C:\xmen.xml")

Set xmen = xmlDoc.documentElement.selectNodes(strXPath)
  For Each xman In xmen  
   print xman.text & vbNewLine
  Next

 Result:

Should return all text values in the XML file

2. The Second Easiest Path – Child Elements

The Child element location allows you to obtain a single element name. It will select all child elements that match the name specified. So, to return the first person’s ‘name’ element value in our XMEN doc we would use:

To return the first XMEN name value match:

strXPath = "/x-men/member/name"
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.Async="false"
xmlDoc.Load("C:\xmen.xml")

Set  NodeList = xmlDoc.documentElement.selectSingleNode(strXPath)
msgbox NodeList.text

Result: Should return the name ‘Professor X'

3. To return the all the XMEN name values

strXPath = "/x-men/member/name"

Set xmlDoc = CreateObject("Microsoft.XMLDOM")  
xmlDoc.Async="false"
xmlDoc.Load("C:\xmen.xml")
 
'Get all x-men names
Set xmen = xmlDoc.documentElement.selectNodes(strXPath)
  For Each xman In xmen  
    msgbox xman.text
  Next

Result:

Should return all the XMEN names

4. Elements Attributes

You can also return a particular attribute of an element using the @ sign.

To return all the ‘power’ elements text values that have ability type equal to two:

strXPath = "/x-men/member/ability[@type='2']"
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.Async="false"
xmlDoc.Load("C:\xmen.xml")

Set xmen = xmlDoc.documentElement.selectNodes(strXPath)
  For Each xman In xmen  
    msgbox xman.text
  Next

Result:

Should return all the power value “Superhuman senses.”

5. To return the attribute text value of an element:

strXPath = "/x-men/member/ability[@type='2']"
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.Async="false"
xmlDoc.Load("C:\xmen.xml")

Set xmen = xmlDoc.documentElement.selectNodes(strXPath)
  For Each xman In xmen  
    msgbox xman.text
  Next

Result:

Should return the type attribute value of 1

Mutant

This should be enough to get you started.  Congratulations!! You are now an official XMEN XPath QTP Mutant.

Fellow Bibliomaniacs
For everything you ever wanted to know about XPath check out:

5 comments
Nand - February 1, 2012

Let me explain what my problem is

1 Page A has 4 links aa,bb,cc,dd
1.1 Click aa link to go to popup that has inboxes ggg,hhh

2 Page B has 4 links aa,bb,cc,dd
2.1 Click aa link to go to popup that has inboxes ggg,hhh

NOW, ggg,hhh inboxes have same property values whether i go to aa of page A or aa of page B. When i run the test, test stops at ggg of page A and doesn’t enter value and throws error, “Several objects have same property values.Use unique value to identify the object”

How to make tool recognize objects uniquely?
Should RTO values be changed and made unique?
How do you manage with a website that has several unique fields?

Reply
    Joe Colantonio - February 11, 2012

    Nand » First i usually would try to use an index value EX WinObject(“text:=ggg”,”index:=1″) . Or try to find a combination of properties that would make the objects id unique. If there is a label next to the field you could also try QTP’s visual relation identifier. Are you using an OR or descriptive programming?

    Reply
Rohit - July 30, 2013

i m trying to use the 2nd method you have given.
2. The Second Easiest Path – Child Elements

While using i m getting an error as Object required on the line number where i m trying to print the value. please help how to resolve this issue???

Reply
Harish - August 14, 2014

Hi joe,
I have a requirement to search for the tag value and get the value.
here it is ..

test1
11,2,13

test2
12,72,13

Admin
20,72,12,13

test3
13

How can search for test3 and get the value of memberof? Please guide me here with your suggestions.

Reply
Click here to add a comment

Leave a comment: