我們使用自定義標頭來驗證我們的網絡應用程序。 http代理服務攔截請求,確認用戶的真實性,然後將自定義標頭注入請求。從水豚傳遞自定義頭到硒
爲了測試應用程序,我需要將這些頭寫入到請求中,然後再打到我的ApplicationController方法。眼下,目前黑客的作品爲我所有的非JavaScript測試:
# in hooks.rb
Before do
require 'capybara/driver/rack_test_driver'
module Capybara::Driver
class Authentic < RackTest
def env
super.merge('My-Custom-Header' => "foo")
end
end
end
Capybara.register_driver(:authentic) do |app|
Capybara::Driver::Authentic.new(app)
end
Capybara.default_driver = :authentic
end
#in the controller
class ApplicationController < ActionController::Base
before_filter :authenticate
def authenticate
render :file => File.join(Rails.root, "public", "403.html") unless request.env.keys.include?('foo')
end
end
我不希望被認證的任何要求,我可以只使用一個標籤來使用常規rack_test司機:
Feature: Authentication
Valid users should be authenticated.
Invalid users should be redirected to the login page.
Scenario: Authorized
Given I am on the homepage
Then I should see "Create a New Research Project"
@rack_test
Scenario: Unauthorized
Given I go to the homepage
Then I should see "You are not authorized to view this page."
當我使用硒瀏覽器時,類似的重新定義驅動程序env的方法似乎並不奏效。我認爲這是因爲該應用程序在java線程中被後臺處理,並且所有Rack的東西已經安裝完畢。
我應該在哪裏注入硒的自定義標題來提取它們?
謝謝!
這是我採取的方法。與此類似:http://pastie.org/1807957 – 2011-04-18 17:31:32