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 Node Matchers Assert Selector

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

Overview

Concrete tools for asserting element presence, count, and text content provided by Capybara::Node::Matchers.

Description

Node::Matchers#assert_selector constructs a SelectorQuery, resolves it within synchronize, and raises Capybara::ExpectationNotMet if the result doesn't match count expectations. #has_selector? wraps this in make_predicate which catches ExpectationNotMet and returns Boolean.

Node::Matchers#assert_text verifies text content using TextQuery, with support for exact matching, normalization, and count expectations. #has_text? (aliased as has_content?) is the predicate form.

Usage

Use with RSpec matchers: expect(page).to have_selector(...) or call directly: page.assert_selector(...).

Code Reference

Source Location

  • Repository: capybara
  • File: lib/capybara/node/matchers.rb
  • Lines: L38-40 (has_selector?), L109-115 (assert_selector), L700-706 (assert_text), L738-741 (has_text?/has_content?)

Signature

def assert_selector(*args, &optional_filter_block)
  # @param args [Symbol, String, Hash] Selector args + options
  # @return [true]
  # @raise [Capybara::ExpectationNotMet]
end

def has_selector?(*args, **options, &optional_filter_block)
  # @return [Boolean]
end

def assert_text(type_or_text, *args, **opts)
  # @param type_or_text [:all, :visible, String, Regexp]
  # @return [true]
  # @raise [Capybara::ExpectationNotMet]
end

def has_text?(*args, **options)
  # @return [Boolean]
end
# alias: has_content?

Import

require 'capybara'
# Available on page, any Node::Base, and via RSpec matchers

I/O Contract

Inputs

Name Type Required Description
selector args Symbol, String Yes Selector type and locator
text String/Regexp No Text content to verify
count Integer No Exact expected count
minimum Integer No Minimum expected count
maximum Integer No Maximum expected count
between Range No Range of expected counts
exact_text Boolean/String No Require exact text match
wait Numeric No Override default wait time

Outputs

Name Type Description
assert_* true Returns true on success, raises on failure
has_*? Boolean Returns true/false

Usage Examples

Selector Assertions

# With RSpec
expect(page).to have_selector('table tr', count: 5)
expect(page).to have_selector(:button, 'Submit')
expect(page).to have_no_selector('.error')

# Direct assertion
page.assert_selector('#content', visible: true)
page.assert_no_selector('.loading-spinner', wait: 10)

Text Assertions

expect(page).to have_text('Welcome, Alice')
expect(page).to have_content(/order #\d+/i)
expect(page).to have_no_text('Error')

# With exact matching
expect(page).to have_text('Total: $99.99', exact: true)

Related Pages

Implements Principle

Uses Heuristic

Page Connections

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