Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reuse Cucumber steps

I want to reuse some Cucumber steps but can't seem to find the right way.

I want to write a step like:

Given /^I login with (.*) credentials$/ |type|   # do stuff with type being one of "invalid" or "valid" end 

But then have another step like:

Given /^I login successfully$   # call "Given I login with valid credentials" end 

So in testing user authentication I can use the former, but most other places, I can use the latter, and not actually have to repro code.

Is there a way to call that other step, or do I just put the logic in a helper method, and call said method from each task (basically a method extraction refactoring, which, after reading my question makes me believe that's actually the best way anyway)?

like image 489
Daniel Huckstep Avatar asked May 27 '09 21:05

Daniel Huckstep


People also ask

How do cucumbers prevent duplicate steps?

One way you could start to avoid duplication is to document your steps and what they do, so that you and your team can look back on them to see if there is an existing step that they can use.

What is step in Cucumber?

A Step Definition is a Java method Kotlin function Scala function JavaScript function Ruby block with an expression that links it to one or more Gherkin steps. When Cucumber executes a Gherkin step in a scenario, it will look for a matching step definition to execute.

What are cucumbers examples?

In Cucumber, an example is called a scenario. Scenarios are defined in . feature files, which are stored in the src/test/resources/hellocucumber directory (or a subdirectory). One concrete example would be that Sunday isn't Friday.


2 Answers

UPDATE: The method described below has been deprecated. The recommended way to call a step from within another step now looks like this:

Given /^I login successfully$/     step "I login with valid credentials"  end  

Old, deprecated method (for reference):

You can call steps from other steps like this:

Given /^I login successfully$/   Given "I login with valid credentials"   Then "I should be logged in" end 

If all of the scenarios within a feature require this (or other steps), you can also add a Background to each features, with the common steps, like so:

Background:   Given I log in with valid credentials  Scenario: Change my password   Given I am on the account page 
like image 23
tomafro Avatar answered Sep 16 '22 19:09

tomafro


Note that the method for calling steps within steps has changed in recent versions of cucumber, which you'll see if you get an error like "WARNING: Using 'Given/When/Then' in step definitions is deprecated, use 'step' to call other steps instead:/path/to/step_definitions/foo_steps.rb:631:in `block in ' ". See the cucumber wiki for details.

The gist of the change is that you should now use the step or steps methods.

When /^I make all my stuff shiny$/   step "I polish my first thing" end  When /^I make all my stuff shiny$/   steps %Q{     When I polish my first thing     When I shine my second thing   } end 
like image 197
michaeltwofish Avatar answered Sep 19 '22 19:09

michaeltwofish