SOAPUI Square brackets around my actual results causing assert to fail - groovy

I am writing a Groovy script assertion which is verifying a value from a previous JDBC response step against a value contained in a SOAP response.
When I run my scripts I can see that both values are coming back the same but the actual result value (from the SOAP response) is surrounded by square brackets which in turn makes the assert fail. I'm guessing this is something to do with one being a string and one not?
How do I either strip out the square brackets from actual result or get them added to the expected result value to ensure the assert passes?
Below is my assert script.
Expected result is 001
Actual result is [001]
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder( messageExchange.responseContent )
def pxml = new XmlSlurper().parseText(context.response)
//grab the expected result from jdbc response
def expectedCodes = context.expand( '${JDBC Request#ResponseAsXml#//*:TW304_PRODHIST.PRODUCT_1}' )
//grab the actual result from the SOAP response
def actualCodes = pxml.'**'.findAll{it.name() == 'CurrHospProductCode'}*.text()
assert expectedCodes == actualCodes
log.info expectedCodes
log.info actualCodes

Because you are expecting a single value as expected where as you are getting an array with single element in it.
You can do it as below if that is right :
assert expectedCodes == actualCodes[0]
On a side note, you may have to check carefully are you really expecting single value only or if there is possible to get list of values.
EDIT: based on your script.
findAll gives you list as result. If you are expecting single element in the xml, then you can change it to find and then you actual code should work as it is.

Related

What is the right way to compare two strings in SoapUI with groovy script assertion?

I need to compare two strings in SoapUI. The first one is from a text file stored in my local directory, and the second one is from an XML response I get from a REST API operation. Before comparing the two strings, I use some methods on them to remove the header because they contain information like Dates and Processing Time which is sure to be different each time.
Below is what I've tried.
def xml = messageExchange.responseContentAsXml
String fileData = new File("C://Users/362784/project/outputPGB123.txt").text
String responseContent = new XmlSlurper().parseText(xml)
String fileDataFiltered = fileData.substring(fileData.indexOf("PASSED :"))
String responseContentFiltered = responseContent.substring(responseContent.indexOf("PASSED :"))
log.info(fileDataFiltered)
log.info(responseContentFiltered)
assert fileDataFiltered == responseContentFiltered
Here is the error I received
SoapUI error message
and my two identical log.info
log.info
Here's what the XML response looks like
I am new to SoapUI and I'm not sure what those two are actually comparing but I've checked the log.info of them on https://www.diffchecker.com/diff and the contents are identical. However, this assertion returns an error.
Can anyone point out what I did wrong and how do I get the result as passed?
In Java/Groovy you compare string values for equality like this:
assert fileDataFiltered.equals(responseContentFiltered)
See if that solves your problem.
The == comparator could, for example, compare object instances which could fail even if the text values are identical. See here for a more in-depth explanation.
EDIT:
Having seen your sample, it looks like the value you are comparing is inside XML character data (CDATA).
Consider the following example from here:
Some XML:
def response = '''<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:sam="http://www.example.org/sample/">
<soapenv:Header/>
<soapenv:Body>
<sam:searchResponse>
<sam:searchResponse>
<item><id>1234</id><description><![CDATA[<item><width>123</width><height>345</height><length>098</length><isle>A34</isle></item>]]></description><price>123</price>
</item>
</sam:searchResponse>
</sam:searchResponse>
</soapenv:Body>
</soapenv:Envelope>
'''
Then access the CDATA node with XmlSlurper:
def Envelope = new XmlSlurper().parseText(response)
def cdata = Envelope.Body.searchResponse.searchResponse.item.description
log.info cdata
log.info cdata.getClass()
assert cdata instanceof groovy.util.slurpersupport.NodeChildren
As you can see, the value returned is of object NodeChildren. You can convert it to a string with:
log.info cdata.toString()
log.info cdata.toString().getClass()
So let's do a comparison (as per cfrick's comment, you can use == or .equals())
def expectedCdata = '<item><width>123</width><height>345</height>length>098</length><isle>A34</isle></item>'
if (cdata.toString().equals(expectedCdata)) { log.info 'Equal' }
else {log.info 'Different'}
It still fails ???
Well, that is because of a residual newline that isn't obvious from printing with log.info, if you remove whitespace it works in this case:
if (cdata.toString().replaceAll("\\s","").equals(expectedCdata)) { log.info 'Equal' }
else {log.info 'Different'}
As you can see, there are many levels of possible failure. You have to work through it.

Identifying multiple values in SOAP UI response with Groovy

