The Capybara API: Querying
Explore how to use Capybara's query methods like have_selector and assert_selector to check HTML elements, attributes, text, and navigation in Rails integration tests. Understand options such as count, text matching, visibility, and scoping with within to write precise tests. Learn how to use save_and_open_page for debugging test failures.
We'll cover the following...
Capybara has a few methods designed to query the simulated browser page to check for various selector patterns in the page. This is one case where the syntax differs slightly between Minitest and RSpec.
Capybara testing
Here the method current_url is used, which returns as a complete string the current URL that the simulated user is viewing. That’s useful for testing whether the navigation links take us where they should.
The most common query method in Capybara is written as the matcher have_selector in RSpec, and as assert_selector in Minitest. The two are identical in functionality. By default, Capybara looks for a CSS selector matching the argument to the query method, using the common # shortcut for DOM ID and a dot (.) for DOM class. The assertion passes if the selector is found.
Capybara queries
The Capybara query ...