Bart
Bart

Reputation: 101

Generic step definitions in Cucumber

I was wondering if it would be possible to write generic step_definitions in cucumber, that can be used for a Given, When or Then scenario when needed. For example:

Given the browser is started
When I navigate to my webpage
Then the element with id login is present

Given the element with id login is present
When I press the button
Then something should happen

In this case, I would like to create one step_definition for the element with id login is present that can be used in the Given and the Then clause.

Using Javascript, I can type:

defineSupportCode(({ Given, Then }) => {
    Then(/^Element with id (.+) is present$/, (name) => {
        return client.expect.element("#" + name).to.be.present;    
    });
});

Although this seems to work if I provide Given and Then in the defineSupportCode (even though I don't understand why), it feels not right to specify the step itself as a Then. I would prefer to define it as Generic or anything like that. Of course I could write the Scenario as:

* the element with id login is present

but that's something I actually dislike as well. Am I missing something here?

Upvotes: 2

Views: 1077

Answers (1)

Thomas Sundberg
Thomas Sundberg

Reputation: 4323

Steps in Cucumber are global.

Cucumber as such doesn't care about the method name of a step. It matches which method to execute by using the regular expression. This means that you can create the general steps you are looking for.

Upvotes: 2

Related Questions