Search code examples
jsonrestgroovysoapuiassertions

SoapUI - Load Test -Assertion : Adding assertion to Load Test using SoapUI


I am new to SoapUI tool. I am using SoapUI version 5.3.0 My Application have a couple of RESTful APIs. I am sending multiple request to a WebService in the form of a json request as below:

{
   "app_key":"i8gAVDwcAq40n2kAv6Ox+w==",
   "targetDB":"${#TestCase#TARGET_DB}",
   "createNew": "true"
}

The response from the WebService is as follows:

<StartDataExtractResult xmlns="http://schemas.datacontract.org/2004/07/AriaTechCore" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
   <StatusCode>1</StatusCode>
   <StatusText>success</StatusText>
   <RequestNumber>101</RequestNumber>
</StartDataExtractResult>

I am using a Groovy Script to generate a dynamic name for "targetDB" as follows:

def targetdb = ((context.expand('${#TestCase#TARGET_DB}') ?: 100) as Integer) + 1
log.info "Target db for current request : ${targetdb}"
context.testCase.setPropertyValue('TARGET_DB', targetdb.toString())

I have designed my Test data in such a way that passing the name of the 'targetdb' as "101" in the Request will result in creating 101 tag in the response. The Load Test is executing fine.

Now I want to add some assertions to each of the Load Test Responses to check if the StatusCode tag contains "1", StatusText tag contains "success" & RequestNumber tag contains the value of the variable "${#TestCase#TARGET_DB}" (sent in Request json) . To achieve that I wrote a Script Assertion as follows:

    def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def holder = groovyUtils.getXmlHolder(messageExchange.responseContent)
holder.namespaces["ns1"] = "http://schemas.datacontract.org/2004/07/AriaTechCore"

def nodeStatusCode = holder.getNodeValue("//ns1:StatusCode")
assert nodeStatusCode != null
if(nodeStatusCode=="1")
{ log.info "Pass" }
else
{ log.info "Fail"}

def nodeStatusText = holder.getNodeValue("//ns1:StatusText")
assert nodeStatusText != null
if(nodeStatusText=="success")
{ log.info "Pass" }
else
{ log.info "Fail"}

def nodeRequestNumber = holder.getNodeValue("//ns1:RequestNumber")
assert nodeRequestNumber != null
if(nodeRequestNumber=="${TARGET_DB}")
{ log.info "Pass" }
else
{ log.info "Fail"}

But I am getting an error as:

No such Property: TARGET_DB for class: Script 53

Can anyone help me out please?

Here is the screenshot of my project: Assertion


Solution

  • I have got an answer for this query from another forum. Here is the answer to the question:

    1. We need to create a Step as Properties and add a property to that as "databaseName"
    2. We need to add another Groovy Script for the Property as follows:

      String testString = '${#TestCase#TARGET_DB}'

      testRunner.testCase.setPropertyValue( "databaseName", testString )

      def getLocalPropValue = testRunner.testCase.getPropertyValue("databaseName")

      log.info(getLocalPropValue)

      testRunner.testCase.testSteps["Properties"].setPropertyValue( "databaseName", testString )

    3. Finally we can have the Script Assertion as follows:

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

      def holder = groovyUtils.getXmlHolder(messageExchange.responseContent)

      holder.namespaces["ns1"] = "http://schemas.datacontract.org/2004/07/AriaTechCore"

      def nodeStatusCode = holder.getNodeValue("//ns1:StatusCode")

      assert nodeStatusCode != null

      if(nodeStatusCode=="1")

      { log.info "Pass" }

      else { log.info "Fail"}

      def nodeStatusText = holder.getNodeValue("//ns1:StatusText")

      assert nodeStatusText != null

      if(nodeStatusText=="success") { log.info "Pass" }

      else

      { log.info "Fail"}

      def nodeRequestNumber = holder.getNodeValue("//ns1:RequestNumber")

      assert nodeRequestNumber != null

      if (nodeRequestNumber == context.expand('${#TestCase#TARGET_DB}'))

      { log.info "Pass" }

      else

      { log.info "Fail"}

    @Rao thanks a lot for your valuable suggestions & comments.