将自定义标题传递给Capybara的Selenium

我们使用自定义标头来validation我们的网络应用。 http代理服务拦截请求,确认用户的真实性,然后将自定义标头注入请求。

为了测试应用程序,我需要在请求到达ApplicationController方法之前将这些标头写入请求。 现在,当前的hack适用于我所有的非JavaScript测试:

# in hooks.rb Before do require 'capybara/driver/rack_test_driver' module Capybara::Driver class Authentic  "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  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." 

当我使用selenium浏览器时,在驱动程序上重新定义env的类似方法似乎并不有效。 我认为这是因为应用程序在java线程中被假脱机,并且所有Rack内容已经设置好了。

我应该在哪里注入selenium的自定义标头来接它们?

谢谢!

在我们申请之前,我们正在使用额外的机架中间件(用于测试环境)。 在那里你可以对env / request做任何事情,它对应用程序和所有驱动程序都是完全透明的。

您可以尝试创建一个新的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 

我不确定你是否可以在步骤运行时修改标题 – 我相信你可以使用Selenium API。