Created
August 30, 2012 17:59
-
-
Save leviwilson/3535625 to your computer and use it in GitHub Desktop.
Reusing background steps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
When I login with 'user' / 'secret' | |
Then I am successfully logged in |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Given /^I have logged in successfully$/ | |
Given "I login with 'user' / 'secret' | |
Then "I am successfully logged in" | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Background: | |
Given I have logged in successfully |
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
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
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.