Capybara Test Helpers

Build Status Maintainability Test Coverage Gem Version License

Capybara Test Helpers is an opinionated library built on top of capybara, that encourages good testing practices based on encapsulation and reuse.

Write tests that everyone can understand, and leverage your Ruby skills to keep them easy to read and easy to change.

Documentation 📖

Visit the documentation website to check out the guides, API reference, and examples.

Installation 💿

Add this line to your application's Gemfile:

gem 'capybara_test_helpers'

To use with RSpec, add the following to your spec_helper.rb:

require 'capybara_test_helpers/rspec'

To use with Cucumber, add the following to your support/env.rb:

require 'capybara_test_helpers/cucumber'

Additional installation instructions are available in the documentation website.

Quick Tour ⚡️

Let's say we have a list of cities, and we want to test the Edit functionality using Capybara.

scenario 'editing a city' do
  visit('/cities')

  within('.cities') {
    find(:table_row, { 'Name' => 'NYC' }).click_on('Edit')
  }
  fill_in 'Name', with: 'New York City'
  click_on('Update City')

  within('.cities') {
    expect(page).not_to have_selector(:table_row, { 'Name' => 'NYC' })
    expect(page).to have_selector(:table_row, { 'Name' => 'New York City' })
  }
end

Even though it gets the job done, it takes a while to understand what the test is trying to do.

Without discipline these tests can become hard to manage and require frequent updating.

Using Test Helpers

We can avoid the duplication and keep the focus on the test instead of its implementation by using test helpers.

scenario 'editing a city', test_helpers: [:cities] do
  cities.visit_page

  cities.edit('NYC', with: { name: 'New York City' })

  cities.should_no_longer.have_city('NYC')
  cities.should_now.have_city('New York City')
end

Learn more about it in the documentation website.

Special Thanks 🙏

This library wouldn't be the same without early validation from my colleagues, and numerous improvements and bugfixes they contributed to it. Thanks for the support 😃

  • capybara: Solid library to write integration tests in Ruby.

License

The gem is available as open source under the terms of the MIT License.