【问题标题】:Groovy script to Read an xml file and update next step request with file contents用于读取 xml 文件并使用文件内容更新下一步请求的 Groovy 脚本
【发布时间】:2016-11-07 17:41:23
【问题描述】:

需求:从文件夹中读取xml文件并将文件内容传递给Soap请求。

问题 我正在尝试使用 groovy 脚本读取保存在文件夹中的文件,但无法读取文件的内容。我在尝试打印 xml 文件的内容时遇到空指针异常。

def fileList = []
new File("C:\\Users\\Documents\\Groovy Scripts\\requests").eachFile
{ f ->
if (f.isFile()&& f.name.endsWith('.xml'))
{
 def filename = f.name[0..-5]
 fileList.add(filename)
 log.info filename

 }
}
if (fileList.size() <1)
{
testRunner.fail("No request files found")
}
context.put('fileList', fileList)

def f = new File("C:\\Users\\Documents\\Groovy Scripts\\requests\\${context.fileList}.last().text")
log.info f

更新基于 cmets,添加到问题中。

我的测试用例包含 3 个步骤。第 1 步:从文件夹中读取 xml 文件。第二步:使用xml文件内容作为soap请求输入。步骤 3:将步骤 2 的响应保存为 xml。

【问题讨论】:

  • 你在做data-driven 测试吗?你能展示一下你的测试用例的结构吗?
  • 我的测试用例包含 3 个步骤。第 1 步:从文件夹中读取 xml 文件。第二步:使用xml文件内容作为soap请求输入。步骤 3:将步骤 2 的响应保存为 xml。
  • 不确定您是否理解前面的问题。因为你没有回复重点。无论如何,文件夹中有多少个文件?
  • 文件夹中的文件大小不同...根据我的要求和我发布的代码,我知道我有点错误...请更正我。

标签: xml soap groovy soapui


【解决方案1】:

据了解,您需要进行数据驱动测试,请求保存在一个目录中。

以前,here 提供了一种方法来循环遍历数据并保存响应。

您现在可能需要的所有更改都在第一步中 - 它读取目录,循环遍历您的文件并将文件内容设置为请求并运行soap请求步骤。

步骤 1 的 Groovy 脚本:

import groovy.io.FileType

//change your input directory name below
def dir = new File('path/to/input/dir')
dir.eachFile (FileType.FILES) { file ->  

   //Get the step
   def step = context.testCase.getTestStepAt(1)
   //Set the file content as test step request
   step.testRequest.requestContent = file.text
   log.info "New request is set for step2 : ${request}"
   //Run the step2
   step.run(testRunner, context)
}
//By now all the orders got executed, now need to exit the step without additionally running step2
//So, jump to step2, index is 2
testRunner.gotoStep(2)

您可以继续使用上面提供的链接中提到的其余步骤。

【讨论】:

  • 很高兴知道它很有帮助。如果您愿意,您可以投票赞成对您有帮助的答案。
  • 我正在尝试为我的自动化添加更多功能。在这里,我试图在 excel 表中添加字段。如何只记录最后一个文件响应。你能帮我写下所有文件响应的响应吗?注意:每个文件响应必须在新行中
  • code import jxl.* import jxl.write.* def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context ) def holder = groovyUtils.getXmlHolder("Step2#Response") def request=groovyUtils.getXmlHolder("Step2#Request") WritableWorkbook workbook = Workbook.createWorkbook(new File("C:\\Users\\Documents\\ Groovy Scripts\\response\\output.xls")) WritableSheet sheet = workbook.createSheet("工作表 1", 0) code
  • xPath1 = "//*:description/text()" xPath2 = "//*:commonOrderId/text()" xPath3 = "//*:m/text()" def row= 1 标签 orderid = new Label(0,row ,request.getNodeValue(xPath2)); sheet.addCell(orderid);标签模式 = new Label(1,row ,request.getNodeValue(xPath3)); sheet.addCell(m);标签描述 = new Label(2,row , holder.getNodeValue(xPath1)); sheet.addCell(描述);标签响应 = new Label(3, row, context.expand('${Step2#Response}')); sheet.addCell(响应);行=行+1 workbook.write(); workbook.close();
  • ,我在第 2 步中使用了上述代码。我相信每次创建新的 Excel 表都会产生问题。请帮忙
猜你喜欢
  • 2013-05-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-30
  • 1970-01-01
  • 1970-01-01
  • 2013-07-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多