【问题标题】:Karate: Write to a text file空手道:写入文本文件
【发布时间】:2019-10-15 11:19:16
【问题描述】:

我有以下功能文件,它读取输入并将输入 id 附加到响应中,并以以下格式写入文本文件:

 |123|{"products": [ { "pid": "1a"} ] }|
 |124|{"products": [ { "pid": "1b"} ] }|
 |125|{"products": [ { "pid": "1c"} ] }|

这样我就可以用它制作输入表,并且不需要以文本格式复制每个响应并粘贴来制作示例:

我尝试了以下方法:

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')}

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();
              }
            }
        }
    }  

【问题讨论】:

  • 您是否尝试从 JSON 构建示例表?
  • 我正在尝试为回归套件构建示例表。
  • 有什么方法可以一次性完成。我有 1000 多个测试用例,用于 Excel 表中的单个 API 以实现自动化!同样,我有几项服务。

标签: karate


【解决方案1】:

您可以使用 CSV 文件(必须以逗号分隔):https://github.com/intuit/karate#csv-files

另见example:

Scenario Outline: cat name: <name>
    Given url demoBaseUrl
    And path 'cats'
    And request { name: '<name>', age: <age> }
    When method post
    Then status 200
    And match response == { id: '#number', name: '<name>' }

    # the single cell can be any valid karate expression
    # and even reference a variable defined in the Background
    Examples:
    | read('kittens.csv') |

【讨论】:

  • 由于字符限制,我无法使用 csv。 API 响应超出了 csv 的限制,因此如果我们复制到 csv,它将被截断。这就是我采用这种方法的原因
  • @JyothishKaimal 我建议您只创建一个 JSON 数组,这就是您所需要的。您可以通过 Java 做到这一点,许多团队通过这种方式与数据库集成。看看这个例子:github.com/intuit/karate#data-driven-features 然后这个:github.com/intuit/karate/blob/master/karate-demo/src/test/java/… - 如果你还有问题,请按照这个过程:github.com/intuit/karate/wiki/How-to-Submit-an-Issue
  • 谢谢@Peter。我在这里遇到了另一个问题,响应 json 包含数组:"Indicators": [ "A1","A2","A3","A4", ] 但问题是每次我点击 api 时数组项的顺序都会发生变化,比如 "Indicators": [ "A2","A1","A3","A4", ] 因为匹配失败。有没有办法忽略数组项的顺序?或者我该如何解决这个问题?
  • @JyothishKaimal 请不要将其称为“问题”,请阅读文档并查找contains
猜你喜欢
  • 2019-07-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多