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 Has Text

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

Overview

Shared RSpec test suite that validates the has_text? and has_no_text? predicate methods across all Capybara drivers.

Description

Capybara::SpecHelper provides shared test specifications that driver implementations must pass. This file contains the shared examples for the #has_text? and #has_no_text? methods, exercising string and regexp matching, whitespace normalization (including normalize_ws and Capybara.default_normalize_ws), visibility filtering (:all and Capybara.ignore_hidden_elements), count options (count, minimum, maximum, between), scoped text assertions, quote handling, tag stripping, script/title exclusion, exact text matching, asynchronous wait behavior, and non-string parameter coercion.

Usage

Include these shared examples in driver-specific test suites to verify compliance with Capybara's expected behavior contract.

Code Reference

Source Location

Signature

Capybara::SpecHelper.spec '#has_text?' do
  # shared test examples
end

Capybara::SpecHelper.spec '#has_no_text?' do
  # shared test examples
end

Import

require 'capybara/spec/spec_helper'

I/O Contract

Inputs

Name Type Required Description
session Capybara::Session Yes Active session to test against

Outputs

Name Type Description
Test results RSpec::Core::Example Pass/fail assertions

Key Test Scenarios

#has_text?

  • String matching -- detecting text on the page by substring, including symbol coercion via to_s
  • Scoped text -- verifying text within a within block, ensuring scope boundaries are respected
  • Tag stripping -- ignoring HTML tags so that inner text is checked as a continuous string
  • Whitespace normalization -- normalize_ws: true collapses whitespace; normalize_ws: false preserves raw whitespace; Capybara.default_normalize_ws sets the default behavior
  • Regexp matching -- using regular expressions to match page text, with special-character escaping for string arguments
  • Visibility filtering -- hidden text excluded by default; :all visibility type includes hidden text; Capybara.ignore_hidden_elements = false includes hidden text globally
  • Script and title exclusion -- text inside <script> and <title> tags is not matched
  • Quote handling -- single quotes, double quotes, and mixed quotes in page text
  • Exact matching -- :exact => true requires the full text of the element to match
  • Non-string parameters -- integer and nil arguments with appropriate coercion and nil-warning to stderr
  • Objects with to_s and to_hash -- correctly using to_s and not to_hash for text coercion, including empty-options edge case
  • Asynchronous wait (requires: [:js]) -- waiting for dynamically appearing text using Capybara.using_wait_time and :wait option
  • Count options -- :count, :minimum, :maximum, :between with integer coercion and fluent API (.exactly(n).times, .at_most(n).times, .at_least(n).times, .once)
  • Invalid option validation -- raising ArgumentError when unknown options are passed

#has_no_text?

  • Absence detection -- verifying true when text is not on the page and false when it is
  • Scoped text (inverse) -- respecting within scope for negative text assertions
  • Tag stripping -- ensuring raw HTML tag strings are absent but stripped inner text is present
  • Quote handling -- same quote scenarios tested inversely
  • Visibility filtering -- hidden text treated as absent by default; :all and ignore_hidden_elements override
  • Script and title exclusion -- text in <script> and <title> tags treated as absent
  • Regexp matching (inverse) -- regular expressions that do not match return true
  • Special character escaping -- string arguments with regexp-special characters do not accidentally match
  • Asynchronous disappearance (requires: [:js]) -- waiting for text to be removed from the page, with configurable :wait duration

Related Pages

Page Connections

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