问题
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:
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 theresponse
. And do not write your tests so that scenarios (or features) depend on other scenarios, this is a very bad practice.see if
karate.write()
works for you as per this answerwrite 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