将自定义标头传递给来自Capybara的Selenium [英] Passing Custom Headers to Selenium from Capybara

查看:207
本文介绍了将自定义标头传递给来自Capybara的Selenium的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用自订标头来验证我们的网路应用程式。 http代理服务拦截请求,确认用户的真实性,然后将自定义头部注入到请求中。

We use custom headers to authenticate our web apps. An http proxy service intercepts requests, confirms the authenticity of the user and then injects custom headers into the request.

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

In order to test the app, I need to write those headers into the request before it hits my ApplicationController methods. Right now, the current hack works for all my non-javascript tests:

# 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驱动程序:

any request I don't want to be authenticated, I can just use a tag to use the regular rack_test driver:

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的东西已经设置。

when I use the selenium browser, a similar method of redefining env on the driver does not seem to be effective. I assume this is because the app is spooled up in the java thread and all the Rack stuff is already setup.

我应该在哪里注入自定义标题以便接收?

Where should I inject the custom headers for selenium to pick them up?

谢谢!

推荐答案

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

We are using an extra piece of rack middleware (for the test environment) before our application. There you can do anything to the env / request and its completely transparent for the application and all drivers.

这篇关于将自定义标头传递给来自Capybara的Selenium的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