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