isotope|eleven


About_adam_gamble

Use different user agents per cucumber scenario

by: Adam Gamble

March 12th, 2012 15:44

So I needed to test a redirect that only happened for IOS devices. I had no idea how to accomplish this with a specific cucumber scenario. So I did what any good developer would do. I figured it out.

Here is how:

make a new file features/support/custom_user_agents.rb

Capybara.register_driver :iphone do |app|

  require 'selenium/webdriver'
  profile = Selenium::WebDriver::Firefox::Profile.new

  #Change the line below to change the user agent
  profile['general.useragent.override'] = "Mozilla/5.0 (iPhone; U; CPU
  like Mac OS X; en)       AppleWebKit/420+ (KHTML, like Gecko) Version/3.0
  Mobile/1A535b Safari/419.3"

  Capybara::Selenium::Driver.new(app, :profile => profile)
end

Capybara.use_default_driver

then tag your scenario with @iphone and it will automatically use the new selenium driver we just created! Other scenarios will still use the default driver. Hope this helps save someone some time.

Tags:
Adam has been a professional developer for almost 10 years. Adam is lead developer on projects for 2 different fortune 500 companies. In his spare time he's writing "Beginning Rails 4" which is published by Apress and will be released in October 2013.

View All Posts