APIs Unleashed 04:
Handling SOAP APIs

banner

SOAP 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.

soap structure

The SOAP message is nothing but a mere XML document which has the below components.

 

  • - An Envelope element that identifies the XML document as a SOAP message – This is the containing part of the SOAP message and is used to encapsulate all the details in the SOAP message. This is the root element in the SOAP message.

 

  • - A Header element that contains header information – The header element can contain information such as authentication credentials which can be used by the calling application. It can also contain the definition of complex types which could be used in the SOAP message. By default, the SOAP message can contain parameters which could be of simple types such as strings and numbers, but can also be a complex object type.

 

  • -  A Body element that contains call and response information – This element is what contains the actual data which needs to be sent between the web service and the calling application. Below is an example of the SOAP body which actually works on the complex type defined in the header section. Here is the response of the Tutorial Name and Tutorial Description that is sent to the calling application which calls this web service.

KarateDSL: Handling SOAP APIs

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

API: http://www.dneonline.com/calculator.asmx

 

Method: Post

 

Request Body:

request body

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.

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).

runner file

Step 4: To see the report, copy the html link from the console and paste it into any browser, like shown below.

report

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 1

Method 2- By using relative/dynamic path

method 2

Please note-

 

  1. 1.  In JsonPath, "$" is referred to as the "root member object."

 

  1. 2. When using above path, the value is enclosed in square brackets, indicating that it is an array.

 

Feature file is as follows:

feature file

Step 2: Execute the Runner file.

 

Step 3: Copy the html link and paste it on browser to view Karate Report which looks like-

report

The techniques for handling SOAP API using Karate DSL have been demonstrated in the the above examples.

POSTMAN: Handling SOAP APIs

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

postman

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

request body

Paste the below content as your body:

body content

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"

send
xml

Yes, we finally did a SOAP API call in Postman.

How do we parse the response?

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:

response object

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

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.

cheerio

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!!

Missed Previous Chapters?

Disclaimer
This is truly a parallel learning series, which is not meant for promotion or degradation of any tools or technologies. We are aiming to give a learning experience for our audience in terms of common scenarios.

Know Our Synapses

Meet the brains behind this learning series!

Pricilla Bio






Priyanka Bio




Let's


Together

Follow Us

Stay updated with all the latest contents & announcements


Linkedin


Twitter


Instagram


Facebook