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
Advice on How to Validate Names and Surnames Using Regex
How to Create an Operator for Deep Copy/Cloning of Objects in Ruby
Require Ruby File Without .Rb Extension
Undefined Method 'Instance' for Capistrano::Configuration:Class
Ruby: Automatically Set Instance Variable as Method Argument
Ruby on Rails. Bundler. Cucumber. Rake Aborted! Command Failed with Status (1)
Ruby: Overriding the Puts Method
Generic Way to Replace an Object in It's Own Method
Is It a Good Idea to Pass a Huge String as an Argument to Sidekiq Worker
Rails: Rake Db:Create:All (Could Not Connect to Server)
Cancan Abilities in Separate File
Ruby Optionparser Empty Switch "-" Behavior
Why Does 'Puts(Nil or 4)' Fail in Ruby
Should Gemfile.Lock Be Committed to Source Control on Windows
Ruby Equivalent for Python's For/Else
Error When Installing Libv8 3.11.8.3
What Regex How to Use to Get the Domain Name from a Url in Ruby