Exploring HTTP requests types (GET, POST, PUT and DELETE) and...
Read MoreSOAP is an XML-based protocol for accessing web services over HTTP. It has some specifications which could be used across all applications.
SOAP is known as the Simple Object Access Protocol, but in later times was just shortened to SOAP v1.2. SOAP is a protocol or in other words is a definition of how web services talk to each other or talk to client applications that invoke them. The diagram below shows the various building blocks of a SOAP Message.
The SOAP message is nothing but a mere XML document which has the below components.
Let's see how we can handle SOAP APIs using KarateDSL
We’ve already seen how KarateDSL handles REST APIs.
Do you want to know how SOAP APIs are handled in the karate DSL?
All of the examples in this article will make use of the API listed below
Response:
Let’s look at some SOAP API implementations using the Karate DSL:
Example 1: Print the response of SOAP API
Step 1: Create a new feature file in the src/test/java directory.
Step 2: Copy and paste the code below into the feature file.
Step 3: Replace the name of the feature file in the Runner file, then hit the execute icon (I am using Eclipse IDE).
Step 4: To see the report, copy the html link from the console and paste it into any browser, like shown below.
Karate Report is as follows:
Example 2: Get the value present for “AddResult” and assert the value
Step 1: To get the value of the “AddResult” key, we can use the same feature file.
The value for the specified key can be acquired in one of the following ways:
1st Method- Use a static/absolute path.
Method 2- By using relative/dynamic path
Please note-
Feature file is as follows:
Step 2: Execute the Runner file.
Step 3: Copy the html link and paste it on browser to view Karate Report which looks like-
The techniques for handling SOAP API using Karate DSL have been demonstrated in the the above examples.
Let's see how we can handle SOAP APIs using POSTMAN
Postman is not only for REST client, this supports SOAP APIs too. In the recent times Postman added many features to support the handling of SOAP APIs. Please read this blog to know more about directly importing the WSDL files into Postman.
Postman is not just for REST APIs. Now we can see an example of SOAP API and how to handle that in Postman. Parsing the SOAP response is a bit similar to REST, but there are some differences. We need to understand the SOAP response better to parse them.
For our demo, we will be using a Public SOAP API calculator endpoint for addition. Let’s get into our example.
Step 1: Create a new request in Postman and paste the URL: http://www.dneonline.com/calculator.asmx
Note: For all SOAP API endpoint the HTTP method will be POST.
Step 2: Along with the existing headers, make sure to add the “Content-Type” as “text/xml”
Step 3: We need to enter the body as raw XML
Paste the below content as your body:
Here the fields intA and intB are two different values which are editable. For every run we can update them and see different test results.
Step 4: Click “Save” and “Send”
Yes, we finally did a SOAP API call in Postman.
We got the XML response back from the server which needs to be parsed/split as small elements to use that in future.
Method 1:
Step 1: Same as JSON response, we need to store the XML response as JSON object using xml2Json() method
var jsonObject = xml2Json(responseBody);
console.log(jsonObject);
Now the response object looks like below:
Step 2: We are already aware of the variables in Postman, and we can now try to reach the desired element and store them as variables.
Ignore the first line in the response “<?xml version=”1.0″ encoding=”utf-8″?>“
Start from “soap:Envelope” tag
var result = jsonObject['soap:Envelope']['soap:Body']['AddResponse']['AddResult'];
pm.environment.set("Result", result);
Now the result is stored in a variable which can be used further in our Test scripts/assertions.
That’s it. It’s simple.
Method 2:
Incase if you have few elements under the same tag you can save that path as a variable. And you can use that variable and append the name of the desired tag after that.
For example,
var respResult = jsonObject['soap:Envelope']['soap:Body']['AddResponse'];
var result1 = respResult['AddResult'];
pm.environment.set("Result1", result1);
Both of them returns the same value.
Method 3:
Using cheerio.js
If the response is lengthy and you need to go through many tag names to reach your desired element, we have another option too. One of my favorite things about Postman is, it’s abilities to support the JavaScript libraries. One such amazing library is cheerio.js in NPM package. Any XML/HTML page can be scraped using cheerio. We can use them in Postman for parsing the XML/HTML responses. Interesting isn’t? Let’s discuss more on this.
By using this method we can directly access the tag name using the $ symbol. All the result fields will have the same value.
Wow, we actually parsed the XML response of an SOAP API. It might be a bit tough initially, so always try to understand the response structure. Once you are familiar you can try to adopt using cheerio because this will save lot of your time and can avoid lengthy scripts.
We discussed handling the SOAP API requests/responses in Postman and KarateDSL. Each of them have their own way to parse the response. Hope you enjoyed reading this. We just tried with the single endpoint, try to use different SOAP APIs and play with the responses. This will help in writing the assertions.
In the next post, we will be presenting you a quiz, where you can evaluate your learnings alongside winning some cool prizes!
Dear readers, thank you for your time. See you in the next few days with our quiz!!
Exploring HTTP requests types (GET, POST, PUT and DELETE) and...
Read MoreExploring different Auth Types(Basic Auth, API_Key, OAUTH2.0) and how to...
Read MoreExploring handling of REST API endpoints using Postman and KarateDSL
Read More