O'Reilly logo

Application Testing with Capybara by Matthew Robbins

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Asserting and querying

Now that you can navigate around your application, submit forms, and locate any element in the DOM, we need to turn our attention to validating the expected behavior.

Capybara allows us to do this in a couple of ways.

The first option is to use the Capybara "Query" API directly. Capybara provides a whole set of methods for querying the page under test and returning a Boolean value.

page.has_content? 'capybara rocks'
page.has_selector? '#main'

You could use these methods with a traditional "assertion" approach where tests assert against a Boolean condition. The other option is to use RSpec "Magic Matchers". These matchers in fact just "piggy back" off the query methods:

page.should have_content 'capybara rocks' page.should have_selector ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required