Groovy脚本来读取一个XML文件,并用文件内容更新下一步请求 [英] Groovy script to Read an xml file and update next step request with file contents

查看:543
本文介绍了Groovy脚本来读取一个XML文件,并用文件内容更新下一步请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

  def fileList = [] 
新文件(C:\\ Users \\ Documents \\Groovy Scripts \\requests)。eachFile
{f - >
if(f.isFile()& f.name.endsWith('。xml'))
{
def filename = f.name [0 ..- 5] $如果(fileList.size()<1)
b $ b fileList.add(文件名)
log.info文件名

}
}
{
testRunner.fail(找不到请求文件)
}
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文件内容作为肥皂请求输入。第3步:将第2步的响应保存为输出文件夹中的xml。

解决方案

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



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

全部您现在可能需要的更改是在第一步 - 读取目录,并循环通过您的文件并将文件内容设置为请求并运行soap请求步骤。



<第1步的Groovy脚本:

 导入groovy.io.FileType 

//将输入目录名改为
def dir = new File('path / to / input / dir')
dir.eachFile(FileType.FILES){file - >

//获取步骤
def step = context.testCase.getTestStepAt(1)
//将文件内容设置为测试步骤请求
step.testRequest。 requestContent = file.text
log.info为step2设置新请求:$ {request}
//运行step2
step.run(testRunner,context)
}
//现在所有的订单都被执行了,现在需要退出步骤而不需要另外运行step2
//所以,跳转到step2,索引是2
testRunner.gotoStep(2)

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


Requirement: To read the xml file from the folder and pass the contents of the file to Soap request.

Issue I am trying to read the file saved in the folder using groovy script, But unable to read the contents of the file. I am getting Null pointer exception while trying to print the contents of the xml file.

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 based on comments, adding to the question.

My test case contains 3 steps. Step 1: to read the xml file from the folder. Step 2: use the xml file content as soap request input. Step 3: save the response of step 2 in output folder as xml.

解决方案

It is understand that you need to do the data-driven tests where requests are kept in a directory.

Previously, an approach is provided here to loop thru the data and save responses.

All the change you might need now is in the very first step - which reads the directory, and loops thru your files and set the file content as request and run the soap request step.

Groovy Script for Step1:

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)

You can continue to use the remaining steps as mentioned in the above provided link.

这篇关于Groovy脚本来读取一个XML文件,并用文件内容更新下一步请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