soap ui来自请求内容的动态值

时间:2017-10-18 07:01:46

标签: groovy soapui

我有一个SOAP请求,其中包含随机方法生成的动态值。如何捕获这些生成的值来记录?

我的SOAP请求:

<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脚本:

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()

上面的groovy脚本输出如下:

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

LengthValue标签的实际值= 3490,如何获取此值?

3 个答案:

答案 0 :(得分:3)

我认为您正在做的是从请求模板中获取LengthValue&#39;而不是SoapUI实际传递给Web服务的东西。

在你的例子中,你有......

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

该请求就是您在SoapUI中看到的包含您的变量的请求,例如$ {myVar的}。我认为您所追求的是原始请求&#39;。

在SoapUI中,运行请求并在运行后,您应该会看到一个标记为&#39; Raw Request&#39;的标签。这就是SoapUI实际发送给Web服务的内容,在这里您将看到vars已经过评估并将显示该值,这就是您所追求的。

我已经采取了你的榜样,并做了一些改造。我还没有对它进行测试,但它应该对你有帮助。

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()

答案 1 :(得分:3)

对于相同的请求步骤,可以使用RawRequestScript Assertion属性轻松读取。

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"

答案 2 :(得分:2)

您从实际请求中获得了价值。尝试从RawRequest获取数据

使用下面的代码

def holder = groovyUtils.getXmlHolder(mentionThenameofthestep#RawRequest)

上面的代码将指向您的teststep的Raw请求,其中包含执行期间传递的实际值,而不是“$ {=(int)(Math.random()* 9999)}”