Script assertion in Soap UI for WSDL API methods

In this post I will show you how to add script assertion in SoapUI response of wsdl api test methods.
Here I have an example of CurrencyConvertor, I created a SoapUI project and added test case for this method as you can see in below window.

Now here in response body I will add assertion that verify the “ConversionRateResult” value is always 104 for given input value.
Steps to add script assertion:
  • Click on “Assertions” button at bottom of left side.
  • Clicks add icon as point 1 mentioned on below image.
  • Chose script option as mentioned point 2.
  • Click script assertion as mentioned point 3.
  • Click add button as mentioned point 4.

You can see a window opened where you can add your script assertion code. 
When I execute “CurrencyConvertor” with input parameter values “USD”, “ALL” got below response message 
   Approach to write script assertion for above response:

1. Import xml holder class at first line and create XmlHolder object.
import com.eviware.soapui.support.XmlHolder

def holder = new XmlHolder( messageExchange.responseContentAsXml )

2. Add name space for http://www.webserviceX.NET as in response.
 holder.namespaces["ns1"] = "http://www.webserviceX.NET/"

3. To verify node ConversionRateResponse.ConversionRateResult add below code:
def node = holder.getDomNode( "//ns1:ConversionRateResponse[1]/ns1:ConversionRateResult[1]" )
assert node != null
4. To verify node value of ConversionRateResponse.ConversionRateResult add below code:
def response = context.expand( '${ConversionRate#Response#declare namespace ns1=\'http://www.webserviceX.NET/\'; //ns1:ConversionRateResponse[1]/ns1:ConversionRateResult[1]}' )
assert response != null
assert response == "104"

Complete assertion code:
import com.eviware.soapui.support.XmlHolder

def holder = new XmlHolder( messageExchange.responseContentAsXml )
holder.namespaces["ns1"] = http://www.webserviceX.NET/

def node = holder.getDomNode( "//ns1:ConversionRateResponse[1]/ns1:ConversionRateResult[1]" )
assert node != null

def response = context.expand( '${ConversionRate#Response#declare namespace ns1=\'http://www.webserviceX.NET/\'; //ns1:ConversionRateResponse[1]/ns1:ConversionRateResult[1]}' )
assert response != null
assert response == "104

Hope this will help you add script assertion in your SoapUI wsdl api test methods.

No comments:

Post a Comment

Leave your comments, queries, suggestion I will try to provide solution