Introducing webdriver-user-agent gem: a quick way to run ruby webdriver tests emulating mobile devices

Update: As @David points out below, Chrome window resizing does not seem to work at all on Windows. I am not sure why but I will update this blog if I fix this issue.

After chatting to Jari Bakken at the Test Automation Bazaar in Austin, I wrote and released the webdriver-user-agent gem.

I’m not convinced that running your automated functional tests on real or emulated mobile devices will yeild the appropriate defects for effort involved, so this gem makes it simple to run yourwatir-webdriver or selenium-webdriver tests on real browsers (:firefox and :chrome) but using mobile device user agents and resolutions, so your app thinks they’re a mobile device.


Add this line to your application’s Gemfile:

gem 'webdriver-user-agent'

And then execute:

$ bundle

Or install it yourself as:

$ gem install webdriver-user-agent


An example script in selenium-webdriver:

require 'selenium-webdriver'
require 'webdriver-user-agent'
driver = UserAgent.driver(:browser => :chrome, :agent => :iphone, :orientation => :landscape)
driver.get ''
driver.current_url.should == ''

and another example script in watir-webdriver:

require 'watir-webdriver'
require 'webdriver-user-agent'
driver = UserAgent.driver(:browser => :chrome, :agent => :iphone, :orientation => :landscape)
browser = driver
browser.goto ''
browser.url.should == '' 

The available options are:

  • :browser
    • :firefox (default)
    • :chrome
  • :agent
    • :iphone (default)
    • :ipad
    • :android_phone
    • :android_tablet
  • :orientation
    • :portrait (default)
    • :landscape

This is what the screenshots look like: in iPhone portrait in iPad landscape


I think this will be a useful gem for those wanting to test mobile device functionality without having to set up complex infrastructure.

Author: Alister Scott

Alister is an Excellence Wrangler for Automattic.

