空手道:写入文本文件 [英] Karate: Write to a text file
问题描述
我有以下功能文件,该功能文件读取输入并将输入ID附加到响应中,并以以下格式写入文本文件:
I have the following feature file that reads the input and appends the input id with the response and writes to a text file in the below format:
|123|{"products": [ { "pid": "1a"} ] }|
|124|{"products": [ { "pid": "1b"} ] }|
|125|{"products": [ { "pid": "1c"} ] }|
这样我就可以用它来创建输入表了,不需要以文本格式复制每个响应并粘贴来制作示例:
so that I can make the input table with it and dont need copy each response in text format and paste to make Examples:
我尝试了以下方法:
Feature: sample karate test script
Background:
* url BaseUrl
* configure headers = read('classpath:headers.js')
* def jsonFromCsv = read('data.csv')
* def size = karate.sizeOf(jsonFromCsv)
* print size
Scenario Outline: Get All Tests
* def doStorage =
"""
function(args) {
var DataStorage = Java.type('DataStorage.DataStorage'); // Java class that writes to a text file
var dS = new DataStorage();
return dS.write(args);
}"""
Given path '/v1/path'
When method get
Then status 200
* def json = __row.expected
* def id = __row.id
* def app = '|'+<id>+'|'+json+'|'
* print app # prinst it in the expected format
* def result = call doStorage app
Examples:
| jsonFromCsv |
但是问题是只有最近读取的数据正在写入文件中.
我也尝试了下面的方法,但是给出了与上面相同的结果:
* def js = function (app){karate.write(app,'input.txt')}
But the issue is only the last read data is being written to the file.
I had tried the below as well but gives the same result as above:
* def js = function (app){karate.write(app,'input.txt')}
DataStorage.java:
DataStorage.java:
package DataStorage;
import java.io.*;
public class DataStorage {
public void write( String text) throws IOException {
BufferedWriter output = null;
try {
File file = new File("input");
output = new BufferedWriter(new FileWriter(file));
output.append(text);
output.close();
} catch ( IOException e ) {
e.printStackTrace();
} finally {
if ( output != null ) {
output.close();
}
}
}
}
推荐答案
You can use CSV files (have to be comma delimited): https://github.com/intuit/karate#csv-files
另请参见 查看全文