Wait a second  – what about Xpath?

Missed Part 1 and 2? Catch up here and here!

You are right – Xpath is THE query language for XML content – so it is built for exactly this task. Our solution comes with two different means to use Xpath.

Evaluate XPath

Let’s take a look at Evaluate XPath first. You find this module in the standard subset:

XML1 (1)

It takes three parameters:

XML2 (1)

The first parameter is the XML resource which holds the XML data to be evaluated. The second the Xpath expression – if you want to get familiar with Xpath take a look on online tutorials like http://www.w3schools.com/xpath/.

The third is the result of the evaluation – at the moment we support only textual result and not XML subnodes.

Xpath is powerful but not so easy to understand. Let’s therefore start with a simple task by counting the number of shelves.

rsz_xml4_1

 

Remember the XML representing the library content?

XML5 (1)

 

I can navigate with Xpath through the XML structure similar to file path. E.g. the Xpath expression \library\shelf returns all XML nodes with the name shelf under the top node library.

I can use this expression to, for example, count the number of shelves in our library. This can be achieved with the count function:

XML6 (1)

If you don’t care about the XML structure you can also use the following expression:

XML7 (1)

The // expression can be used to count all books in the library- since I am not interested where the books are located, only in the number of books.

XML8 (1)

 

If I want to buffer the ISBN of the first book in the shelf science I can use the following expression:

XML9 (1)

 

The expression //shelf[name=’science’] returns all shelves with this name. In our case there is only one. By adding /book[1]/ISBN I  can buffer the ISBN of this book.

This example also shows the limitations with Xpath. Complex scenarios get rather hard to understand and hard to maintain.

Imagine that you want to check the name of the Author of the book “Godel, Escher, Bach”

One way of doing it is:

XML10 (1)

 

First we search any book which has describedby/value starting with the name we are searching for:

//book[describedby/value[starts-with(.,’Godel, Escher, Bach’)]]

Afterwards we select the describedby node with the name ‘Author’ to check the value

//book[describedby/value[starts-with(.,’Godel, Escher, Bach’)]]/describedby[./name=’Author’]/value

Believe me it took me quite a while to get this working and it is really hard to read and to maintain in the future. I therefore recommend to use the Evaluate Xpath only when it is clear for you and for others what you want to achieve in your test step.

Using Xpath in Modules

A second possibility of using Xpath is on a module level. You can restrict the XML handled by a specific module node by using an Xpath expression. However, this requires some manual work.

Let us again do the following example: I want to select a shelf with certain book in it.

I first create manually a new Configuration Parameter by selecting Create->Configuration Param on the properties Tab of the node shelf:

XML11 (1)

 

The name of this new parameter is Transition with the value XPathToXmlElement.

XML12 (1)

 

Afterward I create a Transition Param:

XML13 (1)

 

and name it Xpath:

XML14 (1)

 

By setting it the following Xpath expression:

XML16 (1)

 

I can later select a shelf in which a certain book is found. The name of the book will be retrieved from the buffer booktitle.

 

Note: In this case I use the full name – of course I could also use the starts-with function.

After finding the right shelf, I now want to find the book in the shelf. I use the same two additional parameters on the node book of the module:

XML18 (1)

 

This allows us check immediately the ISBN of the book.

XML19 (1)

 

I can also use the Xpath expression to create a simpler version of the module with the describedby nodes.

I first drag and drop node value to the book node:

XML20 (1)

 

Afterwards I delete the describedby subnode – my module looks like that:

XML21 (1)

 

I rename the name of the node to author and create the Xpath entries as above:

XML22

 

This way I select the subnode value of the describedby node with the name Author. The test case looks like that:

XML23 (1)

 

Both XPath options allows me to handle XML data but require a lot of knowledge.

Thanks for following along the How To Verify Complex XML Structures series, we’d love to hear your comments and questions!

Part 1: The Library
Part 2: The Library Grows

 

Want to learn more? Download our free Fact Sheet on API testing with Tosca Testsuite!

 

Download the Fact Sheet

 

 

 

Tricentis Logo White Version
X