1

I am using cucumber for BDD testing and the feature file looks as follows:

Feature: Sing Up

  Scenario: User tries to sign up without interests 
    Given the interests are empty
    When user presses on sign up
    Then it should show "The interests field is empty."

  Scenario: User tries to sign up with 2 interests 
    Given 2 interests are entered
    When user presses on sign up
    Then it should show "The interests should be at least three."

The I've created the steps in JS:

const assert = require("assert");
const { Given, When, Then } = require("cucumber");

Given("the interests are empty", function() {
  // Write code here that turns the phrase above into concrete actions
  return "pending";
});

When("user presses on sign up", function() {
  // Write code here that turns the phrase above into concrete actions
  return "pending";
});

Then("it should show {string}", function(string) {
  // Write code here that turns the phrase above into concrete actions
  return "pending";
});

/*----------------------------------------------------------------------*/

Given("{int} interests are entered", function(int) {
  // Given('{float} interests are entered', function (float) {
  // Write code here that turns the phrase above into concrete actions
  return "pending";
});

When("user presses on sign up", function() {
  // Write code here that turns the phrase above into concrete actions
  return "pending";
});

Then("it should show {string}", function(string) {
  // Write code here that turns the phrase above into concrete actions
  return "pending";
});

The compiler complains:

   ✖ When user presses on sign up
       Multiple step definitions match:
         user presses on sign up - features/step_definitions/signup/steps.js:9 
         user presses on sign up - features/step_definitions/signup/steps.js:27

How to allow same when in different scenario?

softshipper
  • 32,463
  • 51
  • 192
  • 400

1 Answers1

4

Your step definitions have to be unique and you don't have to redefine them per scenario. The compiler tells that your user presses on sign up step has been defined twice (same with it should show {string}).

Laszlo
  • 2,225
  • 19
  • 22
  • 4
    In other words, delete both of the duplicates; the single definition will be run by both scenarios. – Mike Collins Mar 11 '20 at 21:59
  • @MikeCollins So what happens if 2 of my scenario has one step which has the same words in both of the scenario but does 2 different things depending upon the scenario in place. Anyway to override step definition per scenario? – taimur alam Mar 16 '21 at 12:03
  • @taimuralam no, I don't think this is possible. The Cucumber parser doesn't have a mechanism for telling it to prefer one matching step definition over another. I've never come across a scenario where this would be required - feel free to open another question with the details and perhaps I can provide more insight. – Mike Collins Mar 16 '21 at 15:46