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

查看:31
本文介绍了从 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 方法之前将这些标头写入请求中.现在,当前的 hack 适用于我所有的非 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.

我应该在哪里注入 selenium 的自定义标头以获取它们?

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天全站免登陆