Reputation: 1068
I have two scenarios in two different feature files but both scenarios tests search functions but on different parts of my page. The scenarios I have looks something like this:
Scenario Outline: Search items from QuickSearch
Given that the following items
| id | title |
| 1 | Item1 |
| 2 | Item2 |
When I search for <criteria> in this search
Then I should get <result>
And I should not get <excluded>
Examples:
|criteria|result | excluded |
| 1 | 1 | 2 |
| 2 | 2 | 1 |
and:
Scenario Outline: Using a filter
Given that I have the following things:
|id |name |
|1 | thing1 |
|2 | thing2 |
When I use the <filter> filled with <criteria>
Then I should obtain these <results>
And I should not obtain these <exclusions>
Examples:
|filter |criteria |results |exclusions |
|name |thing |1,2 | |
|id |1 |1 |2 |
As you can tell in the second scenario I have changed the word get to obtain in order to write separate steps for the two scenarios. The only reason I need two different steps is because the id in the 2 different scenarios map to different names (no I cant use the same for both and don't want to start on id 3 in the second)
So I'm thinking about common steps for both of the scenarios (at least when it comes to the then steps) and I want a hash mapping id and name together in order to do validation, but I want the hash to be different depending on which scenario called the step.
So, is there a way in cucumber + capybara that I can tell which scenario called a step?
Upvotes: 2
Views: 1886
Reputation: 46846
I do not know of a way to directly access the scenario name from the Cucumber step. However, you can access the name in a before hook and store it in a variable so that it is available to your steps.
Add this before hook to your env.rb
:
Before do |scenario|
case scenario
when Cucumber::Ast::OutlineTable::ExampleRow
@scenario_name = scenario.scenario_outline.name
when Cucumber::Ast::Scenario
@scenario_name = scenario.name
else
raise('Unhandled scenario class')
end
end
Edit: if you're using a more recent version of Cucumber, try instead:
Before do |scenario|
case scenario.source.last
when Cucumber::Core::Ast::ExamplesTable::Row
@scenario_name = scenario.scenario_outline.name
when Cucumber::Core::Ast::Scenario
@scenario_name = scenario.name
else
raise('Unhandled scenario class')
end
end
Your steps can then check the scenario name using @scenario_name
. Example:
Then /I should get (.*)/ do |result|
if @scenario_name == 'Search items from QuickSearch'
# Do scenario specific stuff
elsif @scenario_name == 'Using a filter'
# Do scenario specific stuff
end
# Do any scenario stuff
end
Upvotes: 1