Groovy 脚本读取 xml 文件并使用文件内容更新下一步请求

2023-12-06

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

Issue我试图使用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

Update根据评论,添加到问题中。

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


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

之前提供了一个方法here循环遍历数据并保存响应。

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

Step1 的 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)

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Groovy 脚本读取 xml 文件并使用文件内容更新下一步请求 的相关文章

随机推荐