Wait a second – what about Xpath?
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.
Let’s take a look at Evaluate XPath first. You find this module in the standard subset:
It takes three parameters:
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.
Remember the XML representing the library content?
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:
If you don’t care about the XML structure you can also use the following expression:
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.
If I want to buffer the ISBN of the first book in the shelf science I can use the following expression:
The expression //shelf[name=’science’] returns all shelves with this name. In our case there is only one. By adding /book/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:
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:
The name of this new parameter is Transition with the value XPathToXmlElement.
Afterward I create a Transition Param:
and name it Xpath:
By setting it the following Xpath expression:
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:
This allows us check immediately the ISBN of the book.
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:
Afterwards I delete the describedby subnode – my module looks like that:
I rename the name of the node to author and create the Xpath entries as above:
This way I select the subnode value of the describedby node with the name Author. The test case looks like that:
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!
Want to learn more? Download our free Fact Sheet on API testing with Tosca Testsuite!
Download the Fact Sheet
Stay connected– subscribe