【问题标题】:Capybara & Cucumber | Getting cookies水豚和黄瓜 |获取 cookie
【发布时间】:2019-05-31 23:01:09
【问题描述】:

我正在尝试在 Cucumber 步骤中获取 cookie 值:

步骤定义

When /^I log in$/ do
  # code to log in
end

Then /^cookies should be set$/ do
  cookies[:author].should_not be_nil
end

控制器

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

但它不起作用:

结果

expected: not nil
     got: nil

在 RSpec 示例中,一切正常:

控制器规范

require 'spec_helper'

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

如何使用 Capybara 在 Cucumber 步骤中获取 cookie 值?

谢谢。

Debian GNU/Linux 6.0.4;

红宝石 1.9.3;

Ruby on Rails 3.2.1;

黄瓜 1.1.4;

Cucumber-Rails 1.2.1;

水豚1.1.2;

机架测试 0.6.1。

【问题讨论】:

    标签: ruby cucumber capybara


    【解决方案1】:

    步骤定义

    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
    

    这可行,但是,我仍在寻找一种不那么冗长的方法。此外,我想知道如何在步骤定义中获取会话数据。

    更新

    要获取会话数据,应该执行以下操作:

    步骤定义

    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
    

    这也很冗长。

    【讨论】:

    【解决方案2】:

    Selenium API 似乎发生了变化。建议的解决方案不起作用,但我花了一些时间环顾四周后,找到了解决方案。

    保存 cookie:

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

    读取 cookie:

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

    cookie_named 返回一个由“name”和“value”组成的数组,所以我们需要一个额外的引用来提取cookie值。

    【讨论】:

      【解决方案3】:

      试试show_me_the_cookies gem。
      https://github.com/nruth/show_me_the_cookies

      【讨论】:

        【解决方案4】:

        这是我的步骤定义代码:

        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
        

        以下是测试失败时的输出示例:

        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"'
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-06-11
          • 2014-02-24
          • 1970-01-01
          • 2012-07-06
          • 2012-01-18
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多