I was recently helped through a problem here where I wanted to identify multiple values from a SOAPUI response. This was successfully answered here.
I tried to take this same approach to a new problem I have. This time I would like to pick up 4 values from each section to use as my expected results.
Below is an example of my response that I want to run the assert expected results against:
<ns1:LimitAndUsageDetailsList>
<ns2:LimitAndUsageDetails xmlns:ns2="http://www.">
<ns2:LimitCode>022</ns2:LimitCode>
<ns2:LimitCodeDesc>APPLIANCES</ns2:LimitCodeDesc>
<ns2:LimitType>N</ns2:LimitType>
<ns2:LimitBenefit>500.0</ns2:LimitBenefit>
<ns2:LimitBenefitUsed>0.0</ns2:LimitBenefitUsed>
<ns2:LimitBenefitAvailable>500.0</ns2:LimitBenefitAvailable>
<ns2:LimitBenefitService>0</ns2:LimitBenefitService>
<ns2:LimitBenefitUsedService>0</ns2:LimitBenefitUsedService>
<ns2:LimitBenefitAvailableService>0</ns2:LimitBenefitAvailableService>
<ns2:QualifyingPeriodIndicator/>
<ns2:ClaimIndicator>B</ns2:ClaimIndicator>
<ns2:LimitPeriod>1</ns2:LimitPeriod>
<ns2:LimitPeriodType>C</ns2:LimitPeriodType>
<ns2:LimitScale>INDIV</ns2:LimitScale>
</ns2:LimitAndUsageDetails>
<ns2:LimitAndUsageDetails xmlns:ns2="http://www.">
<ns2:LimitCode>023</ns2:LimitCode>
<ns2:LimitCodeDesc>NEBULISER</ns2:LimitCodeDesc>
<ns2:LimitType>N</ns2:LimitType>
<ns2:LimitBenefit>0.0</ns2:LimitBenefit>
<ns2:LimitBenefitUsed>0.0</ns2:LimitBenefitUsed>
<ns2:LimitBenefitAvailable>0.0</ns2:LimitBenefitAvailable>
<ns2:LimitBenefitService>1</ns2:LimitBenefitService>
<ns2:LimitBenefitUsedService>0</ns2:LimitBenefitUsedService>
<ns2:LimitBenefitAvailableService>1</ns2:LimitBenefitAvailableService>
<ns2:QualifyingPeriodIndicator/>
<ns2:ClaimIndicator>B</ns2:ClaimIndicator>
<ns2:LimitPeriod>3</ns2:LimitPeriod>
<ns2:LimitPeriodType>R</ns2:LimitPeriodType>
<ns2:LimitScale>INDIV</ns2:LimitScale>
</ns2:LimitAndUsageDetails>
And I am interested in LimitCode, LimitType, LimitPeriod and LimitPeriodType elements.
I have tried the following assert script but it doesnt work.
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder( messageExchange.responseContent )
def pxml = new XmlSlurper().parseText(context.response)
def expected = [
"022":"N":"1":"C"
'023':'N':'3':'R',
'030':'N':'1':'C',
]
def xml = new XmlSlurper().parseText(context.response)
def actual = xml.'**'.findAll{it.name() == 'LimitAndUsageDetails'}.collectEntries{[(it.LimitCode.text()): it.LimitType.text(): it.LimitPeriod.text(): it.LimitPeriodType.text()]}
assert expected == actual
Here is what you need to do.
Expected data requires couple more items as opposed to previous question.
So need to create map(to identify which data to what element)
Since there are list of details to be verified, expected data should be list of maps.
Please see the Script Assertion below:
assert context.response, 'Response is empty or null'
//list of maps; each map for single usage detail
//If the details are more, you may use a csv data file; solution may vary slightly
def expected = [
[ LimitCode: '022', LimitType: 'N', LimitPeriod: 1, LimitPeriodType: 'C'],
[ LimitCode: '023', LimitType: 'N', LimitPeriod: 3, LimitPeriodType: 'R']
]
 
 
def xml = new XmlSlurper().parseText(context.respone)
//Build the actual list of map from response
def actual = xml.'**'.findAll{it.name() == 'LimitAndUsageDetails'}.collect{ [
LimitCode : it.LimitCode.text(),
LimitType : it.LimitType.text(),
LimitPeriod : it.LimitPeriod.text() as Integer,
LimitPeriodType : it.LimitPeriodType.text()
]
}.sort {it.LimitCode}
assert expected == actual
You may quickly try the same online demo
You could use the below code which is also to get Response node values in
def groovyutils=new com.eviware.soapui.support.GroovyUtils(context)
def holder=groovyutils.getXmlHolder(messageExchange.responseContent)
holder.namespaces["ns"]="http://www.webserviceX.NET/"
def conversionRate=holder.getNodeValue("//ns:ConversionRateResult")
i was not able to test on your code since i dont have the correct namespace, but this code works perfectly fine for getting a value from response of currency convertor
you just need to change xpath inside getNodeValue and the namespace URL. this way its simpler

