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?
- Looks like you need to get property before use it in the assertion.
https://www.soapui.org/functional-testing/properties/working-with-properties.html 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"].valueAlternatively it might be easier to use the XPath Match assertion instead of the Script assertion:
XPath expression: //*:RequestNumber
Expected result: ${#TestCase#TARGET_DB}
@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)