In Cucumber 'Feature file '-> 'Example

2019-08-27 06:46发布

问题:

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 file:

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    |

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

回答1:

Not directly. However, you can have a record ID (or test case number) of sorts in the Example table. You can then retrieve records from the CSV in the step code based on the ID.

Scenario Outline: Rocky Search Status with Filters
    Given Open firefox and start application for Rocky Search Status
    When User enters data specified in test case <tcn>
    Then Message displayed Rocky Search Status Successful
    Then Application should be closed after Rocky Search Status

    Examples: 
     |tcn|
     |1  |
     |2  |

The "When" step will use the tcn to retrieve the corresponding record from the CSV.



回答2:

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