Is there a way to write to a text file using Karate

核能气质少年 提交于 2019-11-28 10:58:17

问题


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?


回答1:


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 can be any data-type.

Here is an example.

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

  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. And do not write your tests so that scenarios (or features) depend on other scenarios, this is a very bad practice.

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

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




回答2:


My justification for writing to a file is a different one. I am using karate explicitly to implement a mock. I want to expose an endpoint wherein the upstream system will send some basic data through json payload using POST/PUT method and karate will construct the subsequent payload file and stores it the specific folder, and this newly created payload file will be exposed through another GET call.



来源:https://stackoverflow.com/questions/54591724/is-there-a-way-to-write-to-a-text-file-using-karate

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!