groovy - 将 Groovy 脚本结果写入文件

标签 groovy soapui

我想将 groovy 脚本结果写入我的 Mac 计算机上的文件中。 我如何在 Groovy 中做到这一点? 这是我的尝试:

log.info "Number of nodes:" + numElements
log.info ("Matching codes:"+matches)
log.info ("Fails:"+fails)

// Exporting results to a file
today = new Date()
sdf = new java.text.SimpleDateFormat("dd-MM-yyyy-hh-mm")
todayStr = sdf.format(today)
new File( "User/documents" + todayStr + "report.txt" ).write(numElements, "UTF-8" )
new File( "User/documents" + todayStr + "report.txt" ).write(matches, "UTF-8" )
new File( "User/documents" + todayStr + "report.txt" ).write(fails, "UTF-8" )

有人可以帮忙导出结果部分吗?

谢谢, 问候, A 好的,我已经成功创建了一个文件

file = new File(dir + "${todayStr}_report.txt").createNewFile()

如何添加元素数量、匹配和失败?像这样:

 File.append (file, matches)? 

我收到以下错误:

groovy.lang.MissingMethodException: No signature of method: static java.io.File.append() is applicable for argument types: (java.lang.Boolean, java.util.ArrayList) values: [true, [EU 4G FLAT L(CORPORATE) SP, EU 4G FLAT M(CORPORATE), ...]] Possible solutions: append(java.lang.Object, java.lang.String), append(java.io.InputStream), append(java.lang.Object), append([B), canRead(), find() error at line: 33

最佳答案

您的文件路径错误。我没有 MAC,所以我不能 100% 确定,但从我的角度来看,你应该有:

new File("/User/documents/${todayStr}report.txt").write(numElements, "UTF-8")

您至少缺少两个反斜杠,第一个在用户之前,第二个在路径中的文档之后。使用您现在的方法,它尝试保存到目录 User/documentDATE,很确定它不存在。

上面我向您展示了绝对路径的方法。你也可以严格这样写:

 new File("${todayStr}report.txt").write(numElements, "UTF-8")

如果文件被创建,那么您将 100% 确定这是您的文件路径的问题:)

还有一些事情 - 因为它是一种 gr​​oovy,所以尝试利用该语言相对于 Java 的优势,有多种处理文件的方法,我还重写了您的日志以向您展示它的工作有多么简单groovy 中的字符串:

log.info "Number of nodes:  ${numElements}"
log.info "Matching codes: ${matches}"
log.info "Fails: ${fails}"

希望对你有帮助

关于groovy - 将 Groovy 脚本结果写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25163688/

相关文章:

grails - 上传MultipartFile时未捕获SizeLimitExceededException

java - 如何在 Groovy 中使用 SOAPUI 类

groovy - groovy 脚本中的测试步骤结果列表

xml - 从 WSDL 文件确定端点

groovy - 使用poi.jar作为数据接收器脚本时出现Groovy编译错误

java - 为什么我会收到 java.net.SocketException : Connection reset error from web service through SOAP UI and Java client?

grails - 使用 Spock 和服务类在 Grails 中进行数据驱动测试

rest - 向 Grails RestfulController 添加自定义操作

groovy - 如何使用 Groovy 读取 txt 文件、获取数据并将其作为变量存储在 SOAPUI 的自定义属性中?

maven - 分步说明设置 SoapUI 项目以使用 Maven 作为 CI 的一部分运行