Posted about 7 years ago. Visible to the public. Repeats.

How to: Start Selenium browser maximized or with custom window size

If your application requires a minimum window size (like 1024 pixels wide) and/or you have tests failing because elements are out of reach and can't be clicked, you may want to set a specific size for your browser window.

Put the following code somewhere inside features/support (e.g. env.rb) to resize your browser window for any scenario tagged @javascript.

Before '@javascript' do page.driver.browser.manage.window.resize_to(1024, 768) end

If this does not work, try using Capybara.current_session instead of page:

Before '@javascript' do window = Capybara.current_session.driver.browser.manage.window window.resize_to(1024, 768) # width, height end

In case you want to maximize your browser window, use maximize instead of resize_to:

Before '@javascript' do page.driver.browser.manage.window.maximize end

However, you should always prefer setting fixed window dimensions instead maximizing since "maximized" may be something different for another developer.

Legacy: If the above does not work

The following has worked on Capybara 1.1.2. It will resize the browser window only when it first appears:

module Capybara module Selenium Driver.class_eval do def browser_with_resize browser_was_uninitialized = @browser.nil? browser_without_resize # If the browser window was just opened, we resize it: execute_script("window.resizeTo(1024, screen.height)") if browser_was_uninitialized @browser end alias_method_chain :browser, :resize end end end

Put that into a file like features/support/start_resized.rb and run your tests.\
When the browser window opens up, it should immediately resize to 1024 pixels width now.

Note that if you want the window to be maximized, just call resizeTo(screen.width, screen.height) in the JavaScript. That will make the browser as big as your screen, which is almost as if it's maximized.

This might also fix errors like unexpected '#' after 'DESCENDANT_SELECTOR' (Nokogiri::CSS::SyntaxError)

Once an application no longer requires constant development, it needs periodic maintenance for stable and secure operation. makandra offers monthly maintenance contracts that let you focus on your business while we make sure the lights stay on.

Owner of this card:

Arne Hartherz
Last edit:
over 4 years ago
by Henning Koch
startup, resize, maximize, firefox, chrome
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Arne Hartherz to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more