How to pick up child element of specific parent element in Script Assertion?

I have the following XML example which shows there are multiple occurrences of node 'ProductCode' which fall under both nodes 'PrevHospProduct' and also under 'PrevExtrasProducts'.
<ns2:PrevHospProducts>
<ns2:PrevHospProduct>
<ns2:ProductCode>D00</ns2:ProductCode>
<ns2:ExcessPaid>Yes</ns2:ExcessPaid>
</ns2:PrevHospProduct>
<ns2:PrevHospProduct>
<ns2:ProductCode>900</ns2:ProductCode>
</ns2:PrevHospProduct>
</ns2:PrevHospProducts>
<ns2:PrevExtrasProducts>
<ns2:PrevExtraProduct>
<ns2:ProductCode>00A</ns2:ProductCode>
</ns2:PrevExtraProduct>
</ns2:PrevExtrasProducts>
For this test I am only interested in the values in 'ProductCode' which are a child of 'PrevHospProduct'. I am not interested in any of the values under 'PrevExtrasProducts'.
I have the following Groovy Script Assertion in SoapUI to pick up values in 'ProductCode' but the test is failing as the actual results are also returning 'D00', '900' and '00A' from the examples response. I only want the expected results to pick values 'D00', '900'.
def expectedCodes = ['D00','900']
def pxml = new XmlSlurper().parseText(context.response)
def actualCodes = pxml.'**'.findAll{it.name() == 'ProductCode' }*.text() as List
assert expectedCodes.sort() == actualCodes.sort()
First need to find the parent node i.e., PrevHospProduct and then get the ProductCode.
Here is the script assertion:
def expectedCodes = ['D00','900']
def pxml = new XmlSlurper().parseText(context.response)
def actualCodes = pxml.'**'.findAll{it.name() == 'PrevHospProduct'}*.ProductCode*.text() as List
log.info actualCodes
assert expectedCodes.sort() == actualCodes.sort()

How to assert array values in groovy using script assertion

I am writing a script assertion to see if a element value contains in the array list and if it does, it passes.
When I print the element:Number, I get like an example [1,2,3,3] as array. If Number contains say 3, script has to pass.
I have written below code which is failing, probably because the written value is an array list, how to assert an array value?
def response = messageExchange.getResponseContent()
def xml = new XmlSlurper().parseText(response)
def invoiceNumber= xml.'**'.findAll { it.name() == 'Number'}
log.info "$invoiceNumber"
assert invoiceNumber.contains(1)
The problem is that invoiceNumber is a Collection of groovy.util.slurpersupport.NodeChild elements instead of Integer elements. This is why the contains(3) comparison never returns true.
You've to convert array of groovy.util.slurpersupport.NodeChild to array of integers before the contains(), you can do it using the spread dot operator an NodeChild.toInteger(), so your script must be:
def response = messageExchange.getResponseContent()
def xml = new XmlSlurper().parseText(response)
def invoiceNumber= xml.'**'.findAll { it.name() == 'Number'}
log.info "$invoiceNumber"
// convert the array to array of integers
invoiceNumber = invoiceNumber*.toInteger()
// now you can perform the assert correctly
assert invoiceNumber .contains(3)
Hope it helps,

SoapUI, temper request data break when value is read from property file

I have a groovy script as the first test step inside a test case, part of it looks like:
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def holder = groovyUtils.getXmlHolder("SampleTestt#Request").getXml()
log.info holder
When SampleTest test step has all element values hardcoded, the request xml can be printed fine.
However if some of the request values is read from a test case property, like the following for example
${#TestCase#Id}
The the above groovy script through error as:
org.apache.xmlbeans.XMLException: error: Unexpected character encountered : '$'
Can you please help?
Thanks.
You can use context.expand() to evaluate the properties inside your request and then parse the result to xmlHolder, your code could looks like:
// get your request replacing the properties inside by their values
def xmlRequest = context.expand('${SampleTestt#Request}')
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def holder = groovyUtils.getXmlHolder(xmlRequest)
log.info holder.getXml()
Note that I use SampleTestt as your test step request name, but I think that the last t could be a typo... check if it's the correct request name before use the code.
Hope this helps,

Resources