Run Chrome in a specific resolution or user agent with Selenium

Updated . Posted . Visible to the public.

When you want to test how an web-application reacts in a specific resolution, you can set up a specific Selenium driver for some tests:

 Before('@chrome320x480') do
     Capybara.current_driver = :chrome320x480
 end

 After('@chrome320x480') do
    Capybara.use_default_driver 
 end

You can use either chromium or chrome beta (as of 2012.05 the Version "19.0.1084.41 beta" works), or any other member of the family. It only needs to supports the "--window-size" command-line switch. See this list Show archive.org snapshot for an inspiration of other switches that might also work. Also, you need the chromedriver Show archive.org snapshot somewhere in your path. You can also set a special binary Show archive.org snapshot you only use for theese tests.

You set up the driver in a file like this

 # features/support/chrome.rb
 Capybara.register_driver :chrome320x480 do |app|
   args = []
   args << "--window-size=320,480"
   # you can also set the user agent 
   # args << "--user-agent='Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3'"
   Capybara::Selenium::Driver.new(app, :browser => :chrome, :args => args)
 end

With all that in place, you can switch between drivers using tags:

 Feature: The app works with low resolutions

 @javascript
 @chrome320x480
 Scenario: Visit my profile in low res
   Given "bart" is a user with the email "bart@simpson.com" and the password "secret"
   When I sign in as "bart" 
   Then I should bee on the home page
     And "logout" should be reachable
Last edit
Dominik Schöler
License
Source code in this card is licensed under the MIT License.
Posted by Lexy to makandra dev (2012-05-10 08:21)