空手道:写入文本文件 [英] Karate: Write to a text file

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

问题描述

我有以下功能文件,该功能文件读取输入并将输入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();
              }
            }
        }
    }  

推荐答案

您可以使用CSV文件(必须以逗号分隔):

You can use CSV files (have to be comma delimited): https://github.com/intuit/karate#csv-files

另请参见 查看全文

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