Created
October 13, 2013 01:58
-
-
Save mars/6957187 to your computer and use it in GitHub Desktop.
Set window size for Capybara/Selenium/chromedriver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Capybara.register_driver :chrome do |app| | |
Capybara::Selenium::Driver.new(app, | |
browser: :chrome, | |
desired_capabilities: { | |
"chromeOptions" => { | |
"args" => %w{ window-size=1024,768 } | |
} | |
} | |
) | |
end |
Also found working solution in actionpack
def register_selenium(app)
Capybara::Selenium::Driver.new(app, **{ browser: @browser.type }.merge(browser_options)).tap do |driver|
driver.browser.manage.window.size = Selenium::WebDriver::Dimension.new(*@screen_size)
end
end
2023 checking in here, here's what worked for me:
Capybara.register_driver :selenium_chrome do |app|
options = Selenium::WebDriver::Chrome::Options.new
%w[
incognito
disable-extensions
auto-open-devtools-for-tabs
window-size=1920,1080
].each { options.add_argument _1 }
%w[
headless
disable-gpu
].each { options.add_argument _1 } if %w[1 on true].include?(ENV['HEADLESS'])
Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
end
Capybara.default_driver = :selenium_chrome
Capybara.javascript_driver = :selenium_chrome
2023 checking in here, here's what worked for me:
Capybara.register_driver :selenium_chrome do |app| options = Selenium::WebDriver::Chrome::Options.new %w[ incognito disable-extensions auto-open-devtools-for-tabs window-size=1920,1080 ].each { options.add_argument _1 } %w[ headless disable-gpu ].each { options.add_argument _1 } if %w[1 on true].include?(ENV['HEADLESS']) Capybara::Selenium::Driver.new(app, browser: :chrome, options: options) end Capybara.default_driver = :selenium_chrome Capybara.javascript_driver = :selenium_chrome
I was not able to get this to work for me with the HEADLESS=true bundle exec rspec
syntax. I ended up registering two drivers :selenium_chrome
and :selenium_chrome_headless
and using a separate selenium.rb
:
# spec/support/selenium.rb
RSpec.configure do |config|
config.before(:each) do
if ENV.fetch('HEADLESS', false)
driven_by :selenium_chrome_headless
else
driven_by :selenium_chrome
end
end
end
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This works best in 2019:
This is basically the code from the gem itself with some tweaks added:
https://github.com/teamcapybara/capybara/blob/master/lib/capybara/registrations/drivers.rb
The additional option/flags work for me but you probebly want to checkout what you would need here first:
https://peter.sh/experiments/chromium-command-line-switches/
KG