how do you handle do not repeat yourself gherkin script?

152 views Asked by At

I am new to Cucumber and Gherkin, here's the situation I think I will come across. A home page contains many features, naturally you want to start from the home and perform a task, feature.

Given user logged into home page
When user performs feature 1
Then ... 

Given user logged into home page
When user performs feature 2
Then ... 

When you run Cucumber the for the second feature, it will complain that the Given step is ambiguous.

How does one resolve this issue? You can change the Given step a little bit for the second feature. By doing so, you are actually repeating yourself. Thoughts, anyone?

2

There are 2 answers

0
Richlewis On

I set mine up slightly different, taking your example

Given user logged into home page
When user performs feature 1
Then ... 

Given user logged into home page
When user performs feature 2
Then ... 

Within my corresponding steps file i would just have a section at the top for reusable steps (as don't forget all matches are on regexp)

# Reusable Steps
Given(/^user logged into home page$/) do
  # login steps here
end

# Feature 1
When(/^user performs feature 1$/) do
  # steps here
end

# Feature 2
When(/^user performs feature 2$/) do
  # steps here
end

personal preference i guess but thought i would share

4
Pippo On

if user logged into homepage is a pre requisite for your tests within the feature to run, then put it in the background:

Background:
  Given user logged into home page

Then:

Scenario: 1
When user performs feature 1
Then ... 

Scenario: 2
When user performs feature 2
Then ...