views:

703

answers:

2

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

+8  A: 

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
tomafro
Thank you muchly good sir.
Daniel Huckstep
A: 

You can put steps into "webrat_steps.rb". This is where all the predefined steps live ("When I go to", "Then I should see", etc.) but you can add your own here too. Anything defined here will be available to all features.

Doug R
you can also add your own my_steps.rb with your custom steps in the step_definition folder. Cucumber will detect them too and you keep your webrat steps clean.
Mauricio