有没有一种方法可以使用空手道写入文本文件 [英] Is there a way to write to a text file using Karate

查看:54
本文介绍了有没有一种方法可以使用空手道写入文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在空手道测试中,我需要将响应ID写入txt文件(或其他任何文件格式,例如JSON),想知道它是否具有执行此功能的能力,我在文档中没有看到其他内容.如果没有,是否有一个简单的JavaScript函数可以实现?

In my karate tests i need to write response id's to txt files (or any other file format such as JSON), was wondering if it has any capability to do this, I haven't seen otherwise in the documentation. In the case of no, is there a simple JavaScript function to do so?

推荐答案

在JavaScript中尝试karate.write(value, filename).我们不记录此文件,因为我们不鼓励这样做.此外,该文件将仅写入当前的"build"目录,对于Maven项目/独立JAR,该目录为target.

Try karate.write(value, filename) in JavaScript. We have kept this un-documented because we don't encourage it. Also the file will be written only to the current "build" directory which will be target for Maven projects / stand-alone JAR.

value可以是任何数据类型,空手道将把字节(或纯文本)写出.没有任何其他格式的内置支持.

value can be any data-type, and Karate will write the bytes (or plain-text) out. There is no built-in support for any other format.

这是一个对于将来遇到此问题的其他人,正确的做法是:

for others coming across this answer in the future the right thing to do is:

  1. 不要一开始就写文件,您根本不需要这样做,这个问题通常是由经验不足的人提出的,出于某种原因,他们认为在验证之前保存"响应的唯一方法是将其写入文件.不,请不要浪费您的时间-只需match反对response.发出其他HTTP请求时,可以将其(或部分)保存为变量.并且不要编写测试,以使方案(或功能)依赖于其他方案,这是非常不好的做法.另外请注意,默认情况下,空手道会将所有HTTP请求和响应转储到日志文件(通常在target/karate.log中)以及HTML报告中.

  1. don't write files in the first place, you never need to do this, and this question is typically asked by inexperienced folks who for some reason think that the only way to "save" a response before validation is to write it to a file. No, please don't waste your time - and please just match against the response. You can save it (or parts of it) to variables while you make other HTTP requests. And do not write your tests so that scenarios (or features) depend on other scenarios, this is a very bad practice. Also note that by default, Karate will dump all HTTP requests and responses in the log file (typically in target/karate.log) and also in the HTML report.

根据此答案查看karate.write()是否对您有用

see if karate.write() works for you as per this answer

编写一个自定义Java(或使用JVM的JS函数),以使用

write a custom Java (or JS function that uses the JVM) to do what you want using Java interop

这篇关于有没有一种方法可以使用空手道写入文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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