Tuesday, 4 October 2016

Test SOAP Services using REST Assured API

REST Assured(RA) is a framework built on Java, developed to test the REST services. However we can test SOAP services too by forming the request in the below format.

RA has inbuilt support for multiple authentication like BASIC, OAuth, OAuth2, Form, Certificate, Digest, CSRF (Cross Site Request Forgery) etc.

RA out of the box has the support for BDD approach.

Follow the below steps to configure & test the SOAP web services.



STEPS:


STEP 1: Download the latest version of REST ASSURED binaries from the below location.

          REST Assured Binaries.


STEP 2: Create the java project in eclipse & add the 'rest-assured-*.*.*.jar' and other dependent jars into project build path.



STEP 3: Prepare the required Input parameters like 

String url = "https://app.test.com/start.swe?src=WebService&Username=abcd";

String requestType = "POST";

//SOAP Action & content-type header params to be passed for SOAP services
HashMap headermap = new HashMap<>();
headermap.put("Content-Type", "text/xml");
headermap.put("SOAPAction", "\"document/http://www.app1.com/Rk:IR\"");
headermap.put......

//Assign the SOAP body to the below variable.
String reqBody = "<soapenv:Envelope
    xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"
    xmlns:rk=\"http://www.app1.com/Rk\"
    xmlns:r1=\"http://xml/app/rack\">"+
" <soapenv:Header/>"+
"<soapenv:Body>"+
"<rk:IR_Input>"+
"<r1:LR>"+
"<r1:Id>TstAPI1</r1:Id>"+
"</r1:LR>"+
"</rk:IR_Input>"+
"</soapenv:Body>"+
"</soapenv:Envelope>";

//Path of the response node, which need to be validated with the expected value
String validationKey = "reponse.test.no";
String expVal = "A04BH12";
String actualVal = "";

//Variable to capture response.
String resp = "";

STEP 4: Pass the above details as input to the REST Assured APIs, which would execute the request & validate the response.

if(requestType == "GET")
resp = RestAssured.given().log().all().headers(headermap).get(url);
else if(requestType == "POST")
resp = RestAssured.given().log().all().headers(headermap).body(reqBody).post(url);
..................
..........
.....
....
..

//Extract the required key value & compare with the expected value.

XMLPath path = new XMLPath(resp);
actualVal = path.getString(validationKey); 
if(actualVal == expVal)
   System.out.println("Test PASS");
else
   System.out.println("Test FAIL");




References:
https://github.com/rest-assured/rest-assured/wiki/GettingStarted
https://github.com/rest-assured/rest-assured/wiki/Downloads

