如何读取txt文件,获取数据并使用Groovy将其作为变量存储在SOAPUI的自定义属性中?

时间:2014-11-27 15:52:53

标签: groovy soapui

我本周开始使用Soapui,我做了一些测试,例如发送请求POST并将响应保存为文件夹中的txt文件。 我要做的是读取此txt文件,复制特定数据并将其存储在自定义属性中。 因为我想在嵌套请求POST中使用此对象,这取决于第一个请求。

我想在Groovy中做到这一点。 我只有开源SOAPUI版本5.0.0

谢谢

1 个答案:

答案 0 :(得分:1)

您必须在测试用例中添加groovy测试步骤,并像在java中一样,检查groovy documentation

仅作为引用,SOAPUI 5.2.0具有groovy 2.1.7版本(check the dependency in pom.xml),因此在{000}运行在SOAPUI上的脚本中,您可以使用{标准包含在groovy中{1}},SOAPUI类,其他一些groovy 2.1.7 API,另外您可以在jre中包含其他jar,以便在SOAPUI\bin\ext脚本中使用它们。

最后,您要求从文件中读取一些数据并将其写入自定义属性,例如,您可以按照以下方式执行此操作:

groovy

由于您的问题不明确,我向您展示了一个可能的示例,展示了如何读取文件以查找特定内容并将此内容保存在testCase中的自定义属性中。

请注意,在// read the file from path def file = new File('/path/yourFile') // for example read line by line def yourData = file.eachLine { line -> // check if the line contains your data if(line.contains('mySpecifiyData=')){ return line } } // put the line in a custom property in the testCase testRunner.testCase.setPropertyValue('yourProp',yourData) 脚本中,您可以使用全局对象:groovytestRunnercontext,在此示例中我使用log要访问testRunner及其属性,就像您可以认为testCase访问testRunnertestSuitesproject等一样...查看文档:

  1. http://www.soapui.org/Scripting-Properties/tips-a-tricks.html
  2. http://www.soapui.org/Functional-Testing/working-with-scripts.html
  3. 希望这有帮助,