Skip to content

Instantly share code, notes, and snippets.

@mars
Created October 13, 2013 01:58
Show Gist options
  • Save mars/6957187 to your computer and use it in GitHub Desktop.
Save mars/6957187 to your computer and use it in GitHub Desktop.
Set window size for Capybara/Selenium/chromedriver
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app,
browser: :chrome,
desired_capabilities: {
"chromeOptions" => {
"args" => %w{ window-size=1024,768 }
}
}
)
end
@crespire
Copy link

crespire commented Sep 14, 2023

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