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
env.rb) to resize your browser window for any scenario tagged
If this does not work, try using
Capybara.current_session instead of
In case you want to maximize your browser window, use
maximize instead of
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
This might also fix errors like
unexpected '#' after 'DESCENDANT_SELECTOR' (Nokogiri::CSS::SyntaxError)