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.

Principle:Teamcapybara Capybara RSpec Integration

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

Overview

A testing integration pattern that configures acceptance testing DSL methods and matchers into a test framework's execution lifecycle.

Description

RSpec Integration is the pattern of hooking a browser automation library into RSpec's example group lifecycle. This involves including DSL modules into specific example group types (:feature and :system), registering before hooks for driver switching, and after hooks for session cleanup. The goal is to provide seamless access to browser interaction methods (visit, find, fill_in, etc.) and expectation matchers (have_selector, have_text, etc.) within test examples without manual setup.

This pattern solves the problem of test isolation: each example must start with a clean browser state, and any driver changes (e.g., switching to a JavaScript-capable driver) must be reverted after the example completes.

Usage

Use this principle when setting up Capybara with RSpec for feature specs or system specs. It is the entry point for all browser-based testing in a Rails or Rack application. A single require 'capybara/rspec' triggers the full integration.

Theoretical Basis

The integration follows a lifecycle hook pattern:

  1. Module Inclusion: Capybara::DSL and Capybara::RSpecMatchers are included into :feature and :system example groups
  2. Before Hook: Switches to JavaScript driver if js: true metadata or a specific :driver metadata is set
  3. After Hook: Calls Capybara.reset_sessions! and Capybara.use_default_driver to restore clean state
# Abstract lifecycle pattern (not actual code)
before_each_example:
  if metadata[:js] then switch_to_javascript_driver
  if metadata[:driver] then switch_to_named_driver

after_each_example:
  reset_all_sessions  # clear cookies, navigate away
  restore_default_driver

Related Pages

Implemented By

Page Connections

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