Principle:Teamcapybara Capybara Element Assertion
| 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 assertions — assert_selector / has_selector? for element presence
- Text assertions — assert_text / has_text? (alias has_content?) for text content
- Negation — assert_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