Now the REST service with xml responses can be tested with CukeRestSalad.
This plugin assumes json response by default. But if the http response contains a header – “Content-Type: application/xml”, then it will try to parse the response as xml. If the response is xml, then the xpath in the feature files should comply with the java xpath syntax. These are pretty good tutorials to start off – “Java XPath Tutorial” and “How XPath Works” For the sake of demonstration we will take and example xml and define step definition for the same:
<response>
<store>
<book>
<category>reference</category>
<author>Nigel Rees</author>
<title>Sayings of the Century</title>
<price>8.95</price>
</book>
<book>
<category>fiction</category>
<author>Evelyn Waugh</author>
<title>Sword of Honour</title>
<price>12.99</price>
</book>
<book>
<category>fiction</category>
<author>Herman Melville</author>
<title>Moby Dick</title>
<isbn>0-553-21311-3</isbn>
<price>8.99</price>
</book>
<book>
<category>fiction</category>
<author>J. R. R. Tolkien</author>
<title>The Lord of the Rings</title>
<isbn>0-395-19395-8</isbn>
<price>22.99</price>
</book>
<bicycle>
<color>red</color>
<price>19.95</price>
</bicycle>
<unicorn></unicorn>
</store>
<expensive>10</expensive>
</response>
The step definitions for the above xml would be:
And The response should contain "//store//color" with value "red"
And The response should contain "//expensive"
And The response should contain "//author" with values:
| Nigel Rees | Evelyn Waugh | Herman Melville | J. R. R. Tolkien |
And The "//book" array has element with below attributes:
| category | author | title | price |
| reference | Nigel Rees | Sayings of the Century | 8.95 |
| fiction | Evelyn Waugh | Sword of Honour | 12.99 |
| fiction | Herman Melville | Moby Dick | 8.99 |
| fiction | J. R. R. Tolkien | The Lord of the Rings | 22.99 |
And The response should contain "//unicorn" as empty array
And The response should contain "//book" with 4 elements
And The response is empty