Groovy 脚本读取 xml 文件并使用文件内容更新下一步请求
Groovy script to Read an xml file and update next step request with file contents
要求:从文件夹中读取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
根据评论更新,添加到问题中。
我的测试用例包含 3 个步骤。第 1 步:从文件夹中读取 xml 文件。第 2 步:使用 xml 文件内容作为 soap 请求输入。第 3 步:在输出文件夹中将第 2 步的响应保存为 xml.
据了解,您需要执行将请求保存在目录中的数据驱动测试。
以前,提供了一种方法 来循环遍历数据并保存响应。
您现在可能需要的所有更改都在第一步中 - 读取目录,遍历文件并将文件内容设置为请求和 运行 soap 请求步骤。
Groovy 第 1 步的脚本:
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)
您可以继续使用上面提供的剩余步骤link。
要求:从文件夹中读取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
根据评论更新,添加到问题中。
我的测试用例包含 3 个步骤。第 1 步:从文件夹中读取 xml 文件。第 2 步:使用 xml 文件内容作为 soap 请求输入。第 3 步:在输出文件夹中将第 2 步的响应保存为 xml.
据了解,您需要执行将请求保存在目录中的数据驱动测试。
以前,提供了一种方法
您现在可能需要的所有更改都在第一步中 - 读取目录,遍历文件并将文件内容设置为请求和 运行 soap 请求步骤。
Groovy 第 1 步的脚本:
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)
您可以继续使用上面提供的剩余步骤link。