Home > Software design >  How do i extract the SOAP XML Response Tag and Validate it in Rest Assured using TestNG
How do i extract the SOAP XML Response Tag and Validate it in Rest Assured using TestNG

Time:11-11

This is my Code :

package testcases;

import io.restassured.RestAssured;
import io.restassured.path.xml.XmlPath;
import io.restassured.response.Response;
import org.apache.commons.io.IOUtils;
import org.testng.annotations.Test;

import java.io.FileInputStream;

import static io.restassured.RestAssured.given;
public class api_automation {


        @Test
        public  void postMethod() throws Exception{

            FileInputStream fileinputstream= new FileInputStream("/home/intellij-projects/rest-assured/src/main/java/testcases/request.xml");
            RestAssured.baseURI="*******";
            Response responses= given()
                    .header("Content-Type", "text/xml")
                    .and()
                    .body(IOUtils.toString(fileinputstream,"UTF-8"))
                    .when()
                    .post("******")
                    .then()
                    .statusCode(200)
                    .and()
                    .log().all().extract().response();


            XmlPath xmlPath=new XmlPath(responses.asString());
            String StatusCode= xmlPath.getString("<StatusCode>");

            System.out.println("Status Code is "  StatusCode);

        }

    }

Output of My Code

HTTP/1.1 200 OK
Date: Thu, 10 Nov 2022 12:20:21 IST
Content-Type: text/xml;charset=iso-8859-1
Expires: Thu, 10 Nov 2022 12:20:21 IST
Content-Length: 951
Server: Jetty(9.4.14.v20181114)

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <ns4:SendMessageElement xmlns:ns6="http://******/uwl/ws/message/wsdl/error" 
xmlns:ns5="http://*****/uwl/ws/message/wsdl/common" 
xmlns:ns4="http://*****/uwl/ws/message/wsdl/send" 
xmlns:ns3="http://*****/uwl/ws/message/wsdl/application" 
xmlns:ns2="http://*****/uwl/ws/message/wsdl/header" 
xmlns="http://*****/uwl/ws/message/wsdl/acm">
      <ns4:MessageId>1101220210165</ns4:MessageId>
      <ns4:StatusCode>Success-2000</ns4:StatusCode> ---This Tag needs to be validated
      <ns4:StatusDetail>Success</ns4:StatusDetail>
      <ns4:DestinationResponses>
        <ns4:destinationResponse>
          <ns4:Destination>1000030</ns4:Destination>
          <ns4:TimeStamp>20221110122021</ns4:TimeStamp>
          <ns4:MessageId>43</ns4:MessageId>
          <ns4:StatusCode>Success-2000</ns4:StatusCode>
          <ns4:StatusDetail>Success</ns4:StatusDetail>
        </ns4:destinationResponse>
      </ns4:DestinationResponses>
    </ns4:SendMessageElement>
  </soap:Body>
</soap:Envelope>


Status Code is1101220210165Success-2000Success10000302022111012202143SBL-SMS-MT-2000Success

===============================================
Default Suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================


I want the Test to Pass provided that the given Status Code Matches the Response Tag Status Code

Like in the Code declare a variable and the response should be equal to that

I want to match the following tag with the expected outcome:

  <ns4:StatusCode>Success-2000</ns4:StatusCode>

With the following lines , i am getting other tag responses as well

        String StatusCode= xmlPath.getString("<StatusCode>");

        System.out.println("Status Code is "  StatusCode);

Output :

Status Code is1101220210165Success-2000Success10000302022111012202143SBL-SMS-MT-2000Success

CodePudding user response:

Use this path: Envelope.Body.SendMessageElement.DestinationResponses.destinationResponse.StatusCode

Test is:

public static void main(String[] args) {
    String result = RestAssured
            .get("https://run.mocky.io/v3/08f352c8-641d-4adc-b2e6-49add556c883")
            .then()
            .extract()
            .xmlPath()
            .getString("Envelope.Body.SendMessageElement.DestinationResponses.destinationResponse.StatusCode");
    System.out.println(result);
}

Output: Success-2000

  • Related