Cookies in a cucumber dough using capybara

As part of my integration tests for the website, I use a capybara cucumber. It seems that capybara cannot emulate the use of cookies.

For example, I set a cookie when a user signs:

    def sign_in(user)
      cookies.permanent.signed[:remember_token] = [user.id, user.salt]
      current_user = user
    end

However, when I later get the cookie value using cookie.inspect, it returns {} Is this capybara limitation known? How can I test a subscribed user on multiple requests, if so?

I have to add my test:

Scenario: User is signed in when they press Sign In
 Given I have an existing account with email "bob@jones.com" and password "123456"
 And I am on the signin page
 When I fill in "Email" with "bob@jones.com"
 And I fill in "Password" with "123456"
 And I press "Sign In"
 Then I should see "Welcome Bob Jones"
+3
source share
5 answers

here is a good way to show cookie contents when running your function

https://gist.github.com/484787

+2

, . cookie "admin_cta_choice" , .

Given /I have selected CTA "([^"]+)"/ do |cta_name|
  cta = Cta.find_by_name(cta_name)
  raise "no cta with name '#{cta_name}'" if cta.nil?

  k = "admin_cta_choice"
  v = cta.id

  case Capybara.current_session.driver
  when Capybara::Poltergeist::Driver
    page.driver.set_cookie(k,v)
  when Capybara::RackTest::Driver
    headers = {}
    Rack::Utils.set_cookie_header!(headers,k,v)
    cookie_string = headers['Set-Cookie']
    Capybara.current_session.driver.browser.set_cookie(cookie_string)
  when Capybara::Selenium::Driver
    page.driver.browser.manage.add_cookie(:name=>k, :value=>v)
  else
    raise "no cookie-setter implemented for driver #{Capybara.current_session.driver.class.name}"
  end
end
+4

? @selenium , .:)

+1

Capybara API cookie.

Capyabara - . , cookie .

To see this in action, just look at an example Rails application .

0
source

Source: https://habr.com/ru/post/1774771/


All Articles