Groovy:用不同的文件名打印

Groovy: Print with different file names

我正在学习 groovy 语言并使用 SOAP 进行测试,我也是语言新手。我只是 运行 我的加载脚本,但我想通过另一个文件名打印出我的请求和响应。我可以打印 requests/responses 但写在同一个文件上。

这是我的路径:

def outputPath = "C:/FileName/"

outputPath = outputPath

def folder = new File( outputPath )

if( !folder.exists() ) {
  folder.mkdirs()
}

return outputPath

这是我的打印输出:

def request = context.expand( '${Script1#Request}' )
def response = context.expand( '${Script1#Response}' )
def outputPath = context.expand( '${init#result}' )

def requestPath = outputPath + "/Script1_req.xml"
def responsePath = outputPath + "/Script1_res.xml"

def f = new File(requestPath)
f.write(request, "UTF-8")

def f2= new File(responsePath)
f2.write(response, "UTF-8")

如何使用另一个文件名打印出我的每个脚本,例如 script1_req(1-XXXX).xml?

感谢您的回答。

您只需为文件名附加唯一值即可。

下面两个语句的变化

def requestPath = outputPath + "/Script1_req.xml"
def responsePath = outputPath + "/Script1_res.xml"

收件人:

//Get the date in the given format
​def date = new Date().format('yyyyMMddHHmmss')
def requestPath = "${outputPath}/Script1_req_${date}.xml" as String
def responsePath = "${outputPath}/Script1_res_${date}.xml" as String