Improve cookie support #179

Closed
joshuaclayton opened this Issue Sep 30, 2011 · 3 comments

Projects

None yet

3 participants

Owner

It'd be pretty awesome if capybara-webkit standardized on the way it handles cookies, maybe like selenium: http://selenium.googlecode.com/svn/trunk/docs/api/rb/Selenium/WebDriver/Options.html#add_cookie-instance_method

Currently to have access to current_user in your steps, for example, you'd have to do something like this:

require "cgi"
module SessionHelpers
  def current_user
    remember_token = if Capybara.current_driver == :webkit
                       found_cookie = page.driver.browser.get_cookies.detect {|c| c.include?("remember_token=") }
                       if found_cookie
                         CGI::Cookie.parse(found_cookie)["remember_token"].first
                       end
                     else
                       cookies["remember_token"]
                     end
    User.where(:remember_token => remember_token).first
  end
end

World(SessionHelpers)

It'd be nice if there was a nicer way to grab cookies by name, get their values, etc.

Contributor
niklasb commented Oct 1, 2011

I'll hack together some sugar for this when I find the time (which will probably not happen before next Thursday). The mechanize gem provides a nice cookie jar implementation on top of WEBrick::Cookie, so you might wanna use this as an intermediate solution or monkey-patch the get_cookies function by yourself.
Note however that it's probably not as simple as returning a hash of key/value pairs, since cookies can have different values depending on the context (name, path, protocol, ...).
It might be useful to default this context to the host/port of the web-application under test, but cookies set by remote domains should also be accessible using a more intuitive API, IMHO.

@niklasb niklasb added a commit to niklasb/capybara-webkit that referenced this issue Oct 6, 2011
@niklasb niklasb adds syntax sugar for reading cookie values (fix #179) 341306f
Contributor
niklasb commented Oct 6, 2011

I implemented this and opened a pull request. The new idiomatic syntax goes like this:

page.driver.cookies["remember_token"]

And returns the value or nil if no such cookie exists. You can also specify a domain and optionally also a path to match against:

page.driver.cookies["cuser", "www.facebook.com"]
page.driver.cookies["abc", "www.example.org", "/dir/"]

By default, the first matching cookie is returned (most specific path takes precendence).

@niklasb niklasb added a commit to niklasb/capybara-webkit that referenced this issue Oct 6, 2011
@niklasb niklasb adapt wording of CookieJar specs (#179) f76bd70
@massive massive added a commit to massive/capybara-webkit that referenced this issue Oct 31, 2011
@niklasb @massive niklasb + massive adds syntax sugar for reading cookie values (fix #179) 205e393
@massive massive added a commit to massive/capybara-webkit that referenced this issue Oct 31, 2011
@niklasb @massive niklasb + massive adapt wording of CookieJar specs (#179) bd6c889
Contributor

Seems like pull request #183 addressed this.

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