Forum Discussion

debanjan's avatar
debanjan
Occasional Contributor
8 years ago
Solved

How can I assert a json parameter in Request with a XML response through Script Assertion in Soap UI

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 parameter of the 'targetdb' as "101" will result in the RequestNumber tag set to "101" in the response.

 

Now I want to add an assertion to check if the RequestNumber tag contains the same value as 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 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 any one help me out please?

  • You need to use context.expand to do property expansions in scripts:

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

    or you can access test case properties as follows:

    context.testCase.getPropertyValue("TARGET_DB")
    context.testCase.properties["TARGET_DB"].value

     

    Alternatively it might be easier to use the XPath Match assertion instead of the Script assertion:

    XPath expression: //*:RequestNumber

    Expected result: ${#TestCase#TARGET_DB}

  • HKosova's avatar
    HKosova
    8 years ago

    @debanjan, these expressions should be used in place of "${TARGET_DB}", that is:

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

    or

    if (nodeRequestNumber == context.testCase.getPropertyValue("TARGET_DB"))

    or

    if (nodeRequestNumber == context.testCase.properties["TARGET_DB"].value)

6 Replies

    • debanjan's avatar
      debanjan
      Occasional Contributor

      Hi MaximStanovov Thanks for your suggestion. As you suggested. I have done the following:

       

      1. Added a TestStep by the name Property List and a Property "databaseName" with in.

      2. Added a Groovy Script by the name "Groovy Property" and added the following code:

       

      String testString = ${TARGET_DB}
      testRunner.testCase.setPropertyValue( "databaseName", testString )
      def getLocalPropValue = testRunner.testCase.getPropertyValue("databaseName")
      log.info(getLocalPropValue)

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

       

      I have observed when I pass a simple string initially e.g "abcde" the Groovy sets the value of Property "databaseName" to "abcde". But when ever I pass the following it gives me an error as follows:

       

      1. '${#TestCase#TARGET_DB}'   Then I can see that the Groovy gives me a similar output as ${#TestCase#TARGET_DB}

       

      Even now I can see the "databaseName" property is being set to ${#TestCase#TARGET_DB}

       

      Now, I have modified my Script Assertion as follows:

      context.testCase.getPropertyValue("TARGET_DB")
      context.testCase.properties["TARGET_DB"].value
      if(nodeRequestNumber=='${TARGET_DB}')
      { log.info "Pass" }
      else
      { log.info "Fail"}

       

       

      The result I am getting is still Fail. As the Assertion is in a for loop so I am not getting any error though but log message says Fail.

       

      Any more suggestion please? 

       

       

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    You need to use context.expand to do property expansions in scripts:

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

    or you can access test case properties as follows:

    context.testCase.getPropertyValue("TARGET_DB")
    context.testCase.properties["TARGET_DB"].value

     

    Alternatively it might be easier to use the XPath Match assertion instead of the Script assertion:

    XPath expression: //*:RequestNumber

    Expected result: ${#TestCase#TARGET_DB}

    • debanjan's avatar
      debanjan
      Occasional Contributor

      HKosova Thanks for your suggestion. As you mentioned I did the following:

       

      Case A:

      Added this line to my script before comparison : context.expand('${#TestCase#TARGET_DB}')

       

      But still the error comes as: No such property: TARGET_DB for class: Script8

       

      Case B: 

      Added these 2 lines to my script before comparison :

      context.testCase.getPropertyValue("TARGET_DB")
      context.testCase.properties["TARGET_DB"].value

       

      But still the error comes as: No such property: TARGET_DB for class: Script9

       

      Case C:

      XPath Match assertion

       

      Yes, it worked. 

      A big thanks to you again.

       

      But I will always like to know, why  the variable was not accessible from Script Assertion. My other assertions are still through Script Assertion.

      • HKosova's avatar
        HKosova
        SmartBear Alumni (Retired)

        @debanjan, these expressions should be used in place of "${TARGET_DB}", that is:

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

        or

        if (nodeRequestNumber == context.testCase.getPropertyValue("TARGET_DB"))

        or

        if (nodeRequestNumber == context.testCase.properties["TARGET_DB"].value)