微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将自定义标题从水豚传递到Selenium

如何解决将自定义标题从水豚传递到Selenium

|| 我们使用自定义标头来验证我们的Web应用程序。 http代理服务拦截请求,确认用户的真实性,然后将自定义标头注入请求中。 为了测试应用程序,我需要在请求击中ApplicationController方法之前将这些标头写入请求。现在,当前的hack适用于我所有的非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东西都已设置。 我应该在哪里注入硒的自定义标头以将其提取? 谢谢!     

解决方法

在我们的应用程序之前,我们正在使用额外的机架中间件(用于测试环境)。在那里,您可以对env /请求执行任何操作,并且对应用程序和所有驱动程序完全透明。     ,您可以尝试创建新的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修改标头。     

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。