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.
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 = "";
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
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
Is this code helps me in API Testing automation
ReplyDeleteCertainly Ricky. The above code snippet lets you know how to automate the SOAP webservices / SOAP API testing.
DeleteThere 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
This is wrong code
ReplyDeleteThe 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.
DeleteHi how can i add the certificate for the soap request using rest assured ?
ReplyDeleteRestAssured has multiple ways to handle Certificates.
Deleteyou 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);
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
ReplyDeleteRest Assured has different ways to deal with certificates.
DeleteIt 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();
hi shiv can you give me the complete sample of automation of soap service using restssured
ReplyDeleteMost of it is there in the above article. Let me know where do you feel difficulty in understanding. I can suggest you the solution.
DeleteHey can u give me the complete code to validate SOAP xml in cucumber by adding rest-ssured dependency in pom.xml??
DeleteHi 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....
DeleteHi 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.
ReplyDelete?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
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
ReplyDeleteHi Shiva,
ReplyDeleteCould 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.
Hope you have used the 'SOAPAction' header properly.And the response you get should be in String Format.
DeleteWhile Using
XMLPath path = new XMLPath(resp);
actualVal = path.getString(validationKey);
validationKey = is the node path in '.' format like reponse.test.no.
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.
ReplyDeleteHi Shiva,
ReplyDeleteHow 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.
As far as i know, there is no such option in RestAssured Libraries.
DeleteHope to get in the newer versions. :)
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
DeleteI am not sure if i understand your requirement. What do you mean by SoapUI link..?
DeleteCAn you explain your requirement.
Thank you so much for providing such a useful piece of information.
ReplyDeleteSQL Server Load Soap Api
Do you have git repo for this code ? if yes could you please share URL
ReplyDeleteHi, My SOAP API is working fine thru SOAPUI tool, but when I call the service thru RestAssured, I am getting a unexpected response.
ReplyDeleteHas anyone faced similar issue ?
hope you have added the soap action as part of rest assured headers..
Deleteif you have no issue, can you share code?
DeleteThis method was helpful for me: relaxedHTTPSValidation
ReplyDeleteThanks brother.
ReplyDeleteTo 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.
Hi Shiva,
ReplyDeleteHow Can I authenticate ( Basic ) SOAP request in Rest assured framework?
Yes , I have also same question, could you pls include Basic authentication for SOAP service through REST assured framework ?
ReplyDeleteHi Shiva
ReplyDeletecan 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
one of the comment above has given the steps to find soap action while executing on SOAPUI.
Delete"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."
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