Setting Up Capybara

Learn how to set up capybara in the rails application.

Capybara and RSpec

Capybara allows an automated test to simulate user interaction with a browser. When simulating this interaction, Capybara works in conjunction with a driver, using the simple Capybara API to determine what elements to interact with and using the driver to manage the actual interaction. By default, Capybara uses a native Ruby library that doesn’t manage JavaScript interactions, but it can be configured to use a headless browser such as PhantomJS, headless Chrome, or Selenium to allow JavaScript interactions to be simulated.

Capybara Gemfile

Capybara is part of the default Rails Gemfile in Rails 5.1 and later. If we’re using an earlier version, we add Capybara to our Gemfile’s test group:

gem "capybara"

In either case, there’s a useful tool called capybara-screenshot that takes virtual screenshots of any Capybara test failure. Again, we need to add capybara-screenshot in the Gemfile’s test group of our Gemfile:

gem "capybara-screenshot"

Capybara setup

Although Capybara is already available, there’s a little setup needed to enable capybara-screenshot in RSpec. In a file in the spec.support directory, we add this:

Get hands-on with 1200+ tech skills courses.