2
votes

New to Groovy and a bit of a novice with coding overall but I'm trying so please bear with me. Apologies in advance if I don't but enough detail as to what I'm trying to achieve here!

I am writing a scripted test in SOAPUI for a SOAP call that has multiple instances of the same named node ("//ns2:AddOnCode"). I have expected results of what values I am expecting to return in the instances of this node.

So far I have created the following which works in terms of it only passes if all of these values are present. But I'd like to take this one step further and get the test to fail if this node is returned with a value that I havent defined.

So if say all these 5 values returned but also a 6th //ns2:AddOnCode value came with say '999' then I'd like this test to fail.

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder( messageExchange.responseContent )

assert holder["//ns2:AddOnCode"].contains('029')
assert holder["//ns2:AddOnCode"].contains('030')
assert holder["//ns2:AddOnCode"].contains('040')
assert holder["//ns2:AddOnCode"].contains('083')
assert holder["//ns2:AddOnCode"].contains('105')

Below is my response. You can see there are various instances of AddOnCode and these are what I am intereted in testing. My asserts above succesfully pass as all five of these are present in the response but I need to add a further step to look for any other AddOnCode values that are not in my list of assert values.

                 <ns2:AddOnService>
                    <ns2:AddOnCode>029</ns2:AddOnCode>
                    <ns2:AddOnDesc>OVERPAYMENT</ns2:AddOnDesc>
                 </ns2:AddOnService>
                 <ns2:AddOnService>
                    <ns2:AddOnCode>030</ns2:AddOnCode>
                    <ns2:AddOnDesc>REWARDS POOL</ns2:AddOnDesc>
                 </ns2:AddOnService>
                 <ns2:AddOnService>
                    <ns2:AddOnCode>040</ns2:AddOnCode>
                    <ns2:AddOnDesc>NON URGENT</ns2:AddOnDesc>
                 </ns2:AddOnService>
                 <ns2:AddOnService>
                    <ns2:AddOnCode>083</ns2:AddOnCode>
                    <ns2:AddOnDesc>EXGRATIA</ns2:AddOnDesc>
                 </ns2:AddOnService>
                 <ns2:AddOnService>
                    <ns2:AddOnCode>105</ns2:AddOnCode>
                    <ns2:AddOnDesc>MISC NON CLAIMABLE</ns2:AddOnDesc>
                 </ns2:AddOnService>
              </ns2:AddOnServices>
1
are you talking about not operator? like this: assert ! holder["//ns2:AddOnCode"].contains('999') - daggett
Thanks for the reply. I am aware of the not operator. But I need to apply a not operator if none of my already scripted codes are brought back. Your example would be good to tell me if 999 were to arrive in error but this node could be filled with any value, not just 999. If any value were to return other than the 5 I am expecting then I want the test to fail. - Matt

1 Answers

1
votes

You are not using correctly.
Change from:

assert holder["//ns2:AddOnCode"].contains('029')

To:

assert holder.getNodeValue("//ns2:AddOnCode").contains('029')

EDIT: Based on OP comments, there are repeating nodes.

Since OP is using Script Assertion, below is the script which needs expectedCodes as input and retrieves the actualCodes and compares both and fails if not matches.

//Please edit if needed
def expectedCodes = ['029', '030', '040', '083', '105']
def pxml = new XmlSlurper().parseText(context.response)
def actualCodes = pxml.'**'.findAll{it.name() == 'AddOnCode' }*.text()
assert expectedCodes.sort() == actualCodes.sort()

NOTE: Matthew, the above is independent code for Script Assertion and does not require your existing code.