Abhishek Abhishek - 2 months ago 14
Groovy Question

How to add multiple assertions in soapUI

I want to add multiple assertions in soapUI, like if in response:

<ns0:Message>A</ns0:Message>


is returned. I want to add, "A", "B", "C" as assertions, so that if any of the values is returned, the assertion could Pass. Thanks!

<soapenv:Body>
<ns0:Fault xmlns:ns1="http://www.w3.org/2003/05/soap-envelope" xmlns:ns0="http://schemas.xmlsoap.org/soap/envelope/">
<faultcode>OSB-382500</faultcode>
<faultstring>Mandatory Parameter Customer Type cannot be empty (uuid: 1f8b9637-11b1-47ea-9ebd-3abf2fda950e)</faultstring>
<detail>
<ns0:Fault xmlns:ns0="http://group.vodafone.com/contract/vfo/fault/v1" xmlns:ns2="http://group.vodafone.com/contract/vho/header/v1" xmlns:ns3="http://group.vodafone.com/schema/common/v1" xmlns:ns6="http://docs.oasis-open.org/wsrf/bf-2" xmlns:ns7="http://www.w3.org/2005/08/addressing">
<ns6:Timestamp>2017-08-16T20:44:27.15+05:30</ns6:Timestamp>
<ns6:ErrorCode>500</ns6:ErrorCode>
<ns0:Name/>
<ns0:Severity>Critical</ns0:Severity>
<ns0:Category>Technical</ns0:Category>
<ns0:ReasonCode>ReasonCode</ns0:ReasonCode>
<ns0:Message>A</ns0:Message>
</ns0:Fault>
</detail>
</ns0:Fault>
</soapenv:Body>

Rao Rao
Answer Source

Taking a sample xml, since you haven't mentioned any data.

In SoapUI, you can use below Script Assertion:

assert context.response, 'Response is empty or null'  

//Define or change for the assertion
def validValues = ['Tag': ['A', 'B', 'C']]
def tagToFind = 'Tag'

def pxml = new XmlSlurper().parseText(context.response)

//Find all the tag values and filter those are not in valid values
def result = pxml.'**'.findAll{it.name() == tagToFind &&  !(it.text() in validValues[tagToFind])}
assert !result, "Elements ${tagToFind} have different values other than valid- ${result}"

You can find the online demo quickly with sample xml.

Note that, the sample xml shows assertion error as it has other values than expected.