Rails + Cucumber/Capybara: How to set/retrieve cookies in tests?

前端 未结 3 1783
你的背包
你的背包 2021-02-12 11:48

I\'m implementing a lazy login feature. My cucumber feature should describe it:

    Feature: User log in

        Scenario: Lazy login
            Given I didn\'         


        
相关标签:
3条回答
  • 2021-02-12 12:13

    Use https://github.com/nruth/show_me_the_cookies which wraps the driver methods. It has methods for getting cookies, deleting cookies, and a method for creating cookies called create_cookie.

    0 讨论(0)
  • 2021-02-12 12:16

    I needed just to test the cookie values

    Inspiration taken from https://collectiveidea.com/blog/archives/2012/01/05/capybara-cucumber-and-how-the-cookie-crumbles

    and ported to Rails 5.x

    Create features/support/cookies.rb

    With content

    module Capybara
      class Session
        def cookies
          @cookies ||= ActionDispatch::Request.new(Rails.application.env_config.deep_dup).cookie_jar
        end
      end
    end
    
    Before do
      allow_any_instance_of(ActionDispatch::Request).to receive(:cookie_jar).and_return(page.cookies)
      allow_any_instance_of(ActionDispatch::Request).to receive(:cookies).and_return(page.cookies)
    end
    

    Then the step for testing

    Then('is set cookie {string} with value {string}') do |cookie, value|
      expect(page.cookies.signed[cookie]).to eq value
    end
    
    0 讨论(0)
  • 2021-02-12 12:20

    So eventually I figured it out after trying a lot of different things.

    Given(/^I didn't log out the last time I was on the site$/) do
      user = FactoryGirl.create(:user)
      visit new_user_session_path
      fill_in('user[email]', with: user.email)
      fill_in('user[password]', with: 'test123')
      click_button('Sign in')
    
      Capybara.current_session.driver.request.cookies.[]('auth_token').should_not be_nil
      auth_token_value = Capybara.current_session.driver.request.cookies.[]('auth_token')
      Capybara.reset_sessions!
      page.driver.browser.set_cookie("auth_token=#{auth_token_value}")
    end
    
    When(/^I go to the homepage$/) do
      visit root_path
    end
    
    Then(/^I should automatically be logged in$/) do
      page.should have_content("Logout")
    end
    

    UPDATE:

    Here's what I use in case I'm using Selenium for some of the tests:

    if Capybara.current_session.driver.class == Capybara::Selenium::Driver
      auth_token = page.driver.browser.manage.cookie_named('auth_token')[:value]
      page.driver.browser.manage.delete_all_cookies
      page.driver.browser.manage.add_cookie(:name => "auth_token", :value => auth_token)
    else
      puts "cookies = #{Capybara.current_session.driver.request.cookies}"
      Capybara.current_session.driver.request.cookies.[]('auth_token').should_not be_nil
      auth_token_value = Capybara.current_session.driver.request.cookies.[]('auth_token')
      Capybara.reset_sessions!
      page.driver.browser.set_cookie("auth_token=#{auth_token_value}")
    end
    
    0 讨论(0)
自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题