在 Cucumber '功能文件'->“示例",如何设置 CSV 文件的路径文件、示例、路径、如何设置

2023-09-08 00:10:10 作者:唸红颜

我的示例功能文件,而不是提供示例中的数据,我希望它从 csv 传递如何实现,任何人都可以帮助我.

My sample feature file rather than giving data from Examples I want it to pass from csv how to achieve that can anyone help me out.

功能文件:

Feature: Rocky Search Status

      Scenario Outline: Rocky Search Status with Filters
        Given Open firefox and start application for Rocky Search Status
        When User enters "<price_right>" and "<Carat_left>" and "<Color_right_param>" and  "<Cut_right_param>" and "<Clarity_right_param>"
        Then Message displayed Rocky Search Status Successful
        Then Application should be closed after Rocky Search Status

        Examples: 
          | price_right | Carat_left | Color_right_param  | Cut_right_param |  Clarity_right_param |
          |       10000 |        1.5 |             80     |           180   |                84    |

我希望在项目外部以 CSV 格式定义数据值.

I want the data values to be defined in CSV outside the Project.

推荐答案

你不能用 Gherkin.您可以做的是给您的 CSV 文件一个适当的名称,在 Gherkin 步骤中引用该名称,然后在您的步骤定义中加载并读取该文件.

You can't with Gherkin. What you can do is to give your CSV file an appropriate name, refer to the name inside your Gherkin step, and then load and read the file inside your step definition.

abc.feature

Feature: A
  Scenario: 1
    Given data at abc.csv
    ...

step-definitions.js

Given(/^data at (.*)$/, function (fileName) {
  const data = jsonfile.readFileSync(`${__dirname}/${fileName}`);
  // iterate over data
})