Running Parallel Selenium Tests with Capybara

Capybara + Rspec: How to set up multi browser TC running?

If you happen to pass here looking for a way to run every test through many browsers, I wrote this (working!) (overly talkative) snippet based on Thomas Walpole's accepted answer:

Rspec.configure do |conf|

Capybara.register_driver :chrome do |mode|
Capybara::Selenium::Driver.new mode, :browser => :remote, :desired_capabilities => :chrome
end

Capybara.register_driver :firefox do |mode|
Capybara::Selenium::Driver.new mode, :browser => :remote, :desired_capabilities => :firefox
end

## Here we politely ask every example to run twice, for each browser we want
conf.around do |example|
puts '~> ' + example.metadata[:example_group][:full_description]
# avoid looping over Capybara.drivers as it will also contain "selenium" generic driver and "rack_test" basic (js-less) browser
[:chrome, :firefox].each do |browser|
Capybara.current_driver = browser
puts "~~> #{example.description} @ #{browser}"
example.run
end
end

Capybara.default_driver = :chrome
end

The output ends being something like:

~> The contact page
~~> loads @ chrome
~~> loads @ firefox
.

Finished in 20.9 seconds (files took 2.69 seconds to load)
1 example, 0 failures

Obviously all puts lines can be removed / commented out, they're there just for sample purposes :)

After each ~~> line the said browser opens and runs the example.

Care must be taken, however, to remove :js => true from your examples, or else Capybara will force-run them against the default javascript_driver instead.

Why are parallel tests failing with “Selenium::WebDriver::Error::WebDriverError invalid session id”?

This is a common problem in selenium running in docker env. A way to fix it is use the disable-dev-shm-usage options and change the Capybara server port. Like it:

# ...

Capybara.server_port = 9887 + ENV["CIRCLE_NODE_INDEX"].to_i # In order to create a new server port to each runner

Capybara.register_driver :headless_chrome do |app|
# ...
browser_options.args << '--disable-dev-shm-usage'
# ...
end

Source: https://github.com/grosser/parallel_tests/issues/658#issuecomment-429395002

Capybara's existing test + using Selenium to run the tests against a remote server

That configuration would normally go in spec/spec_helper.rb.

Rails - Minitest + Capybara + Selenium - Test destroy action

Assuming you're using a modern version of Rails (5.2/6) and a standard system test configuration (not running parallel tests in threads) then the concerns in the answer of Gregório Kusowski are irrelevant because the DB connection is shared between your tests and your application, preventing the issue of the tests not being able to see your apps changes.

Also assuming you're using Selenium in these system tests, the main problem you're dealing with is that actions in the browser occur asynchronously from your tests, so just because you've told your test to accept the dialog box doesn't mean the action to delete the company has completed when it returns. The way to verify that is to just sleep for a little bit before checking for the change in count. While that will work it's not a good final solution because it ends up wasting time. Instead, you should be checking for a visual change that indicates the action has completed before verifying the new count

test "destroy" do
companies_count = Company.count
visit company_path(@company)
accept_confirm do
click_on "Delete"
end

assert_text "Company Deleted!" # Check for whatever text is shown to indicate the action has successfully completed

assert_equal (companies_count - 1), Company.count
end

This works because Capybara provided assertions have a waiting/retrying behavior that allows the application up to a specific amount of time to catch up with what the test is expecting.

Note: I've replaced the page.driver... with the correct usage of Capybaras system modal API - If you're using page.driver... it generally indicates you're doing something wrong.



Related Topics



Leave a reply



Submit