Posted 7 months ago. Visible to the public. Repeats.

Capybara: Running tests with headless Chrome

Headless Chrome is a way to run the Chrome browser without a visible window.

Configuring Capybara

Configure the Capybara driver like this:

Copy
Capybara.register_driver :selenium do |app| options = Selenium::WebDriver::Chrome::Options.new options.add_argument('--headless') unless ENV.key?('NO_HEADLESS') options.add_argument('--disable-infobars') options.add_emulation(device_metrics: { width: 1280, height: 960, touch: false }) Capybara::Selenium::Driver.new(app, browser: :chrome, options: options) end Selenium::WebDriver.logger.level = :error

Headless Chrome in legacy Capybara 1 projects

If you're working on a legacy project with Capybara version 1.x, the code below should do:

Copy
Capybara.register_driver :selenium do |app| args = [] args << '--headless' unless ENV.key?('NO_HEADLESS') args << '--disable-infobars' args << '--window-size=1280,960' Capybara::Selenium::Driver.new(app, browser: :chrome, args: args) end

Geordi users: Disable VNC

Note: In case you are using geordi, it makes sense to disable its VNC wrapper around integration tests. Therefore create a file named .geordi.yml in the project root with the following content:

Copy
use_vnc: false

Looking at the hidden browser

Option 1: Run without headless

You can call your tests with NO_HEADLESS=1 bundle exec cucumber to see the Chrome window for this test run.

Option 2: Remote debugging

In the code above, add an argument --remote-debugging-port=9222. You can then navigate to http://localhost:9222 in another browser to see the headless Chrome UI. You might need to disable this in parallel test runs (I haven't tested this).

More documentation here

makandra has been working exclusively with Ruby on Rails since 2007. Our laser focus on a single technology has made us a leader in this space.

Owner of this card:

Avatar
Henning Koch
Last edit:
5 days ago
by Emanuel De
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Henning Koch to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more