aswani
aswani

Reputation: 15

soap ui dynamic value from request content

I have a SOAP request which has dynamic values generated by random method. How to capture these generated values to log?

My SOAP request:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:web="http://www.webserviceX.NET/">
   <soap:Header/>
   <soap:Body>
      <web:ChangeLengthUnit>
         <web:LengthValue>${=(int)(Math.random()*9999)}</web:LengthValue>
         <web:fromLengthUnit>Inches</web:fromLengthUnit>
         <web:toLengthUnit>Centimeters</web:toLengthUnit>
      </web:ChangeLengthUnit>
   </soap:Body>
</soap:Envelope>

Groovy script:

import com.eviware.soapui.support.GroovyUtils;
def prj = testRunner.testCase.testSuite.project.workspace.getProjectByName("Project1")
tCase = prj.testSuites['TestSuite'].testCases['TestCase']
tStep = tCase.getTestStepByName("ChangeLengthUnit")

def stepReq = tStep.getProperty("Request").getValue()

def runner = tStep.run(testRunner, context)
log.info ("runner status ....... : " + runner.hasResponse())
log.info stepReq
for( assertion in tStep.assertionList )
{
log.info "Assertion [" + assertion.label + "] has status [" + assertion.status + "]"
for( e in assertion.errors )
log.info "-> Error [" + e.message + "]"
}
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder( stepReq.toString() )

log.info holder.getNodeValues( "//web:LengthValue" ).toString()

Above groovy script is giving output as below:

Wed Oct 18 14:55:13 SGT 2017:INFO:[${=(int)(Math.random()*9999)}]

Actual value of LengthValue tag = 3490, How to get this value ?

Upvotes: 1

Views: 2315

Answers (3)

Rao
Rao

Reputation: 21389

It can easily read from RawRequest property using Script Assertion for the same request step.

assert context.rawRequest, 'Request is empty or null'

def xml = new XmlSlurper().parseText(context.rawRequest)
def actualValueInTheRequest = xml.'**'.find{it.name() == 'LengthValue'}?.text()
log.info "Value for LengthValue in the actual request is : $actualValueInTheRequest"

Upvotes: 4

Gaurav Khurana
Gaurav Khurana

Reputation: 3936

You are getting value from the actual request. Try to get the data from RawRequest

use below code

def holder = groovyUtils.getXmlHolder(mentionThenameofthestep#RawRequest)

The above code will point to Raw request of your teststep which contains actual value passed during execution instead of "${=(int)(Math.random()*9999)}"

Upvotes: 2

Chris Adams
Chris Adams

Reputation: 1454

I think what you're doing is getting the LengthValue from the request 'template' not what SoapUI has actually passed to the web service.

In your example, you have...

def stepReq = tStep.getProperty("Request").getValue()

The request is what you see in SoapUI that includes your vars, e.g. ${myVar}. I think what you're after is the 'raw request'.

In SoapUI, run the request and after it has ran, you should see a tab labelled 'Raw Request'. This is what SoapUI actually sends to the web service, in here you'll see that the vars have been evaluated and will show the value, which is what you are after.

I've taken your example and made some mods it. I haven't tested it, but it should help you.

import com.eviware.soapui.support.GroovyUtils;
def prj = testRunner.testCase.testSuite.project.workspace.getProjectByName("Project1")
tCase = prj.testSuites['TestSuite'].testCases['TestCase']
tStep = tCase.getTestStepByName("ChangeLengthUnit")

def stepReq = tStep.getProperty("Request").getValue()

def runner = tStep.run(testRunner, context)

// CHA MOD - Can only get raw request after running the step.
def rawRequest = context.expand( '${ChangeLengthUnit#RawRequest}' )
//CHA MOD - Quick look at the rawRequest...
log.info(rawRequest);


log.info ("runner status ....... : " + runner.hasResponse())
log.info stepReq
for( assertion in tStep.assertionList )
{
log.info "Assertion [" + assertion.label + "] has status [" + assertion.status + "]"
for( e in assertion.errors )
log.info "-> Error [" + e.message + "]"
}
def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder( stepReq.toString() )

log.info holder.getNodeValues( "//web:LengthValue" ).toString()

//CHA MOD - Get the length from the raw request...
holder = groovyUtils.getXmlHolder( rawRequest.toString() )
log.info holder.getNodeValues( "//web:LengthValue" ).toString()

Upvotes: 3

Related Questions