Sarvesh Sarvesh - 1 month ago 34
Groovy Question

Assert a SoapUI test case with reason of failure

I have a test case on which I want to put the assertion.

I need to provide a reason for failure in assertion.

My output from XML format is as follow:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<soapenv:Fault>
<faultcode>soapenv:Server</faultcode>
<faultstring>XYZ-001: input is wrong</faultstring>
<detail>
<con:fault xmlns:con="http://www.bea.com/wli/sb/context">
<con:errorCode>XYZ-001</con:errorCode>
<con:reason>input is wrong</con:reason>
<con:location>
<con:node>PipelinePairNode1</con:node>
<con:pipeline>PipelinePairNode1_response</con:pipeline>
<con:stage>stage1</con:stage>
<con:path>response-pipeline</con:path>
</con:location>
</con:fault>
</detail>
</soapenv:Fault>
</soapenv:Body>
</soapenv:Envelope>


My desired result should be the faultstring node of the xml.

For this I have tried with xpath assertion using this code:

declare namespace soapenv='http://schemas.xmlsoap.org/soap/envelope/';
declare namespace con='http://www.bea.com/wli/sb/context';
boolean('/soapenv:Envelope/soapenv:Body/soapenv:Fault/')


and I put true as expected output.
After generating JUnit report it was giving some other reason:

Cancelling due to failed test step

<h3><b>Failure Failed</b></h3><pre>[XPath Match] XPathContains comparison failed for path [declare namespace soapenv='http://schemas.xmlsoap.org/soap/envelope/';
declare namespace con='http://www.bea.com/wli/sb/context';
boolean('/soapenv:Envelope/soapenv:Body/soapenv:Fault/')], expecting [false], actual was [true]
</pre><hr/>


I then proceed with Groovy using following script:

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def requsetHolder = groovyUtils.getXmlHolder( messageExchange.requestContent )
def responseHolder = groovyUtils.getXmlHolder( messageExchange.responseContent )
def refNum = responseHolder.getNodeValue("soapenv:Envelope/soapenv:Body/soapenv:Fault/")
def testrunner = context.getTestRunner();
if (refNum != null){
testrunner.fail("soapenv:Envelope/soapenv:Body/soapenv:Fault/faultstring")
}


but no luck this time time also.
junit failure reason was:

Cancelling due to failed test step

<h3><b>Failure Failed</b></h3><pre>[Script Assertion] net.sf.saxon.trans.XPathException: XPath syntax error at char 46 on line 2 in {...pe/soapenv:Body/soapenv:Fau...}:
Unexpected token "<eof>" in path expression
</pre><hr/>


so is there any way by which i can generate my custom reason in junit output using assertion in either groovy or xpath.

Rao Rao
Answer

Based on your question & comments, here is the Script Assertion.

  • The script contains different ways how to show the customized message in the report.
  • Please follow the in-line comments for details.
  • Sample code snippet added for additional check for specific errorCode element value if the response is a Fault. You can apply it for other elements too.

Script Assertion:

/**
 * The below script should be used as Script Assertion
 * which checks if the response contains Fault, raise error otherwise
 * Once it has fault in it, then it checks for the specific "errorCode", raise error with
 * customized message
 */

//Get the response parsed
def envelope = new XmlSlurper().parseText(context.response)

//There are three approaches to check & and throw customized error message
// if the response does not have Fault. Use one of them
assert envelope.Body.Fault, "Response does not have soap fault"
assert !envelope.Body.Fault.isEmpty(), "Response does not have soap fault"
if (!envelope.Body.Fault) { throw new Error ("Response does not have soap fault") }

//Further check for specific errorCode in the soap fault
def expectedErrorCode = 'XYZ-001'
def actualErrorCode = envelope.'**'.find {it.name() == 'errorCode' } as String

log.info "Actual code is : $actualErrorCode"
assert expectedErrorCode == actualErrorCode, "Soap fault should not have \"${expectedErrorCode}\""

You may quickly test it from here directly to see how it behaves if errorCode does not match.

Comments