Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Teamcapybara Capybara Spec Shared Matchers

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

Overview

This shared test suite validates the complete set of Capybara::RSpecMatchers, ensuring that all custom RSpec matchers behave correctly on both string inputs and live session/node objects.

Description

The file defines RSpec.shared_examples for Capybara::RSpecMatchers, parameterized by a session and mode. It exercises every built-in RSpec matcher provided by Capybara: have_css, have_xpath, have_selector, have_content/have_text, have_element, have_link, have_title, have_current_path, have_button, have_field, have_checked_field, have_unchecked_field, have_select, and have_table. Each matcher is tested in both positive (should) and negative (should_not) forms, on plain HTML strings and on live page/node objects via the session. The suite validates count constraints (exact count, minimum, maximum, between ranges), wait behavior for asynchronous content, description output for readable failure messages, and compound matchers using .and, .and_then, and .or combinators with correct concurrent retry semantics and wait time handling.

Usage

Driver-specific RSpec test files include these shared examples to verify that Capybara's matchers work correctly with their driver. For instance, the Selenium and Rack Test RSpec specs both call it_behaves_like Capybara::RSpecMatchers, passing their session object, to confirm matcher behavior in each environment.

Code Reference

Source Location

Signature

RSpec.shared_examples Capybara::RSpecMatchers do |session, _mode|
  include Capybara::DSL
  include Capybara::RSpecMatchers

  describe 'have_css matcher' do ... end
  describe 'have_xpath matcher' do ... end
  describe 'have_selector matcher' do ... end
  describe 'have_content matcher' do ... end
  describe 'have_text matcher' do ... end
  describe 'have_element matcher' do ... end
  describe 'have_link matcher' do ... end
  describe 'have_title matcher' do ... end
  describe 'have_current_path matcher' do ... end
  describe 'have_button matcher' do ... end
  describe 'have_field matcher' do ... end
  describe 'have_checked_field matcher' do ... end
  describe 'have_unchecked_field matcher' do ... end
  describe 'have_select matcher' do ... end
  describe 'have_table matcher' do ... end

  describe 'Spatial Requirements', requires: [:spatial] do ... end

  describe '#and' do ... end
  describe '#and_then' do ... end
  describe '#or' do ... end
end

Import

require 'spec_helper'
require 'capybara/dsl'
require 'capybara/rspec/matchers'
require 'benchmark'

Key Test Scenarios

  • have_css -- Tests CSS selector matching on strings and pages, with count, minimum, maximum, and between constraints, plus compound .and/.or support
  • have_xpath -- Tests XPath expression matching with positive/negative assertions and compounding
  • have_selector -- Tests the generic selector matcher with proper description generation and count constraints
  • have_content / have_text -- Tests text content matching with string and regexp patterns, visible/all text modes, count validation, and the distinction between have_content and have_text aliases
  • have_element -- Tests element matching by tag name with attribute filters
  • have_link -- Tests link presence matching by link text
  • have_title -- Tests page title matching with string and regexp, including asynchronous wait behavior via :wait option
  • have_current_path -- Tests current URL path matching with wait support for navigation changes
  • have_button -- Tests button presence matching with disabled state options
  • have_field -- Tests form field matching by label text, with value constraints and type coercion via to_s
  • have_checked_field / have_unchecked_field -- Tests checkbox/radio checked state matching in both positive and negative forms
  • have_select -- Tests select element matching by associated options and selected values
  • have_table -- Tests table matching by caption text
  • Compound matchers (#and, #and_then, #or) -- Validates concurrent retry behavior with .and, sequential execution with .and_then, and fallback semantics with .or, including verification that :wait options are correctly ignored in favor of using_wait_time and that timing constraints are met via Benchmark.realtime assertions

Related Pages

Page Connections

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