Assertions in SOAP UI

In this tutorial, we are going to discuss Assertions in SOAP UI. What are the assertions, why do we add assertion and different types of assertions?

In this tutorial, we are going to discuss Assertions in SOAP UI. What are the assertions, why do we add assertion and different types of assertions?

Assertions is most important topic for interview point of view. It is generally asked into API testing interviews.

You can read this link for API testing interview preparation.

What is an Assertion?

In one line we can say Assertions means validations of responses. In this we check expected and actual value is equal or not.

Once we sent the request to the web server and receive a response then we need to validate that response data using assertion. Any number of assertions can be added into test steps.

Assertions in SOAP UI

Types of Assertions                                             

Below are the types of assertions used in SOAP UI.

Property Content

  1. Contains
  2. Not Contains
  3. XPATH Match
  4. XQuery Match

Compliance Status Standard

  1. Invalid HTTP status codes
  2. Valid HTTP status codes
  3. Not SOAP Fault
  4. SOAP fault
  5. Schema Compliance

Script

 Script Assertion

SLA

  1. Response SLA

JMS

  1. JMS Status
  2. JMS Timeout

Security

  1. Sensitive Information Exposure

 JDBC

  1. JDBC Status

Here we will discuss few important Assertions which we are used in SOAP UI.


1. Property Content

By using this we can validate properties of response after triggering a SOAP request.

Contains:

It is used to validate specified string into response. It is also supported to the regular expression.

Initially there is no assertion added in the assertion section. You have to add based on your requirements.

Step -1: Click on ‘+’ Icon to add assertion

Contains Assertion

Step-2: Select property content and then select Content.

Property Content- Contains

Step 3: Enter the expected value and Click on ‘Ok’.

Step 4: In below image you can see Contains assertions is Valid means it is matched with response value.

Note: If we changed expected value then it got failed. See in below Image.

 


Not Contains:

It is used to validate string which should not be present into response.

Below are steps to add this assertion.

Step1: After click on add ‘+’ icon

  • Select Property Content à Select Not Contains à Click on ‘OK’ button
  • Enter expected string which is not present into response and Click on OK.

Not Contains Assertion

  • After added this we can see the result in Assertions section.

 

Note: If we added any string which is matched to the response then it got failed.


XPath Assertions in SoapUI:

It uses XPath expression to point the expected node and its value.

Same from property content you will find Xpath match.

Below are steps to add this assertion.

Step1: After click on add ‘+’ icon

  • Select Property Content–> Select Xpath Match –> Click on ‘Add’ button

Xpath Assertion

Step 2: For adding XPath first we need to declare namespace. An XML namespace is a collection of names which is used to provide unique name to XML elements.

To declare the namespace, just click on declare button.

Xpath match Assertion

Step 3: Now we need to enter the XPath using namespace. See in below image.

In above image put node name with //ns1 or * (wildcard). It can be like below.

//ns1:value or //*:value

Step 4: Click on ‘Select from Current’. See in below image.

After Click on save,  you will get the Green color in Assertion section.

XPath match Assertion Result

 

Bonus Point: If you want to validate particular node then use exists. It will return Boolean value. See in below image.


XQuery Assertion in SoapUI:

XQuery match is used to querying the XML data. Same like SQL in database and here XQuery for XML. It is based on XPath expression. It uses functions to extract data from documents.

Here one question can come in your mind.

What is basic difference between XPath and XQuery?

In XPath, we just extract data or extract single node or multiple nodes. We cannot apply any condition while extracting the data/node. But in case of XQuery we can extract the data based on some conditions.

Step 1: After click on add ‘+’ icon

Select Property Content –> Select XQuery Match –> Click on ‘Add’ button.

XQuery match Assertion

After Click on Add button it will show the popup of XQuery match assertion.

XQuery Assertion

Step 2:  Before writing XQuery match, we need to declare namespace by clicking on Declare button.

XQuery Namespace

In my case syntax of declaration namespace:

declare namespace ns2=’http://example.com/’;

declare namespace S=’http://schemas.xmlsoap.org/soap/envelope/’;

 

Before going to use XQuery match, I have imported the Football Players wsdl.

WSDL: https://ws.footballpool.dataaccess.eu/info.wso?WSDL

You can use the same wsdl for practice.

Here, by XQuery match we will try to find all players name and validate it.  In below image you can see I have already imported Football wsdl file.

 

Step 3:  In step 2, we have already declared the namespace. Now we will write the XQuery expression.

XQuery expression should always put into <result> tag. Inside the result tag I will create one tag <name> and inside this I’ll write xpath of Players name.

Case 1:

In my case I want to validate 2nd player name from the response. See in below image.

My XQuery Expression:

declare namespace soap='http://www.w3.org/2003/05/soap-envelope';
declare namespace m='https://footballpool.dataaccess.eu';

<result>
<name>
{
data(//m:AllPlayerNamesResponse/m:AllPlayerNamesResult/m:tPlayerName[2]/m:sName/text())
}
</name>
</result>

Case 2: In this case we will try to find out all players name and validate it. Here, we will take the variables and utilize those on XQuery match expression.

XQuery expression in above image:

declare namespace soap='http://www.w3.org/2003/05/soap-envelope';
declare namespace m='https://footballpool.dataaccess.eu';

<Result>
{
for $z at $i in //m:tPlayerName
order by $i
return <Name>{$i}---{data($z/m:sName)} </Name>
}
</Result>

Explanation: In above syntax, $i is used for showing the order number of name and $z is used for Players name.

In coming tutorial, we will discuss remaining assertion (Compliance, Script, Security, SLA, JMS etc.)


Watch this Video for XPath and XQuery Match Assertion:


 

Summary:

In this tutorial, we have learnt Assertions in SOAP UI – Property Content assertion which are very useful to validating the SOAP UI response.

In future I will add more real time API testing interview questions.

If you have other questions related API Interview Questions or feedback, the comment section is yours. Don’t forget to leave a comment below!

Must Read: Top 21 Web Services Interview Questions

 

 

Leave a Comment