21 thoughts on “Introducing webdriver-user-agent gem: a quick way to run ruby webdriver tests emulating mobile devices”

  1. I completely agree. Convio has been using the same technique to test the mobile interfaces to our products.


  2. This is a great solution but I’m having trouble with the resizing of chrome.

    FF seems to resize to the appropriate size but chrome won’t. I had the same problem using the driver.manage.window.resize_to method.

    Any advice? We want to use chrome since it’s a webkit based rendering engine.


    1. Are you having trouble resizing its width?
      I noticed that Chrome won’t resize manually smaller than a certain width and can’t work out a way to do it, I think it’s a Chrome constraint rather than anything to do with webdriver unfortunately. Sorry.


  3. Thanks for the response. I couldn’t modify height or width.

    The chrome windows api doesn’t appear to be available to watir-webdriver. A javascript resizeTo doesn’t work either. I can pop a window in the desired size but had difficulty making watir use it.

    Here’s the griping about it. A recent set of changes, I think, fixed it for firefox but not chrome

    I was hoping this gem hit on a clever solution to it, though it’s still a very nice tool to have.


    1. I’m definitely resizing Chrome. What platform are you on?

      Can you clone the repo and run the specs? There’s a Chrome spec that should fail if if doesn’t resize for you.


      1. Windows XP running ruby 1.8.7 with current watir-webdriver and rspec. Latest version of chrome.

        I’ll see if I can run the specs in the next day or so. Thanks for helping!


        1. Finally had time to try it out on a Windows machine.
          You’re right: Chrome does not resize, which is odd as it works fine on Mac OSX.
          Will look into it further when I have time.


  4. Hi Alister,

    This is exactly what i’ve been looking for, for quite some time actually. Especially that I can switch to landscape – so hats off to you :)

    However I have one question…can this gem be used in conjunction with Capybara? (apologies if this is a schoolboy question, i’m quite new to this).

    I cant seem to get this working:
    Capybara.register_driver :selenium do |app|
    job_name = “Javascript tests for iPhone – #{ ‘%Y-%m-%d %H:%M’}”
    puts “Running #{job_name}”
    require ‘selenium/webdriver’
    require ‘webdriver-user-agent’
    profile =,
    :profile => profile,
    :agent => :iphone,
    :orientation => :landscape)

    Does this look OK to you? :-s


  5. Has anyone had any success in running this using a different browser profile with firefox? (ie no flash installed). We have tried various attempts at adding it into the above watir-webdriver example but to no avail. We keep creating a brand new browser instance…

    We would greatly appreciate any suggestions.



    1. So are you specifying an existing non-flash profile and trying to use that?
      Or are you building a non-flash profile in code and trying to use that?
      Some example code would help.
      I imagine it’d be easy to disable flash using a profile string passed into




      1. Hi Alister,

        We have attempted to disable it through our default firefox profile, but a new
        instance of firefox is created with flash enabled.

        The following is in our hooks file for creating the browser instance.

        profile =
        profile.assume_untrusted_certificate_issuer = false
        ORIENTATION = ENV[“LAYOUT”] ||= ‘landscape’
        driver = UserAgent.driver(:browser => :firefox, :agent =>
        “#{DEVICE.downcase}”, :orientation => “#{ORIENTATION.downcase}”)
        browser = driver

        We just dont seem to be able to link it in successfully



        1. I think device and orientation need to be symbols.
          So adding a .to_sym to your code should work

          profile =
          profile.assume_untrusted_certificate_issuer = false
          DEVICE= ENV["DEVICE"]
          ORIENTATION = ENV["LAYOUT"] ||= ‘landscape’
          driver = UserAgent.driver(:browser => :firefox, :agent =>
          “#{DEVICE.downcase}”.to_sym, :orientation => “#{ORIENTATION.downcase}”.to_sym)
          browser = driver 


  6. Hi Alister

    I am trying to use the user agent driver but when I initiate the driver as below, i do not get the resolution of expected device. Have you seen this issue earlier?

    driver = UserAgent.driver(:browser => :firefox, :agent => :android_tablet.to_sym, :orientation => :portrait.to_sym, :profile=>profile) driver

    Any help is highly appreciated.


  7. Hi
    I am using webdriver-user-agent mentioned here –
    This is the code we are using when trying out.
    We are running this on Chrome/FF

    http_client =
    http_client.timeout = HTTP_TIMEOUT
    profile =
    device = ENV[“DEVICE”]
    orientation = ENV[“ORIENTATION”]
    driver = UserAgent.driver(:browser => :firefox, :agent =>device, :orientation=>orientation)
    devices = UserAgent.resolution_for(device,orientation)
    UserAgent.resize_inner_window(driver,devices[0],devices[1]) driver

    Now when the last statement is executed, i get the following error

    (STEP) Launching FIREFOX (using web driver user agent)……
    undefined method `to_sym’ for # (NoMethodError)
    /Users/rambighananthan/.rvm/gems/ruby-1.9.3-p194/gems/watir-4.0.2/lib/watir/loader.rb:42:in `load_driver_for’
    /Users/rambighananthan/.rvm/gems/ruby-1.9.3-p194/gems/watir-4.0.2/lib/watir/loader.rb:8:in `new’

    Based on some investigation, problem is happening at highlighted line below as its trying to .to_sym on the selenium webdriver object.

    def load_driver_for(browser)

    if browser && browser.to_sym != :ie && Watir.driver == :classic
    Watir.driver = :webdriver

    But if we add a line like highlighted below

    def load_driver_for(browser)

    if “#{ENV[“BROWSER”]}”.eql?(“chrome_useragent”)||”#{ENV[“BROWSER”]}”.eql?(“firefox_useragent”)
    Watir.driver = :webdriver
    if browser && browser.to_sym != :ie && Watir.driver == :classic
    Watir.driver = :webdriver

    since this is watir code outside of our framework, this is not the right way to do this, any suggestion on how to avoid this situation ?
    using watir 4.0.2 / selenium-webdriver 2.30.0



  8. I am also interested to know if we can override the user agent string. We are using water web driver. The problem I am facing is that the User agent string from Ipad provided in devices.yaml has “iphone” string in it and the platform code of my company thinks it is a mobile device.

    I will like to pass user agent string and not the device name in the line below, is that possible?
    driver = UserAgent.driver(:browser => :firefox, :agent =>
    “#{DEVICE.downcase}”.to_sym, :orientation => “#{ORIENTATION.downcase}”.to_sym)


Comments are closed.