capybara poltergeist - 覆盖用户代理

时间:2012-11-23 09:53:34

标签: ruby-on-rails ruby ruby-on-rails-3 rubygems gem

有人知道如何将capybara poltergeist的用户代理覆盖到移动设备进行测试吗?

我发现了为selenium webdriver配置的内容: http://blog.plataformatec.com.br/2011/03/configuring-user-agents-with-capybara-selenium-webdriver/

在水豚顽固分子中如何做到这一点?

2 个答案:

答案 0 :(得分:5)

在poltergeist github页面上看到该链接: https://github.com/teampoltergeist/poltergeist#manipulating-request-headers

答案 1 :(得分:5)

如果您不想在每个规范上设置用户代理,请在spec_helper.rb中尝试此操作

Capybara.register_driver :poltergeist_iphone_debug do |app|
  ghost_busters = Capybara::Poltergeist::Driver.new(app, inspector: true)
  ghost_busters.headers = { 'User-Agent' => 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9A334 Safari/7534.48.3' }
  ghost_busters
end

Capybara.default_driver = :rack_test
Capybara.javascript_driver = :poltergeist

RSpec.configure do |config|

  config.treat_symbols_as_metadata_keys_with_true_values = true
  config.before (:each) do |example_method|
    example = example_method.example ## RSpec2 | For RSpec3: example = example_method

    if example.metadata[:mobile]
      Capybara.current_driver = :poltergeist_iphone_debug
    end

  end
end

然后在您的功能规范中,只需执行此操作:

require 'spec_helper'

describe 'Home' do
  describe 'GET /' do

    # Default request desktop
    it 'does not have mobile menu' do
      visit root_path
      expect(page).to_not have_selector('.mobile-menu')
    end

    # Override request mobile
    it 'has mobile menu', :mobile do
      visit root_path
      expect(page).to have_selector('.mobile-menu')
    end
  end
end