Skip to content

Instantly share code, notes, and snippets.

@nruth
Created July 21, 2010 17:16
Show Gist options
  • Select an option

  • Save nruth/484787 to your computer and use it in GitHub Desktop.

Select an option

Save nruth/484787 to your computer and use it in GitHub Desktop.
Testing login "remember me" feature with Capybara (rack::test or selenium) - deleting the session cookie (only)
@javascript @announce
Scenario: remembering users so they don't have to log in again for a while
Given an activated member exists with forename: "Paul", surname: "Smith", email: "paul_smith_91@gmail.com", password: "bananabanana"
When I go to the dashboard
Then I should see "Existing Member Login"
When I fill in "Email" with "paul_smith_91@gmail.com" within "#member_login"
And I fill in "Password" with "bananabanana" within "#member_login"
And I check "Remember me"
And I press "Sign in"
Then I should be on the dashboard
And I should see "Logged in as Paul Smith"
And I should see "Sign out"
Given I close my browser (clearing the Medify session)
When I come back next time
And I go to the dashboard
Then I should see "Logged in as Paul Smith"
And I should see "Sign out"
@rack_test @announce
Scenario: don't remember users across browser restarts if they don't want it
Given an activated member exists with forename: "Paul", surname: "Smith", email: "paul_smith_91@gmail.com", password: "bananabanana"
When I go to the dashboard
Then I should see "Existing Member Login"
When I fill in "Email" with "paul_smith_91@gmail.com" within "#member_login"
And I fill in "Password" with "bananabanana" within "#member_login"
And I uncheck "Remember me"
And I press "Sign in"
Then I should be on the dashboard
And I should see "Logged in as Paul Smith"
And I should see "Sign out"
Given I close my browser (clearing the Medify session)
When I come back next time
And I go to the dashboard
Then I should see "Existing Member Login"
And I should not see "Logged in as Paul Smith"
And I should not see "Sign out"
@lgrains

lgrains commented Oct 20, 2010

Copy link
Copy Markdown

Nice! The only thing I'm having a problem with is how to implement "When I come back next time". What does the step definition look like?

@nruth

nruth commented Oct 20, 2010

Copy link
Copy Markdown
Author

Mine's just an alias for the visit homepage step, like this:

When /^I come back next time$/ do
  When "I go to the homepage"
end

It's just so it reads better in the scenario rather than to do anything technical (the "go to the dashboard" step is enough)

@lgrains

lgrains commented Oct 20, 2010

Copy link
Copy Markdown

Great! Thanks for the quick response.

@railsagile

Copy link
Copy Markdown

It works very well.
Thank you sir.

@ilpoldo

ilpoldo commented Dec 15, 2010

Copy link
Copy Markdown

In case you don't want to bake in the helper method the session key for the app you are working you can access it like this if you are testing a rails 3 app

  def session_cookie_name
    Rails.application.config.session_options[:key]
  end

@nruth

nruth commented Dec 15, 2010

Copy link
Copy Markdown
Author

Thanks, that's a nice addition. I wrote this for rails 2.3 and didn't spent too long looking for a way to do that.
I've updated the gist

@nruth

nruth commented Jan 7, 2011

Copy link
Copy Markdown
Author

Given that there's been some interest in using this I've refactored it and made a real repo so refactoring and new features and whatnot can be done properly.
There's also been culerity support added (from xunker's gist fork) and it's been made into a gem.

@cice

cice commented Jun 22, 2011

Copy link
Copy Markdown

note, in cucumber 1.0.0, you have to check for Capybara::Selenium::Driver, not Capybara::Driver::Selenium, same for RackTest i think

@nruth

nruth commented Jun 22, 2011

Copy link
Copy Markdown
Author

@cice there's a more up-to-date version at https://github.com/nruth/show_me_the_cookies though this particular change is in capybara not cucumber. There's actually a method returning the driver name as a symbol now, so no need to use the driver's class.

@cice

cice commented Jun 22, 2011

Copy link
Copy Markdown

alright, thnx alot

@bilus

bilus commented Dec 24, 2012

Copy link
Copy Markdown

A slightly cleaned-up version that also supports Webkit.

NOTES:

  1. It's based on the code above so it's supposed to work with Selenium & RackTest but I've only tested it against Webkit.
  2. It does not support the @announce tag.
  3. I've just noticed the gem. :D

https://gist.github.com/4367281

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