Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

capybara poltergeist - override user agent

Does somebody know how I could override the user agent of capybara poltergeist to a mobile one for testing?

I found something about configuring that for the selenium webdriver: http://blog.plataformatec.com.br/2011/03/configuring-user-agents-with-capybara-selenium-webdriver/

How this is possible in capybara poltergeist?

like image 430
Matthias Avatar asked Nov 23 '12 09:11

Matthias


2 Answers

see that link on the poltergeist github page: https://github.com/teampoltergeist/poltergeist#manipulating-request-headers

like image 155
Betty St Avatar answered Sep 26 '22 03:09

Betty St


If you don't want to set the user-agent on every spec, try this in your 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

And then in your feature spec, just do this:

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
like image 24
Paul Pettengill Avatar answered Sep 25 '22 03:09

Paul Pettengill