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
@benlieb
Copy link

benlieb commented Oct 17, 2016

I had to do:

Capybara.register_driver :selenium do |app|

For this to work

@KacperPucek
Copy link

Worked like a charm, thanks :)

@mariusandra
Copy link

mariusandra commented Feb 24, 2017

This didn't work for me, but this did:

Capybara.register_driver :chrome do |app|
  Capybara::Selenium::Driver.new(app,
    browser: :chrome,
    args: ["--window-size=1024,768"]
  )
end

@harokevin
Copy link

Capybara.register_driver :chrome do |app|
  Capybara::Selenium::Driver.new(app,
    browser: :chrome,
    args: ["--window-size=1024,768"]
  )
end

worked for me

@rounders
Copy link

rounders commented Oct 5, 2017

with selenium-webdriver 3.6.0 the following did the trick for me:

Capybara.register_driver :chrome do |app|
  options = Selenium::WebDriver::Chrome::Options.new
  options.add_argument("--window-size=1024,768")

  Capybara::Selenium::Driver.new(app,
    browser: :chrome,
    options: options 
  )
end

@paneq
Copy link

paneq commented Mar 2, 2018

Thank you @rounders :)

@seanlinsley
Copy link

@rounders' solution worked for me as well

@gayathri-sapaad
Copy link

desired_capabilities: {
      "chromeOptions" => {
        "args" => %w{ window-size=1024,768 }
      }
    }

This worked for me
capybara (>= 2.15)
chromedriver-helper (2.1.0)

Thanks @mars

@vivipoit
Copy link

I wanted headless and this is what I did:

Capybara.register_driver :selenium_chrome_headless do |app|
  capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(
    chromeOptions: { args: %w(headless disable-gpu window-size=2500,2500) }
  )
  Capybara::Selenium::Driver.new app, browser: :chrome, desired_capabilities: capabilities
end

Capybara.javascript_driver = :selenium_chrome_headless

I wanted to drop it here, since all of these entries helped me understand what I might need to get to my setup.

@khalilgharbaoui
Copy link

khalilgharbaoui commented Sep 8, 2019

This works best in 2019:

Capybara.register_driver :selenium_chrome_headless do |app|
  # Capybara::Selenium::Driver.load_selenium
  browser_options = ::Selenium::WebDriver::Chrome::Options.new.tap do |opts|
    opts.args << '--window-size=1920,1080'
    opts.args << '--force-device-scale-factor=0.95'
    opts.args << '--headless'
    opts.args << '--disable-gpu'
    opts.args << '--disable-site-isolation-trials'
    opts.args << '--no-sandbox'
  end
  Capybara::Selenium::Driver.new(app, browser: :chrome, options: browser_options)
end

Capybara.javascript_driver = :selenium_chrome_headless

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

@woto
Copy link

woto commented Sep 6, 2020

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

@baweaver
Copy link

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

@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