Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Teamcapybara Capybara Capybara RSpec Setup

From Leeroopedia
Knowledge Sources
Domains Testing, Integration
Last Updated 2026-02-12 00:00 GMT

Overview

Concrete tool for integrating Capybara with RSpec by requiring capybara/rspec which auto-configures DSL inclusion, matcher availability, and lifecycle hooks.

Description

The capybara/rspec require entry point triggers a chain of side effects: it requires capybara/dsl, capybara/rspec/matchers, capybara/rspec/features, and capybara/rspec/matcher_proxies. It then calls RSpec.configure to include Capybara::DSL and Capybara::RSpecMatchers into feature and system example groups, and registers before and after hooks for driver management and session cleanup.

Usage

Require this in your spec_helper.rb or rails_helper.rb to enable Capybara in RSpec feature and system specs. This is a one-time setup step.

Code Reference

Source Location

  • Repository: capybara
  • File: lib/capybara/rspec.rb
  • Lines: L1-31

Signature

# Side-effect based require - no function signature
# Triggers RSpec.configure with:
RSpec.configure do |config|
  config.include Capybara::DSL, type: :feature
  config.include Capybara::RSpecMatchers, type: :feature
  config.include Capybara::DSL, type: :system
  config.include Capybara::RSpecMatchers, type: :system
  config.include Capybara::RSpecMatchers, type: :view

  config.after do
    if self.class.include?(Capybara::DSL)
      Capybara.reset_sessions!
      Capybara.use_default_driver
    end
  end

  config.before do |example|
    if self.class.include?(Capybara::DSL)
      Capybara.current_driver = Capybara.javascript_driver if example.metadata[:js]
      Capybara.current_driver = example.metadata[:driver] if example.metadata[:driver]
    end
  end
end

Import

require 'capybara/rspec'

I/O Contract

Inputs

Name Type Required Description
(none) Side-effect require; no parameters

Outputs

Name Type Description
DSL methods Module inclusion visit, find, fill_in, click_on etc. available in feature/system specs
RSpec matchers Module inclusion have_selector, have_text, have_current_path etc. available
Lifecycle hooks RSpec hooks before hook for driver switching, after hook for session reset

Usage Examples

Basic Setup

# spec/rails_helper.rb (or spec/spec_helper.rb)
require 'capybara/rspec'

# Optionally for Rails apps:
require 'capybara/rails'

Feature Spec

# spec/features/login_spec.rb
require 'rails_helper'

feature 'User Login' do
  scenario 'with valid credentials' do
    visit '/login'
    fill_in 'Email', with: 'user@example.com'
    fill_in 'Password', with: 'password123'
    click_button 'Log in'
    expect(page).to have_text('Welcome')
  end

  scenario 'with JavaScript', js: true do
    visit '/login'
    fill_in 'Email', with: 'user@example.com'
    # Uses javascript_driver automatically due to js: true
    expect(page).to have_selector('.js-loaded')
  end
end

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment