- csactor

Breaking

csactor

computer science , software engineering ,information system and information technology lesson series like data structure and algorithm , computer networking , programming , database , web development , operating system , information system , digital marketing and business study.

Sunday, June 14, 2020


SoupUI Assertion


Assertions are validation rules applied to test results that check how your target web service performs. In SoapUI tests, you use assertions to verify response codes, response headers, individual values in response bodies, request execution time and to do other checks.

Once a request is sent to a web server a response is received. We need to validate if the response contains the data that we expect. In order to validate the response, we need to use assertions.

Types of Assertion

  • Contains Assertion
  • Not Contains Assertion
  • X path match Assertion
  • Scripting Assertion
  • X Query Match Assertion


Contains Assertion

Checks for the existence of a specified string.
Contains Assertion checks the existence of a specified string in a response message.
By default, there is no assertion.

Step 01
 Click “Add a new assertion” to add an assertion.



Step 02
  • Select the Assertion Category
  • Select the Assertion Type
  • Click 'Add'



Step 03

 Let us validate if the string 'New Delhi' exist in the response. Click 'OK'


Step 04

Upon adding it, immediately assertion is executed and shows if VALID or INVALID.


Step 05

Now Let us say we change the content of 'Contains Assertion' to 'India' and see what happens.



Not Contains Assertion

Searches for the Non-existence of the specified string.

Not Contains Assertion is the counter part for the contains Assertion.

Step 01

Now after clicking on 'add new assertions' button,
  • Select the Assertion Category
  • Select the Assertion Type
  • Click 'Add'


Step 02
Now let us change the contents of the 'Not Contains Assertion' and see what happens. We will check for the non-existence of the string "New Delhi".







X path match Assertion

XPath assertion uses XPath expression to select the target node and its values. It compares the result of an XPath expression to an expected value. XPath is an XML query language for selecting nodes from an XML.

Step 01

Now after clicking on 'add new assertions' button,
  • Select the Assertion Category
  • Select the Assertion Type - 'X path Match'
  • Click 'Add'

XPath Match Configuration wizard opens. Before adding a XPath, declaration of the NameSpace is required. An XML namespace is a collection of names, identified by a Uniform Resource Identifier (URI) reference, used in XML documents as element and attribute names. The same is used in SoapUI XPath Assertion.
Step 02 − For declaring XML Namespace, click the 'Declare' button, which would do the job, else manually declare a namespace.
Step 03 − After declaring the namespace, refer the XPath using the created namespace.


Step 04
Enter the XPath of the XML node that we need to validate.
//ns1:CapitalCityResult Gives us the Value of the node enclosed between <m:CapitalCityResult> & </m:CapitalCityResult> 
After entering the XML, we need to click on 'Select from current' so that value from the current response would be picked up for comparison going forward.

Step 05
Click Select from current & Save icon


Scripting Assertion



Script Assertion technique is the most widely used technique since managing and maintaining hundreds of assertions are quite difficult for a long response message.
SoapUI uses either Groovy Scripting or JavaScript for scripting assertions. The scripting technique is adopted for developing a framework for testing SOAP.

SOAP UI uses either Groovy Scripting or java script for scripting assertions. The scripting technique is adopted for developing a framework for testing SOAP. 

Step 01
Now after clicking on 'add new assertions' button,
  • Select the Assertion Category
  • Select the Assertion Type
  • Click 'Add'

  Step 02 

The Scripting Dialog opens where user will be able to write user defined script to validate the response XML.                 



                          

Step 3 

Now let us write a groovy script to validate the Conversion Rate. The Script is attached below with the comments embedded. 



//Define Groovy Utils and holder for validating the XML reponse content

def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)

def holder = groovyUtils.getXmlHolder(messageExchange.responseContent)



//Define the NameSpace

holder.namespaces["m"] = "http://www.oorsprong.org/"

//Get the Value of the Node 'CapitalCity Result' and assign to a variable
def CapitalCityResult = holder.getNodeValue("//m:CapitalCityResult")

//print the value of the result in the Output panel
log.info "The result value for string is " + CapitalCityResult

//Comparing the value to print 'Pass' or 'Fail'
if(CapitalCityResult=="New Delhi")
{ log.info "Pass" }
else
{ log.info "fail"}

  1. Click 'Execute' Button to trigger the execution.
  2. The output of the Script is shown in the Output pane. It has printed both, Conversion Value as well as the end result (Pass or Fail)
  3. The Information is displayed that 'Script Assertion Passed'. Click OK.

Step 4

Now the assertion Tab displays all the assertions that we had added for this test suite with the Status against each one of those.




No comments:

Post a Comment