In the previous tutorial, we learned about working with JSON responses using Rest Assured. In this tutorial, we will explore the usage of XML responses.
XML stands for eXtensible Markup Language and is a markup language used for encoding documents in a format that is both human-readable and machine-readable. XML is commonly used in web applications for data exchange.
Rest Assured provides a rich set of methods to work with XML responses. Two commonly used methods are XPath and Matchers. XPath is a language used for navigating through XML documents and Matchers is used to perform assertions on XML responses.
To work with XML responses, we can use the .xmlPath() method provided by Rest Assured. This method converts the response body to an instance of XmlPath which can be used to extract data from the XML document. The following example demonstrates how to use XPath to extract data from an XML response:
Response response = given().get("https://someapi.com/users");
XmlPath xmlPath = response.xmlPath();
String firstName = xmlPath.get("users.user[0].firstName");
In the above example, we send a GET request to the endpoint https://someapi.com/users and get the response. We then convert the response body to an instance of XmlPath and use XPath to extract the firstName of the first user in the response.
XPath is a query language used to navigate and select elements in an XML document. It provides a way to select nodes and attributes from an XML document based on their location and properties. In Rest Assured, XPath can be used to extract data from an XML response.
Here's an example of using XPath to extract data from an XML response:
given()
.when()
.get("https://example.com/api/xmlResponse")
.then()
.assertThat()
.body(hasXPath("/response/status/text()").equalTo("success"))
.body(hasXPath("/response/data/name/text()").equalTo("John Doe"));
In the above example, we are sending a GET request to an API that returns an XML response. We are using the hasXPath method along with equalTo to validate that the response has a status element with a value of "success" and a name element with a value of "John Doe".
Matchers, on the other hand, are used to validate the structure and content of an XML response. Matchers provide a set of predefined rules that can be used to validate an XML response. In Rest Assured, Matchers can be used with XML responses as well as JSON responses.
Here's an example of using Matchers to validate an XML response:
given()
.when()
.get("https://example.com/api/xmlResponse")
.then()
.assertThat()
.body("response.data", hasXPath("count(//name)", equalTo("3")))
.body("response.data", hasXPath("count(//age)", equalTo("3")))
.body("response.data", hasXPath("//name[contains(text(), 'John')]"))
.body("response.data", hasXPath("//age[contains(text(), '30')]"));
In the above example, we are validating that the response has three name elements and three age elements. We are also validating that the response has a name element containing the text "John" and an age element containing the text "30".
Overall, using XPath and Matchers in Rest Assured can help you extract data and validate XML responses with ease.
Comments
Post a Comment