使用soapUI中的Groovy teststep将请求/响应文件存储在本地目录中

时间:2011-06-14 11:04:06

标签: groovy soapui

通过soapUI中的groovy teststep,我希望所有请求和响应文件都存储在具有系统日期的本地目录中。

soapUI中的groovy测试步骤:

def name = context.expand( '${Input#TG}' )

def locatie = context.expand( '${#TestCase#locatie}' )

def createFolder() {
  date = new Date()
  dateFormat = new java.text.SimpleDateFormat('ddMMyyyy')
  shortDate = dateFormat.format(date)
  outputFolder = locatie+shortDate
  createFolder = new File(outputFolder)
  createFolder.mkdir()  
}

def getResponseFilename(name) {
  respFilename = createFolder()+"_"+name+"_response.xml"
}

def getRequestFilename(locatie,name) {
  reqFilename = createFolder()+"_"+ name+"_request.xml"
}

def file = new PrintWriter (getResponseFilename(name))

def response = testRunner.testCase.testSteps
["CheckAdres"].testRequest.response.contentAsString

file.println(response)
file.flush()
file.close()

def file2 = new PrintWriter (getRequestFilename(name))
def request = context.expand('${CheckAdres#Request}')

file2.println(request)
file2.flush()
file2.close()

我收到以下错误:

Tue Jun 14 12:47:24 CEST 2011:**ERROR:groovy.lang.MissingPropertyException: No such property: locatie for class: Script78**
groovy.lang.MissingPropertyException: No such property: locatie for class: Script78
  at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:49)
  at org.codehaus.groovy.runtime.callsite.PogoGetPropertySite.getProperty(PogoGetPropertySite.java:49)
  at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callGroovyObjectGetProperty(AbstractCallSite.java:241)
  at Script78.createFolder(Script78.groovy:8)
  at Script78$createFolder.callCurrent(Unknown Source)
  at Script78.getResponseFilename(Script78.groovy:14)
  at Script78$getResponseFilename.callCurrent(Unknown Source)
  at Script78.run(Script78.groovy:21)
  at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.run(SoapUIGroovyScriptEngine.java:93)
  at com.eviware.soapui.support.scripting.groovy.SoapUIProGroovyScriptEngineFactory$SoapUIProGroovyScriptEngine.run(SourceFile:51)
  at com.eviware.soapui.impl.wsdl.teststeps.WsdlGroovyScriptTestStep.run(WsdlGroovyScriptTestStep.java:148)
  at com.eviware.soapui.impl.wsdl.panels.teststeps.GroovyScriptStepDesktopPanel$RunAction$1.run(GroovyScriptStepDesktopPanel.java:274)
  at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
  at java.lang.Thread.run(Unknown Source)

5 个答案:

答案 0 :(得分:10)

有几种方法可以做到这一点。一个是使用以下脚本创建Groovy测试步骤:

def myOutFile = "C:/Temp/MyOutDir/response.xml"
def response = context.expand( '${MyTestRequest#Response}' )
def f = new File(myOutFile)
f.write(response, "UTF-8")

答案 1 :(得分:2)

尝试使用SoapUI的工具来选择您想要的任何值。 右键单击groovy编辑区域,选择Get Data - >你的测试套件 - >你的测试用例 - >你的测试步骤 - > response。这将为您提供整个回复。您还可以使用此方法深入了解response

答案 2 :(得分:1)

查看麦当劳的答案。保存和拍摄请求的最佳方式。

http://www.loadui.org/forum/viewtopic.php?f=5&t=16354#p38935

答案 3 :(得分:1)

语法较短。逻辑与@robert Shared

类似
def response=context.expand('${TestRequest#RawRequest}')
new File("c:/testpath/input.xml").write(response)

如果你想将它减少到一行

 new File("c:/testpath/input.xml").write(context.expand('${TestRequest#RawRequest}')

您可以将 RawRequest 替换为您要保存的内容

  

请求或回复

当您希望数据替换请求中使用的变量

时,使用

RawRequest

答案 4 :(得分:0)

如果我们应该在保存错误,那就更有用了

import com.eviware.soapui.support.XmlHolder
import java.text.MessageFormat
import org.apache.commons.lang.ObjectUtils

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def retrieve = groovyUtils.getXmlHolder("MyTestRequest#Response" )

if (!ObjectUtils.equals(retrieve.getNodeValue("//*:xpath"), "string")){
 def currentTime = System.currentTimeMillis()
 def fullFilePath = context.expand('${projectDir}') + File.separator + "Fail-"+currentTime+".xml"
 def reportFile = new File(fullFilePath)
 if (!reportFile.exists())
 {
  reportFile.createNewFile()    
  reportFile.append((Object)retrieve.getPrettyXml(), 'UTF-8')
 }
}