【问题标题】:How to use external data file (for example a json file) for scenario outline for iterate over a scenario如何将外部数据文件(例如 json 文件)用于场景大纲以迭代场景
【发布时间】:2019-08-22 14:56:37
【问题描述】:

如何使用外部数据文件(例如 json 文件)作为场景大纲来迭代 CucumberJS 中的场景

【问题讨论】:

  • 嗨,欢迎来到 SO。您能否提供更多详细信息和一些您尝试过的代码示例?

标签: webdriver-io cucumberjs


【解决方案1】:

您需要使用自定义代码将数据注入ExamplesScenario OutlineNoraUiautomation Framework 使用这个技术。

场景示例:

@loginLogout
Feature: loginLogout (Scenario that login and logout with any good user.) 

    Scenario Outline:  Scenario that login and logout with any good user.

    Given I check that 'user' '<user>' is not empty
    Given I check that 'user' '<password>' is not empty

    Given 'BAKERY_HOME' is opened
    Then The BAKERY home page is displayed

    When I log in to BAKERY as '<user>' '<password>'
    Then The administrator part of the BAKERY portal is displayed?
        |key|expected|actual|
        |profile|admin|<profile>|
    Then The referencer part of the BAKERY portal is displayed?
        |key|expected|actual|
        |profile|referencer|<profile>|    

    And I wait 3 seconds

    When I log out of BAKERY
    Then The BAKERY logout page is displayed

    And I go back to 'BAKERY_HOME'

    Examples:
    #DATA
    |id|user|password|profile|
    |1|foo|123456|admin|
    |2|bar|123456|referencer|
    #END

java中注入器的例子,你需要用你喜欢的语言使用相同的技术:

public static void injectDataInGherkinExamples(String filename, Map<Integer, List<String[]>> examplesTable) {
    try {
        if (!examplesTable.isEmpty()) {
            final Path filePath = getFeaturePath(filename);
            final String fileContent = new String(Files.readAllBytes(filePath), Constants.DEFAULT_ENDODING);
            String lang = getFeatureLanguage(fileContent);
            LOGGER.info(lang);
            StringBuilder examplesString;
            final String[] scenarioOutlines = "fr".equals(lang) ? fileContent.split(SCENARIO_OUTLINE_SPLIT_FR) : fileContent.split(SCENARIO_OUTLINE_SPLIT);
            for (final Entry<Integer, List<String[]>> examples : examplesTable.entrySet()) {
                examplesString = new StringBuilder();
                examplesString.append("    ");
                for (int j = 0; j < examples.getValue().size(); j++) {
                    examplesString.append(SCENARIO_EXAMPLE_COLUMNS_SEPARATOR);
                    examplesString.append(j + 1);
                    for (final String col : examples.getValue().get(j)) {
                        examplesString.append(SCENARIO_EXAMPLE_COLUMNS_SEPARATOR);
                        examplesString.append(col);
                    }
                    examplesString.append(SCENARIO_EXAMPLE_COLUMNS_SEPARATOR + "\n    ");
                }

                scenarioOutlines[examples.getKey() + 1] = scenarioOutlines[examples.getKey() + 1].replaceAll("(" + DATA + "\r?\n.*\r?\n)[\\s\\S]*(" + DATA_END + ")",
                        "$1" + examplesString.toString() + "$2");
            }

            try (BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath.toString()), Constants.DEFAULT_ENDODING));) {
                int i = 0;
                bw.write(scenarioOutlines[i]);

                while (++i < scenarioOutlines.length) {
                    if ("fr".equals(lang)) {
                        bw.write(SCENARIO_OUTLINE_SPLIT_FR + scenarioOutlines[i]);
                    } else {
                        bw.write(SCENARIO_OUTLINE_SPLIT + scenarioOutlines[i]);
                    }
                }
            }
        }
    } catch (final IOException e) {
        LOGGER.error("error GherkinFactory.injectDataInGherkinExamples()", e);
    }
}

您可以在 github 上找到所有源代码here

【讨论】:

    猜你喜欢
    • 2014-07-04
    • 1970-01-01
    • 2018-09-23
    • 1970-01-01
    • 1970-01-01
    • 2014-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多