Skip to main content

Day 6: Working with XML responses

Day 6: Working with XML responses

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

Popular posts from this blog

How to integrate Autoit with selenium

For handling Dialog boxes which are not web based, Then Autoit is the best Tool to handle this These are the following code should written in Selenium Selenium Code: try { String[] commands = new String[] {}; commands = new String[] { "Path" }; // location of Autoit EXE file Runtime.getRuntime().exec(commands); } catch (IOException e) { } Autoit code: if WinWaitActive("File Upload") Then ;MsgBox(2,"window found","Found the window") WinActivate("File upload") Send("!n") Sleep(5000) Send("File path") SEND("{ENTER}") ;location of the file you want to a to the form and submit ;Send("!O") Else MsgBox(1,"TimeOut","Timed out") EndIf Here we need to create a Exe file for the Autoit script and that path should be mentioned in selenium code.

Reflection API

The Reflection API allows Java code to examine classes and objects at run time.The new reflection classes allow you to call another class's methods dynamically at run time. With the reflection classes, you can also examine an instance's fields and change the fields' contents. The Reflection API consists of the java.lang.Class class and the java.lang.reflect classes: Field, Method, Constructor, Array, and Modifier. Example program: import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; // calling method whose name we store in a variable public class ReflectionAPI { public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { String x="sampleTest"; //String meth=read name from xls file Method method= ReflectionAPI.class.getMethod(x, String.class); method.invoke(method, "welcome"); System.out.

Collection API

CollectionAPI: The Java Collections API's provide Java developers with a set of classes and interfaces that makes it easier to handle collections of objects. In a sense Collection's works a bit like arrays, except their size can change dynamically, and they have more advanced behaviour than arrays. Example program: import java.util.ArrayList; import java.util.Hashtable; public class CollectionAPI { /** * @param args */ public static void main(String[] args) { int names[] = new int[5]; ArrayList list = new ArrayList (); list.add("Ramu");//0 list.add("Venu");//1 list.add("Raju");//2 System.out.println(list.size()); for(int i =0; i<=list.size(); i++) { System.out.println(list.get(i)); } // key - value // key - unique Hashtable table = new Hashtable (); table.put("name", "Hyderabad"); table.put("place", "Mumbai"); table.put("na