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 Element Assertion

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

Overview

An assertion pattern that verifies DOM element presence, absence, count, and text content with automatic waiting for asynchronous updates.

Description

Element Assertion provides both predicate methods (returning Boolean) and assertion methods (raising on failure) for verifying page state. The key distinction from raw queries is that assertions include auto-waiting — they continuously re-check the DOM until the condition is met or the timeout expires. This makes them ideal for testing dynamic web applications where content appears asynchronously.

The main assertion types:

  • Selector assertionsassert_selector / has_selector? for element presence
  • Text assertionsassert_text / has_text? (alias has_content?) for text content
  • Negationassert_no_selector / has_no_selector? for absence verification

All support count options (count, minimum, maximum, between) for verifying element counts.

Usage

Use assertion methods in RSpec expectations: expect(page).to have_selector('.item', count: 3) or expect(page).to have_text('Success').

Theoretical Basis

# Abstract assertion flow (not actual code)
assert_selector(selector, options):
  synchronize(wait_time):
    result = query_dom(selector, options)
    unless result.matches_count? and result.any?:
      raise ExpectationNotMet(result.failure_message)

has_selector?(selector, options):
  try:
    assert_selector(selector, options)
    return true
  catch ExpectationNotMet:
    return false

Related Pages

Implemented By

Uses Heuristic

Page Connections

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