How to Use Headless Chrome with Capybara and Selenium

How to use headless chrome with capybara and selenium

1) Make sure you don't have another registered driver, I made this mistake myself and had an iphone driver, which was using the args in the old way, that's why I was getting the warning.

2) Make sure you have Chrome version 57+ on Linux, 59+ on macOS or 60+ on Windows;

3) Add/update the gem selenium-webdriver;

4) Add the following driver to your spec_helper.rb or rails_helper.rb:

Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new app, browser: :chrome,
options: Selenium::WebDriver::Chrome::Options.new(args: %w[headless disable-gpu])
end

Capybara.javascript_driver = :chrome

Capybara with headless chrome doesn't clear session between test cases which use different subdomains

Are you storing session information in the browsers window.localStorage and/or window.sessionStorage? If so you can set those to be cleared via options passed to the driver (Note: these settings are the default for the selenium driver in Capybara 3.12+)

Capybara.register_driver :chrome do |app|
options = Selenium::WebDriver::Chrome::Options.new(args: %w[no-sandbox])
options.headless!
Capybara::Selenium::Driver.new(app, browser: :chrome, options: options, clear_local_storage: true, clear_session_storage: true)
end

How to get capybara chrome headless to open sweetalert2 modals for Rspec tests

accept_alert is for dealing with system modals (those the browser creates by default when calling window.alert that don't actually add elements to the page). Sweetalert2 is a JS library that inserts elements to the page to create more stylish "modals". You don't use accept_alert with those, you just interact with them as if they were any other HTML elements on the page. That would mean something along the lines of

....
attach_file(...)
click_button('Upload', disabled: false) # Not sure why you're passing `disabled: false` here since that's the default
within('.swal2-actions') { click_button('the text of the button to accept the "modal"') }
expect(page)....

Update: As discovered in the comments - an additional cause of this issue was the assets not being compiled, in the OPs setup, so the JS wasn't firing at all. This would be immediately clear when running in non-headless mode and seeing that no "modal" was ever being displayed. The fix for that depends on what asset pipeline is being used and how it's configured, which goes beyond the scope of this question.

Chrome headless download pdf using capybara and selenium

You don't need to send that manually anymore it was added to selenium as Selenium::WebDriver::Chrome::Server#download_path=. You can set it in your driver registration via the Capybara::Selenium::Driver instance

...
Capybara::Selenium::Driver.new(
app,
browser: :chrome,
desired_capabilities: caps
).tap { |d| d.browser.download_path = <your download path> }

Getting Headless Chrome to work with Capybara

Since you're using brew to install chromedriver you need to completely remove chromedriver-helper and all the binaries and stubs it has installed. This is because bundler adds the installed binaries/stubs into the path before the version of chromedriver installed by brew and therefore shadows it. You can use

bundle exec which chromedriver

to find out which chromedriver is actually being used when you run your tests. If it's not the one installed by brew (usually /usr/local/bin/chromedriver) then keep removing them until it is.



Related Topics



Leave a reply



Submit