Skip to content

Instantly share code, notes, and snippets.

@leviwilson
Created August 30, 2012 17:59
Show Gist options
  • Save leviwilson/3535625 to your computer and use it in GitHub Desktop.
Save leviwilson/3535625 to your computer and use it in GitHub Desktop.
Reusing background steps
class LoginPage
include Gametel
text(:username, :index => 0)
text(:password, :index => 1)
def login_successfully
self.username = 'user'
self.password = 'secret'
on(LandingPage).should be_active
end
end
Given /^I have logged in successfully$/
on(LoginPage).login_successfully
end
When I login with 'user' / 'secret'
Then I am successfully logged in
Given /^I have logged in successfully$/
Given "I login with 'user' / 'secret'
Then "I am successfully logged in"
end
Background:
Given I have logged in successfully
@leviwilson
Copy link
Author

How would you reuse step definitions? Would you do it like I've done in one_way.rb by re-using other step definitions, or would you push behavior into the page object like I have in anothe_way.rb?

@JonKernPA
Copy link

I wouldn't use nested "Given.. Given"

Instead, turn it into a reusable login method in your steps.rb file -- or in the @chzy page object style if that is preferred

@leviwilson
Copy link
Author

Agreed, that is the approach I had been taking. Nested Given statements now have two points of change if the gherkin is modified. I just wasn't sure what was idiomatic.

@cheezy
Copy link

cheezy commented Aug 30, 2012

I would not call one step definition from another. It will create very brittle steps.

@cheezy
Copy link

cheezy commented Aug 30, 2012

I also would not say "I have logged in successfully". Instead I would say given I'm logged in and I would not have the step verify I have logged in. I would just attempt to perform the next action. If I am not logged in then it will fail.

@leviwilson
Copy link
Author

Right, that's why I added

on(LandingPage).should be_active

So rspec will assert that we're on the landing page where LandingPage looks like

class LandingPage

  def active?
    has_text? 'some text to find on Landing Page'
    has_view? 'id_that_only_exists_on_landing_page'
  end
end

@JonKernPA
Copy link

General good rule of thumb is to strive to keep any technical stuff out of the cucumber steps.

  Scenario: Axial Admin behavior
    Given I am on sign in page
    When I login as an "axial_admin" role
    Then I should see the following links:
      | Encounters |
      | Runsheets  |
      | More       |
      | Sign Out   |

I could probably take the 'as an "X" role' and make that even more english-like: 'I login as an Axial Admin' and do some magic behind the scenes. Deeper in the step implementation, you can see some other styles of reuse:

Given /^I am logged in as an? "([^"]*)" role$/ do |role|
  step %Q(I login as an "#{role}" role)
end
When /^I login as an? "([^"]*)" role$/ do |role|
  login = "#{role}_user"
  @user = FactoryGirl.create :user,
    :id => 'user',
    :login => "#{role}_user",
    :role_string => role,
    :confirmed_at => Time.now
  step %Q(I login as "#{login}")
end

When /^I login as "([^"]*)"$/ do |login|
  visit '/users/sign_in'
  fill_in("user_login", :with => login)
  fill_in("user_password", :with => "P@ssw0rd")
  click_button "Sign In"
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment