2011-04-14 58 views
1

我們使用自定義標頭來驗證我們的網絡應用程序。 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的東西已經安裝完畢。

我應該在哪裏注入硒的自定義標題來提取它們?

謝謝!

回答

1

在我們的應用程序之前,我們正在使用額外的機架中間件(用於測試環境)。在那裏你可以對env/request做任何事情,並且它對應用程序和所有驅動程序都是完全透明的。

+0

這是我採取的方法。與此類似:http://pastie.org/1807957 – 2011-04-18 17:31:32

3

您可以嘗試創建一個新的Selenium驅動程序,一個新的配置文件並覆蓋此新配置文件上的標題。我必須做類似的事情來欺騙不同的用戶代理。

Capybara.register_driver :mobile_browser do |app| 
    require 'selenium-webdriver' 

    profile = Selenium::WebDriver::Firefox::Profile.new 
    profile['general.useragent.override'] = "Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7" 

    Capybara::Driver::Selenium.new(app, {:browser => :firefox, :profile => profile}) 
end 

我不知道,如果你可以,但是,修改標題時的步驟正在運行 - 我敢肯定,你可以使用硒API。