I was recently asked to help out a customer with verifying the response of a web service call. While I am not allowed to share the exact sample, let me show you a similar problem and how you can solve it.
Let’s assume we want to automate a web service which returns the content of a library. The library has shelves in which books of certain category are available.
Our library is brand new so we only have only one book at the moment – the famous book of Charles Darwin:
It is a modern library so every book (ok, there is only one) is cataloged in a central database and a web service allows you to query.
I get the following response from my web service:
My test scenario requires that I verify the author of this book. The name of the author is returned in one of the “describedby” nodes – at the moment there are 4 of them
However using the following version of my test case:
will fail because when there are more than one describedby nodes we will get the following result:
This means Tosca doesn’t know which of the four describedby nodes it has to select.
One solution of this problem is to check the first describedby node
However it assumes that order of the describedby nodes is always the same, which can’t be guaranteed for the future. Below, the result still returns the same information but in a different order:
Our original test will fail.
I want a better solution for this problem. I want to select the describedby node which contains the name of the author.
Let me introduce you into the use of constraints. As the name suggests, a constraint restricts a selection by some criteria.
The following version of the test case finds the describedby node by constraining all nodes to find the one which has the name Author.
After that I can immediately the value Charles Darwin.
Let’s check the XML again and let’s see what our test case is supposed to do:
This solves our problem and it is also stable in case the web service call returns the result in a different sort order.
Stayed tuned for How to Verify Complex XML Structures: Part 2 and Part 3!
Part 2: The Library Grows
Part 3: What About Xpath?