Headless Chrome Show archive.org snapshot is a way to run the Chrome browser without a visible window.
Configure the Capybara driver like this:
Capybara.register_driver :selenium do |app|
options = Selenium::WebDriver::Chrome::Options.new
unless ENV.key?('NO_HEADLESS')
options.add_argument('--headless')
options.add_argument('--disable-gpu')
end
if ENV.key?('CI')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
end
if ENV.key?('REMOTE_DEBUGGING')
options.add_argument('--remote-debugging-address=0.0.0.0')
options.add_argument('--remote-debugging-port=9222')
end
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
If you're working on a legacy project with Capybara version 1.x, the code below should do:
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
You can call your tests with NO_HEADLESS=1 bundle exec cucumber
to see the Chrome window for this test run.
You might want to define a bash alias alias nh='NO_HEADLESS=1'
to shorten this command to nh bundle exec cucumber
.
In case your Chrome is running within a docker container, the remote debugging option helps you to connect your Chrome inside the docker image with a Chrome on your host system.
Here is an example for a docker container started with docker-compose
:
docker-compose run --publish 9222:9222 some-app bash
chrome://inspect/
, check Discover network targets
, follow Configure...
next to the Discover network targets
and add localhost:9222
in the Target discovery settings
REMOTE_DEBUGGING=true bundle exec rspec spec/system/some_spec.rb
with a debugger setNote
Remote debugging does not work well if you change your selenium driver between features (e.g. from a mobile device emulation back to a desktop browser). Therefore it makes more sense to run only single features / specs with REMOTE_DEBUGGING enabled.