33 comments:

  1. Replies
    1. Certainly Ricky. The above code snippet lets you know how to automate the SOAP webservices / SOAP API testing.
      There is one more article specifying a way to automate REST webservices / API.
      http://advancedtestautomation.blogspot.in/2016/08/test-rest-web-services-using-rest.html

      Delete
  2. This is wrong code

    ReplyDelete
    Replies
    1. The above mentioned steps are working for me. Also note, i have not given complete code as such. I have given the steps to make the Soap services work using REST assured. Let me know if you need any help in resolving your issues.

      Delete
  3. Hi how can i add the certificate for the soap request using rest assured ?

    ReplyDelete
    Replies
    1. RestAssured has multiple ways to handle Certificates.
      you can use relaxedHTTPSValidation() & relaxedHTTPSValidation(String protocol) methods to handle the certificate without actually requiring you to provide the certificate details.

      Ex:
      RestAssured.given().relaxedHTTPSValidation().headers(headermap).get(url);

      Delete
  4. how to add keystore to the rest assured for ex: some soap url's are working based on certificates only. without certification unable to open the wsdl even in soap-ui also

    ReplyDelete
    Replies
    1. Rest Assured has different ways to deal with certificates.
      It provides a easy way to escape the cert using 'RelaxedHTPPSValidation' feature & other options like loading the keystore files through code.

      RestAssured.useRelaxedHTTPSValidation();
      OR
      RestAssured.config = RestAssured.newConfig().sslConfig(new SSLConfig("/truststore_javanet.jks", "test1234");
      OR
      KeyStore keyStore = KeyStore.getInstance("PKCS12");
      keyStore.load(new FileInputStream("certs/cert.p12"), password.toCharArray());

      org.apache.http.conn.ssl.SSLSocketFactory clientAuthFactory = new org.apache.http.conn.ssl.SSLSocketFactory(keyStore, password);
      SSLConfig config = new SSLConfig().with().sslSocketFactory(clientAuthFactory).and().allowAllHostnames();
      RestAssured.config = RestAssured.config().sslConfig(config);
      RestAssured.given().when().get("/path").then();

      Delete
  5. hi shiv can you give me the complete sample of automation of soap service using restssured

    ReplyDelete
    Replies
    1. Most of it is there in the above article. Let me know where do you feel difficulty in understanding. I can suggest you the solution.

      Delete
    2. Hey can u give me the complete code to validate SOAP xml in cucumber by adding rest-ssured dependency in pom.xml??

      Delete
    3. Hi all please help me if any body know the solution..I have a sopui link..with me ....they are asking to me for validate all the tag using rest assured...is it possible....pl plea share ur knowledge with me....

      Delete
  6. Hi Shiva, Please find the below request body having multiple level of headers. So how to pass all these header and body in above code. Can you please edit the code basis this request.





    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?



    ?
    ?






    ?

    ?



    ?

    ?


    ?
    ?

    ?

    ?

    ?

    ?

    ?

    ?

    ?



    ?

    ?





    false

    false

    false

    false

    false

    false

    false

    false

    false

    false

    false

    false

    false

    false

    false

    false

    false

    false

    false

    false

    false

    false

    false





    ReplyDelete
  7. I guess my soap request is not posted in correct format, Can you share your mailid so that i will send you complete request as attachment

    ReplyDelete
  8. Hi Shiva,
    Could you please share the SOAP services testing with rest assured in detail with example.
    I am unable to access the SOAP response xml file contents using rest assured.

    ReplyDelete
    Replies
    1. Hope you have used the 'SOAPAction' header properly.And the response you get should be in String Format.
      While Using
      XMLPath path = new XMLPath(resp);
      actualVal = path.getString(validationKey);
      validationKey = is the node path in '.' format like reponse.test.no.

      Delete
  9. Okay Thank you, even it worked for NodeChildren is also worked, but how can I avoid giving long paths, for ex: xmlPath.get("Envelope.Body.ElementsResponse.entityList.entity[0].phoneList.phone").toString(), it looks tedious and also vulnerable to change. Can we have anything like selenium webdriver relative xpaths or any way to traverse effectvely.

    ReplyDelete
  10. Hi Shiva,
    How can we avoid giving long paths, for ex: xmlPath.get("Envelope.Body.ElementsResponse.entityList.entity[0].phoneList.phone").toString(), it looks tedious and also vulnerable to change. Can we have anything like selenium webdriver relative xpaths or any way to traverse effectvely.

    ReplyDelete
    Replies
    1. As far as i know, there is no such option in RestAssured Libraries.
      Hope to get in the newer versions. :)

      Delete
    2. Hi Shiva...I have sopui link...I need to validate all the tag name from sopui link by using the rest assured..can u suggest or guide to me how I can achieve that

      Delete
    3. I am not sure if i understand your requirement. What do you mean by SoapUI link..?
      CAn you explain your requirement.

      Delete
  11. Thank you so much for providing such a useful piece of information.
    SQL Server Load Soap Api

    ReplyDelete
  12. Do you have git repo for this code ? if yes could you please share URL

    ReplyDelete
  13. Hi, My SOAP API is working fine thru SOAPUI tool, but when I call the service thru RestAssured, I am getting a unexpected response.

    Has anyone faced similar issue ?

    ReplyDelete
    Replies
    1. hope you have added the soap action as part of rest assured headers..

      Delete
    2. if you have no issue, can you share code?

      Delete
  14. This method was helpful for me: relaxedHTTPSValidation

    ReplyDelete
  15. Thanks brother.
    To find out SOAPAction, goto SoapUI tool and run you Soap web service here. Then open its one of the request from the project. Then open the "RAW" type content of your request. You will find the SOAPAction here.

    ReplyDelete
  16. Hi Shiva,

    How Can I authenticate ( Basic ) SOAP request in Rest assured framework?

    ReplyDelete
  17. Yes , I have also same question, could you pls include Basic authentication for SOAP service through REST assured framework ?

    ReplyDelete
  18. Hi Shiva
    can you please let me know what is SOAP ACTION ? I'm trying to do one sample call for this WSDL http://www.dneonline.com/calculator.asmx?wsdl and I am not able to find anything with name as SOAP ACTION . can you help me in this please , I will be really helpfull

    ReplyDelete
    Replies
    1. one of the comment above has given the steps to find soap action while executing on SOAPUI.
      "To find out SOAPAction, goto SoapUI tool and run you Soap web service here. Then open its one of the request from the project. Then open the "RAW" type content of your request. You will find the SOAPAction here."

      Delete
  19. Hello, I am getting an error while running the code. Can you please share your email id so that I can share you the exact details ? Thanks.

    ReplyDelete