Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

benlieb commented Oct 17, 2016

I had to do:

Capybara.register_driver :selenium do |app|

For this to work

@KacperPucek

This comment has been minimized.

Copy link

KacperPucek commented Feb 7, 2017

Worked like a charm, thanks :)

@mariusandra

This comment has been minimized.

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

This comment has been minimized.

Copy link

harokevin commented Aug 25, 2017

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

worked for me

@rounders

This comment has been minimized.

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

This comment has been minimized.

Copy link

paneq commented Mar 2, 2018

Thank you @rounders :)

@seanlinsley

This comment has been minimized.

Copy link

seanlinsley commented May 10, 2018

@rounders' solution worked for me as well

@gayathri-sapaad

This comment has been minimized.

Copy link

gayathri-sapaad commented Jan 8, 2019

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

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

Thanks @mars

@vivipoit

This comment has been minimized.

Copy link

vivipoit commented Jan 16, 2019

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

This comment has been minimized.

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/

Cheers

KG

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.