2012-02-10 11 views
6

Tôi đang cố gắng để có được giá trị cookie trong bước Dưa chuột:Capybara & Cucumber | Bắt cookie

Bước định nghĩa

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

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

khiển

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

Nhưng nó không hoạt động:

Kết quả

expected: not nil 
    got: nil 

Thú vị, rằng trong các ví dụ RSpec mọi công việc tốt:

khiển Spec

require 'spec_helper' 

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

Làm thế nào tôi có thể nhận được giá trị cookie trong các bước dưa chuột sử dụng Capybara?

Cảm ơn.

Debian GNU/Linux 6.0.4;

Ruby 1.9.3;

Ruby on Rails 3.2.1;

Dưa chuột 1.1.4;

dưa chuột-Rails 1.2.1;

Capybara 1.1.2;

Rack-Test 0.6.1.

Trả lời

8

Bước Định nghĩa

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 

này hoạt động, tuy nhiên, tôi vẫn đang tìm kiếm một cách ít tiết. Hơn nữa, tôi muốn biết cách lấy dữ liệu phiên trong định nghĩa bước.

Cập nhật

Để có được dữ liệu phiên ta nên làm như sau:

Bước Định nghĩa

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 

này là khá dài dòng quá.

+0

Bạn có thể trích xuất dưới dạng phương thức tùy chỉnh như được giải thích ví dụ: https://github.com/cucumber/cucumber/wiki/Cucumber-Backgrounder#steps-within-steps--an-anti-pattern (về phía cuối của phần). – juanrpozo

2

Đây là mã của tôi về bước defenition:

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 

Dưới đây là ví dụ về đầu ra khi thử nghiệm thất bại:

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

Dường như Selenium API đã thay đổi. Các giải pháp đề xuất đã không làm việc, nhưng tôi sau khi dành một chút thời gian nhìn xung quanh, tôi tìm thấy một giải pháp.

Để lưu một cookie:

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

Để đọc một cookie:

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

cookie_named trả về một mảng gồm "Tên" và "giá trị", vì vậy chúng ta cần một tài liệu tham khảo thêm để trích xuất giá trị cookie.