Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access cookies and session in Capybara 2.4.1

I'm using rails 4 with the following gems:

  • guard-rspec (4.3.1)
  • rspec (3.0.0)
  • rspec-core (3.0.3)
  • rspec-expectations (3.0.3)
  • rspec-mocks (3.0.3)
  • rspec-rails (3.0.2)
  • rspec-support (3.0.3)
  • capybara (2.4.1)
  • capybara-webkit (1.1.0)

Capybara works as expected in normal scenarios/examples, but when I want access to a cookie or session, I get a NameError:

undefined local variable or method `session'

I have a custom class I'm using that stores cookie data and retrieves it to remember things about the user and then present certain things on the screen.

But I can't seem to get the tests to pass and I can't view the cookie or session.

Does it need to be mocked? How can I do this?

like image 786
Nathan Avatar asked Oct 01 '22 04:10

Nathan


2 Answers

Capybara.current_session.driver.request.session works in 2.17.0. I'm assuming much hasn't changed though since 2.4.

like image 147
user701847 Avatar answered Oct 03 '22 11:10

user701847


Webkit exposes cookies like this:

page.driver.browser.manage.all_cookies

Reference here: https://github.com/thoughtbot/capybara-webkit/issues/168

like image 43
Adam Avatar answered Oct 03 '22 11:10

Adam