Skip to content

Instantly share code, notes, and snippets.

@sidane
Last active June 3, 2022 13:27
Show Gist options
  • Select an option

  • Save sidane/2204218 to your computer and use it in GitHub Desktop.

Select an option

Save sidane/2204218 to your computer and use it in GitHub Desktop.
Resize browser window with Capybara Selenium
# Resize selenium browser window to avoid Selenium::WebDriver::Error::MoveTargetOutOfBoundsError errors
#
# Example usage with Rspec (in spec/support/spec_helper.rb):
#
# config.before(:each) do
# set_selenium_window_size(1250, 800) if Capybara.current_driver == :selenium
# end
#
def set_selenium_window_size(width, height)
window = Capybara.current_session.current_window.resize_to
window.resize_to(width, height)
end
@iainbeeston

Copy link
Copy Markdown

Useful, thanks!

@beydogan

beydogan commented Oct 6, 2015

Copy link
Copy Markdown

Excellent! Thank you

@roc

roc commented Apr 15, 2016

Copy link
Copy Markdown

๐Ÿ‘

@rattrayalex

Copy link
Copy Markdown

this appears to be deprecated in favor of:
Capybara.current_session.current_window.resize_to

@francois-tilkee

Copy link
Copy Markdown

๐Ÿ‘

@sidane

sidane commented Oct 11, 2017

Copy link
Copy Markdown
Author

Gist updated to use Capybara.current_session.current_window.resize_to. Thanks @rattrayalex.

@nroose

nroose commented Jan 30, 2018

Copy link
Copy Markdown

Thanks, but the update kinda breaks it. Either do it in one line, or take the resize_to off of the first line. Thanks, though!!!

@sroller

sroller commented Oct 12, 2018

Copy link
Copy Markdown

doesn't work with capybara 3.90
I get the error

C:/Ruby25-x64/lib/ruby/gems/2.5.0/gems/capybara-3.9.0/lib/capybara/rack_test/driver.rb:18:in `initialize': rack-test requires a rack application, but none was given (ArgumentError)

@pertamaks

Copy link
Copy Markdown
opt_fox = Selenium::WebDriver::Firefox::Options.new
opt_fox.add_argument('--width=1024') #your preferred size
opt_fox.add_argument('--height=700') #your preferred size

Capybara.register_driver :firefox do |app|
Capybara::Selenium::Driver.new(app, browser: :firefox, options: opt_fox)
end

For firefox,
Will leave it here, maybe someone in need too,
or, maybe someone can make it better,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment