• Home
  • Testing
  • SAP
  • Web
  • Must Learn!
  • Big Data
  • Live Projects
  • Blog

What Is An Assertion?

  • Assertion means act of affirming or stating something. It can also be interpreted as check point or a validation point.
  • 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

There are various ways of asserting a response; however we will focus on the commonly used Assertions types while validating a response. Below are the ones that are available in Open Source version of SoapUI.

  1. Property Content
  2. Compliance Status Standard
  3. Script
  4. SLA
  5. JMS
  6. Security
    1.  

      Assertions in SoapUI: Complete Tutorial

      Apart from the ones that are listed above, PRO version also has an inbuilt JDBC Assertion using which we can assert if the web service has updated the database correctly.

      In this tutorial we will study

      CONTAINS ASSERTION

      Searches for the existence of the specified string. It also supports regular expression.

      We will continue with the same example from the previous tutorial.

      Step 1: By Default there are no assertions.

      • The Number of Assertions are shown in the Assertions Tab.
      • To add a new assertion, click on 'Add New Assertion' button.

      Assertions in SoapUI: Complete Tutorial  

      Step 2: Now,

      1. Select the Assertion Category.
      2. Select the Assertion Type.
      3. Click 'Add'

      Assertions in SoapUI: Complete Tutorial  

      Step 3: Let us validate if the string '63' exist in the response. Click 'OK'

      Note : We can also ignore case and add regular expression.

      Assertions in SoapUI: Complete Tutorial


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

      Assertions in SoapUI: Complete Tutorial  

      Step 5: Now Let us say we change the content of 'Contains Assertion' to '64' and see what happens.

      Assertions in SoapUI: Complete Tutorial  

      Step 6: The Assertion is executed and the result is thrown to the user. Since we don't have the string "64" within the response, the assertion has failed.

      Assertions in SoapUI: Complete Tutorial  

      NOT CONTAINS ASSERTION

      Searches for the Non-existence of the specified string. It also supports regular expression.

      Step 1: Now after clicking on 'add new assertions' button,

      1. Select the Assertion Category.
      2. Select the Assertion Type – In this case 'NOT Contains'
      3. Click 'Add'
       

      Assertions in SoapUI: Complete Tutorial  

      Step 2: Let us validate if the string 'FromCurrency' exist in the response. Enter the string 'FromCurrency' and Click 'OK'

       Assertions in SoapUI: Complete Tutorial  

      Step 3: As soon as an assertion is added, it executes and displays the result. So far we have added two assertions hence both the assertions are executed and displayed the result.  

      Assertions in SoapUI: Complete Tutorial  

      Step 4: 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 "ConversionRateResponse".

      Assertions in SoapUI: Complete Tutorial  

      Step 5 : The string 'ConversionRateResponse' is actually present in the response, hence the 'NOT Contains' assertion will fail as shown below.

      Assertions in SoapUI: Complete Tutorial    

      XPATH MATCH ASSERTION

      Uses XPath expression to select the target node and its values. XPath, is an XML query language for selecting nodes from an XML document.

      Step 1: Now after clicking on 'Add New Assertions' button,

      1. Select the Assertion Category.
      2. Select the Assertion Type – In this case 'XPath Match'
      3. Click 'Add'

      Assertions in SoapUI: Complete Tutorial  

      Step 2: Add XPath Window opens.

      • Before Adding XPath, we need to declare the NameSpace. An XML namespace is a collection of names, identified by a Uniform Resource Identifier (URI) reference, which are used in XML documents as element and attribute names. The same is used in SOAP UI XPath Assertion.
         
      • For declaring XML Namespace, we just need to click on 'Declare' button which would do the job for us else we can also manually declare a namespace ourselves.
         
      • After declaring the namespace we need to refer the XPath using the created name space.
         
      • Upon clicking the 'Declare' button, two namespaces will pop up as we have two URI's. One of them is the schema URL and the other one corresponds to the actual web service URL. We need to use the actual namespace where the web service is located and NOT the schema namespace while referencing XPath.
           

        Assertions in SoapUI: Complete Tutorial

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

        declare namespace ns1='http://www.webserviceX.NET/';

      Assertions in SoapUI: Complete Tutorial

      Step 3: Now we need to enter the XPath of the XML node that we need to validate.

      //ns1:ConversionRateResult Gives us the Value of the node enclosed between <ConversionRateResult> & </ConversionRateResult> and ns1 corresponds to the declared namespace which is pointing to 'http://www.webserviceX.NET/'  

      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.  

      Assertions in SoapUI: Complete Tutorial  

      Step 4: So far,

      1. After declaring the namespaces, we have entered the XPath of XML node that we need to Validate.
      2. We Need to click 'Select from Current' to make the current value as the expected value.
      3. The current value is shown to the user which we can modify if required.
      4. Click 'Save'.

      Assertions in SoapUI: Complete Tutorial

      Step 5: The added Assertion will be displayed as shown below.

      Assertions in SoapUI: Complete Tutorial

      Xquery Match Assertion

      It Uses an Xquery expression to select content from the target property. We need a much bigger response XML in order to understand XQuery better let us import one other WSDL as shown below: http://www.webservicex.net/medicareSupplier.asmx?WSDL

      Step 1: Perform a Right click on the existing project and select 'Add WSDL'.

      Assertions in SoapUI: Complete Tutorial  

      Step 2: Perform a Right click on the existing project and select 'Add WSDL'. Leave other options as default and Click 'OK' Button.

      Assertions in SoapUI: Complete Tutorial  

      Step 3: All the operations are listed as shown below.

      Assertions in SoapUI: Complete Tutorial  

      Step 4: Now let us add a Test Case within the same test suite that we had created for Testing the currency converter.

      Assertions in SoapUI: Complete Tutorial  

      Step 5: Enter the name of the test case and Click 'OK' Button

      Assertions in SoapUI: Complete Tutorial  

      Step 6: The test case is created as shown below.

       

      Assertions in SoapUI: Complete Tutorial  

      Step 7: Add a new test step of Type 'Soap Test Request' as shown below.  

      Assertions in SoapUI: Complete Tutorial  

      Step 8: Enter the name of the test step. Let us say – Supplier_by_City which would be more meaningful Click 'OK'.  

      Assertions in SoapUI: Complete Tutorial

      Step 9: Select the Operation that we would like to validate. In this case it is 'MedicareSupplierSoap -> GetSupplierByCity'. Click 'OK'.

      Assertions in SoapUI: Complete Tutorial

      Step 10: Enter the Name of the test case and click 'OK'.

      Assertions in SoapUI: Complete Tutorial

      Step 11: The Request XML Outline would be displayed as shown below.

      Assertions in SoapUI: Complete Tutorial  

      Step 12: Now let us find all supplier information for the 'New York' City.

      To do so, add the following lines to your code.

      <GetSupplierByCity xmlns="http://www.webservicex.net/">

      <City>New York</City>

      </GetSupplierByCity>

      WSDL in the below URL - http://www.webservicex.net/medicareSupplier.asmx?op=GetSupplierByCity

      Assertions in SoapUI: Complete Tutorial  

      Step 13: Upon executing the test, we receive the below response  

      Assertions in SoapUI: Complete Tutorial  

      Step 14: Let us say we need to Validate all the Supplier Number. We can't use XPath Assertion as we need to have hundreds of XPath Assertion. Hence the usage of XQuery is inevitable in this case.

      XQuery Assertion helps us to validate a group of XML response which are repetitive in nature.  

      Assertions in SoapUI: Complete Tutorial  

      Step 15: Now click on 'Add an assertion',

      1. Select the 'Assertion Category' – Property Content in this case.
      2. Select the Assertion Type as 'XQuery Assertion'
      3. Click 'Add'.

      Assertions in SoapUI: Complete Tutorial  

      Step 16: Similar to XPath Assertion we need to declare the namespace.

      1. Click 'Declare' Button to automatically allow SOAP UI to declare the namespace. Upon clicking on declare button a 'POP up' with the message 'declare namespace from schema instead' will be displayed to the user. Click 'Yes' to proceed as shown below.

        Note: Upon hitting 'Declare button' you might end up with different URL's as namespace declaration, however, the actual web service location namespace is what would be considered for coding.

        Assertions in SoapUI: Complete Tutorial  

      2. For retrieving all the Supplier Number, We need to write an XPath Query and we will place it within < SupplierNumber> and </ SupplierNumber> Tags.
      3. Click 'Select from the Current' which will execute from the current response.
      4. Upon Clicking 'Select from the Current', All the Supplier Number are listed.
      5. Click 'Save'.
      // Namespace declaration
      declare namespace soap='http://schemas.xmlsoap.org/soap/envelope/';
      declare namespace ns1='http://www.webservicex.net/';
      declare namespace x = '';
      
      // Placing the result in Myresult Tags
      
      {
      // Iterating through all the supplier number 
      for $x in //ns1:GetSupplierByCityResponse/ns1:SupplierDataLists/ns1:SupplierDatas/ns1:SupplierData
      
      //Return all the Supplier number within ‘SupplierNumber’ Tags.
      return {data($x/ns1:SupplierNumber)}
      }
      
      

      Assertions in SoapUI: Complete Tutorial

      Step 17: XQuery Assertion is executed and displays the final result in the 'Assertion' Panel as shown below. Now we have successfully added an Xquery assertion using which we have validated all the supplier Number information. The same would be compared against the actuals, every time the request is sent to the webserver.

      Note: The actual values won't be displayed. If all actual values are same as that of the expected values, then it displays VALID else it will display 'Failed'.

      Assertions in SoapUI: Complete Tutorial

      Scripting Assertions

      This Assertion technique is the most widely used one as it is extremely difficult to manage and maintain hundreds of assertions.

      SOAP UI uses either Groovy Scripting or JavaScript for scripting assertions. The scripting technique is adopted for developing a framework for testing SOAP. Scripting assertions are used under following circumstances.

      • Scripting allows user to perform some operations before and after executing a TestCase using set up and tear down methods respectively. Set up is a procedure which is executed before executing a particular method(example – Object creation and Initialization) while tear down is a procedure which is executed after executing the method(eg: Destroying objects and clean up). This feature is not available in other Assertion types and can be done only through coding.
      • It allows users to perform opening/closing a Project, inorder to initialize or clean-up Project related settings and also to work with environmental variables which is very helpful during scripting.
      • It helps us in asserting a dynamic Response content.
      • Scripting assertions are used for creating user defined assertions that are NOT predefined by SOAP UI.

      For Demonstrating Script assertions, we will make use of Currency converter WSDL, the test case 'INR_to_USD' that we had created earlier.

      Step 1 : Steps to add groovy script is same as that of other assertions except that the assertion is not a predefined one. Instead it is a user defined assertion which offers greater flexibilities than the inbuilt ones.

      • Select the Test step against which the assertion has to be added.
       

      Assertions in SoapUI: Complete Tutorial

      • Click 'Add Assertion' Button as shown below.

      Assertions in SoapUI: Complete Tutorial
      Step 2 : Now select the Assertion category.

      1. In this case it is Script.
      2. Select Script Assertion and there no sub-types associated with it.
      3. Click 'Add'.

      Assertions in SoapUI: Complete Tutorial  

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

      Assertions in SoapUI: Complete Tutorial  

      Step 3 : Now let us write a groovy script to validate the Conversion Rate. The Script is attached below with the comments embedded. It is recommended to have knowledge on Java Script or Groovy Script before attempting to write your own script.

      //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["ns"] = "http://www.webserviceX.NET/"
      
      //Get the Value of the Node 'ConversionRateResult' and assign to a variable
      def conversionRate = holder.getNodeValue("//ns:ConversionRateResult")
      
      //print the value of the conversion rate in the Output pane
      log.info "The conversion value from USD to INR is " + conversionRate
      
      //Comparing the value to print 'Pass' or 'Fail'
      if(conversionRate=="63.7854")
      { 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.

      Note: The final Information pop up will always display with the message 'Script Assertion Passed' as long as the script is syntactically correct. It has got no correlation with your assertion within the script.

      Assertions in SoapUI: Complete Tutorial

      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.

      Assertions in SoapUI: Complete Tutorial  

      Step 5 : Now

      1. Select the Test Suite from the Navigator tree
      2. Click 'Run' Button
      3. Results would be displayed for the entire test suite.

      Assertions in SoapUI: Complete Tutorial  

      When To Use Inbuilt Assertion?

      • When a Response is short such that it can be validated using one of those inbuilt assertions.
      • We can also use Inbuilt Assertion if the response sent from the web server is always static in nature. If it is dynamic, we will not be able to assert it using inbuilt assertions.
      • When usage of inbuilt assertions such as Time out assertions and security assertions becomes inevitable.
      • Inbuilt Assertions holds good pretty well for one time usage where tests need not be repeated.
       

      Assertions Options:

      • The created assertions can be best controlled with the help of control panel that is highlighted below.
         

        Assertions in SoapUI: Complete Tutorial  

      • The created assertions allow testers to configure following things from the assertions tool box.

         

      Option

      Description

      Assertions in SoapUI: Complete Tutorial

      The selected Assertion moves up the order.

      Assertions in SoapUI: Complete Tutorial

      The selected Assertion moves down the order.

      Assertions in SoapUI: Complete Tutorial

      Removes the Selected Assertion

      Assertions in SoapUI: Complete Tutorial

      Re-configure/Edit the selected Assertion.

         
      • Below are the features available exclusively in PRO Version of SOAP UI. PRO version also helps us to Group assertions so that we can add one more layer of validation to the created assertions.
         

        AND: All assertions are evaluated as VALID assertion which will result in PASSED group condition.OR:  At least one of the assertions within the group must be VALID in order to assert a group PASSED condition.  

      • Pro Version also allows Cloning of Assertions:  This option lets testers to allow copying an assertion to a different test step in the same or a different project.
      • Disable/Enable Assertions: This option allows any grouped or ungrouped assertion to be disabled or enabled. If an assertion is disabled, it is grayed out and when a Test Case is executed, disabled assertions won't be executed.
      • Ungroup Assertions:  Any grouped assertions can be ungrouped if testers decide to do so.

      Complete List of Methods available in various assertion types

      Asserting Mechanism

      Description

      PROPERTY CONTENT

      Contains

      Searches for the existence of the specified string. It also supports regular expression.

      Not Contains

      Searches for the Non-existence of the specified string. It also supports regular expression.

      XPath Match

      Uses XPath expression to select the target node and its values.

      XQuery Match

      Uses an Xquery expression to select content from the target property.

      Compliance , Status, Standards

      HTTP Download all resource

      Validates the HTML Document after downloading and it holds good to any property containing HTML.

      Invalid HTTP Status Codes

      Verifies if the HTML response contains a status code that is not in the list of defined codes.

      Not SOAP Fault

      Verifies if the last received message is not a SOAP Fault. It is very obvious that it is applicable only for SOAP Test Steps.

      Schema Compliance

      Verifies if the last received message is compliant with the WSDL or WADL standard schema definition. Holds good for SOAP and REST Test Steps.

      SOAP Fault

      Verifies if the last received message is a SOAP Fault. It is the inverse of 'NOT SOAP' Fault Assertions.

      SOAP Response

      Verifies if the last received response is a valid SOAP Response and holds good for SOAP Test Request Steps only.

      Valid HTTP Status Codes

      Verifies if the HTML response contains a status code that is in the list of defined codes. It is the inverse of 'Invalid HTTP Status Codes' Assertion.

      WS-Addressing Request

      Verifies if the last received request contains appropriate WS-Addressing Headers.

      WS-Addressing Response

      Verifies if the last received response contains appropriate WS-Addressing Headers.

      WS-Security Status

      Validates if the last received message contains valid WS-Security headers and holds good only for SOAP Requests.

      Script

      Script Assertion

      Allows users to execute a custom script to perform user defined validations.

      SLA

      Response SLA

      Validates if the response time of the last received response was within the defined limit.

      JMS

      JMS Status

      Verifies if the JMS request of the Test Step has executed successfully and holds good for Test Steps with a JMS endpoint.

      JMS Timeout

      Verifies if that the JMS response of a test step did not took longer than the specified duration.

      Security

      Sensitive Information Exposure

      Verifies if the response message does not expose sensitive information about the target system. We can use this assertion for REST, SOAP and HTTP Test Steps.

       

      DOWNLOAD THE SOAPUI PROJECT CONTAINING ABOVE ASSERTIONS  

      Common Errors and Trouble Shooting :

      • Use the correct namespace. The Name space should be the URL where the web service is located.  
      • If an error thrown while developing a scripting assertion, use 'log.info' to print the contents of the variables
      •  
      • If you haven't got the desired output, verify if a valid input is passed in the request.

        For example, in currency converter, if you input the 'From currency' as 'XXX' which is not existing, the output throws a fault code as 'SOAP-Client' which means that the issue is with the parameter that is being passed from the client side.  

        Assertions in SoapUI: Complete Tutorial  

        Assertions in SoapUI: Complete Tutorial  

      •  
      • Ensure that you use the correct syntax while using XPATH and XQuery assertion. You should NOT use dot instead of colon while using the above assertion. The Syntax is //namespace:Tagname and NOT //namespace.tagname. By doing so, you might end up having a message that 'NO match in current response' even though the tag name is correct.  

        Assertions in SoapUI: Complete Tutorial

       

YOU MIGHT LIKE: