Implementation:Teamcapybara Capybara Minitest Assertions
| Knowledge Sources | |
|---|---|
| Domains | Testing, Web_Automation |
| Last Updated | 2026-02-12 06:00 GMT |
Overview
The Capybara::Minitest::Assertions module provides a comprehensive set of Minitest-compatible assertion and refutation methods for verifying page content, selectors, and element state in Capybara-driven tests.
Description
Capybara::Minitest::Assertions bridges Capybara's matcher system with the Minitest assertion framework. It uses metaprogramming (via class_eval and define_method) to dynamically generate matching pairs of assert_* and refute_* methods for text, titles, paths, CSS selectors, XPath selectors, and specific HTML element types (links, buttons, fields, selects, tables, etc.). Each generated assertion method delegates to the corresponding Capybara::Node::Matchers method on the resolved subject and converts any Capybara::ExpectationNotMet exception into a Minitest::Assertion so that failures are reported through Minitest's standard mechanism.
Usage
Include Capybara::Minitest::Assertions in your Minitest test class to gain access to Capybara-style assertions such as assert_text, assert_selector, assert_css, assert_button, and their negated counterparts. This is the recommended approach when using Capybara with Minitest rather than RSpec.
Code Reference
Source Location
- Repository: Teamcapybara_Capybara
- File: lib/capybara/minitest.rb
- Lines: 1-398
Signature
module Capybara
module Minitest
module Assertions
# Dynamically generated assertion methods:
# assert_text(*args, **kwargs, &optional_filter_block)
# assert_no_text(*args, **kwargs, &optional_filter_block)
# assert_selector(*args, &optional_filter_block)
# assert_no_selector(*args, &optional_filter_block)
# assert_css(*args, &optional_filter_block)
# assert_xpath(*args, &optional_filter_block)
# assert_link(*args, &optional_filter_block)
# assert_button(*args, &optional_filter_block)
# assert_field(*args, &optional_filter_block)
# assert_checked_field(*args, &optional_filter_block)
# assert_table(*args, &optional_filter_block)
# assert_matches_selector(*args, &optional_filter_block)
# assert_matches_style(*args, &optional_filter_block)
# assert_ancestor(*args, &optional_filter_block)
# assert_sibling(*args, &optional_filter_block)
# ... and corresponding refute_* / assert_no_* aliases
private
def determine_subject(args)
end
def extract_locator(args)
end
end
end
end
Import
require 'minitest'
require 'capybara/dsl'
require 'capybara/minitest'
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| args | Array | Yes | Positional arguments; the first element may be a Capybara::Session, Capybara::Node::Base, or Capybara::Node::Simple to serve as the subject, otherwise page is used. Remaining elements provide the locator and options. |
| kwargs | Hash | No | Keyword arguments forwarded to the underlying Capybara matcher (e.g., text:, count:, wait:).
|
| optional_filter_block | Block | No | An optional block used as a custom filter for matching elements. |
Outputs
| Name | Type | Description |
|---|---|---|
| (pass) | nil | The assertion passes silently when the expected condition is met, incrementing the Minitest assertion counter. |
| (failure) | Minitest::Assertion | Raised when the expected condition is not met, with the message from Capybara::ExpectationNotMet. |
Usage Examples
Basic Usage
require 'capybara/minitest'
class MyFeatureTest < Minitest::Test
include Capybara::DSL
include Capybara::Minitest::Assertions
def test_homepage_has_welcome_message
visit '/'
assert_text 'Welcome'
assert_selector :css, 'h1.title'
assert_link 'Sign In'
refute_text 'Error'
end
def test_form_has_required_fields
visit '/signup'
assert_field 'Email'
assert_button 'Submit'
assert_checked_field 'terms_accepted'
assert_css 'form#signup'
end
end