Capybara & Cucumber | Receiving Cookies

I am trying to get cookie values ​​in the Cucumber step:

Step definitions

When /^I log in$/ do # code to log in end Then /^cookies should be set$/ do cookies[:author].should_not be_nil end 

controller

 class SessionsController < ApplicationController def create cookies[:author] = 'me' redirect_to authors_path end end 

But this does not work:

Result

 expected: not nil got: nil 

Interestingly, in the RSpec examples, everything works just fine:

Controller specification

 require 'spec_helper' describe SessionsController do describe 'create' do it 'sets cookies' do post :create cookies[:author].should_not be_nil end end end 

How can I get cookie values ​​in Cucumber steps using Capybara?

Thanks.

Debian GNU / Linux 6.0.4;

Ruby 1.9.3;

Ruby on Rails 3.2.1;

Cucumber 1.1.4;

Cucumber-Rails 1.2.1;

Capybara 1.1.2;

Rack-Test 0.6.1.

+6
source share
4 answers

Step definitions

 Then /^cookies should be set^/ do Capybara .current_session # Capybara::Session .driver # Capybara::RackTest::Driver .request # Rack::Request .cookies # { "author" => "me" } .[]('author').should_not be_nil end 

This works, however, I'm still looking for a less detailed way. Moreover, I would like to know how to get the session data in the step definition.

Update

To get session data, you must do the following:

Step definitions

 Then /^session data should be set$/ do cookies = Capybara .current_session .driver .request .cookies session_key = Rails .application .config .session_options .fetch(:key) session_data = Marshal.load(Base64.decode64(cookies.fetch(session_key))) session_data['author'].should_not be_nil end 

This is also quite a lot.

+8
source

Selenium API seems to have changed. The proposed solution did not work, but after spending a little time looking around, I found a solution.

To save a cookie:

 browser = Capybara.current_session.driver.browser.manage browser.add_cookie :name => key, :value => val 

To read the cookie:

 browser = Capybara.current_session.driver.browser.manage browser.cookie_named(key)[:value] 

cookie_named returns an array consisting of "name" and "value", so we need an additional link to retrieve the cookie value.

+5
source

Here is my step defragmentation code:

 Then /^cookie "([^\"]*)" should be like "([^\"]*)"$/ do |cookie, value| cookie_value = Capybara.current_session.driver.request.cookies.[](cookie) if cookie_value.respond_to? :should cookie_value.should =~ /#{value}/ else assert cookie_value =~ /#{value}/ end end 

Here is an example output when the test fails:

 expected: /change/ got: "/edit" (using =~) Diff: @@ -1,2 +1,2 @@ -/change/ +"/edit" (RSpec::Expectations::ExpectationNotMetError) ./features/step_definitions/web_steps.rb:244:in `/^cookie "([^\"]*)" should be like "([^\"]*)"$/' features/auth.feature:57:in `And cookie "go" should be like "change"' 
+2
source

Try the show_me_the_cookies stone.
https://github.com/nruth/show_me_the_cookies

+1
source

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


All Articles