Implementation:Teamcapybara Capybara Spec Has Css
| Knowledge Sources | |
|---|---|
| Domains | Testing, Test_Specification |
| Last Updated | 2026-02-12 06:00 GMT |
Overview
Shared RSpec test suite that validates the has_css? and has_no_css? 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_css? and #has_no_css? methods, exercising count options (count, minimum, maximum, between), style filtering (String, Regexp, Hash), class and id filters (including case-insensitive regexp), visibility handling, scoped assertions, spatial requirements, CSS character escaping, text filtering, asynchronous wait behavior, and the predicates_wait configuration toggle.
Usage
Include these shared examples in driver-specific test suites to verify compliance with Capybara's expected behavior contract.
Code Reference
Source Location
- Repository: Teamcapybara_Capybara
- File: lib/capybara/spec/session/has_css_spec.rb
- Lines: 1-375
Signature
Capybara::SpecHelper.spec '#has_css?' do
# shared test examples
end
Capybara::SpecHelper.spec '#has_no_css?' 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_css?
- Presence detection -- verifying true when selectors match elements on the page and false when they do not
- Symbol deprecation warning -- passing a symbol locator triggers a deprecation warning (to be removed in 4.0)
- Id option -- filtering by :id with exact strings and regular expressions
- Class option -- filtering by :class with strings, regular expressions, and arrays of class names
- Style option -- filtering by :style as a String, Regexp, or Hash (Hash form requires: [:css])
- Case-insensitive filters -- :class and :id options with case-insensitive regular expressions
- Scoped assertions -- using within to restrict has_css? to a subtree, including sibling scope error messages
- Asynchronous wait (requires: [:js]) -- waiting for dynamically injected content to appear
- predicates_wait configuration -- Capybara.predicates_wait = true enables waiting; false disables it, with :wait => true override support
- Count options -- :count, :minimum, :maximum, :between with integer coercion and fluent API (.exactly(n).times, .at_most(n).times, .at_least(n).times, .once)
- Text filtering -- narrowing matches by string or regexp text content
- Spatial requirements (requires: [:spatial]) -- above, right_of, below, left_of, near with fluent spatial sugar
- CSS escape handling -- verifying selectors with escaped backslash sequences via Capybara::Selector::CSS.escape
#has_no_css?
- Absence detection -- verifying true when selectors do not match and false when they do
- Scoped assertions -- within blocks correctly limit scope for negative checks
- Asynchronous disappearance (requires: [:js]) -- waiting for elements to be removed from the DOM
- Count options -- :count, :minimum, :maximum, :between with integer coercion (inverse logic from has_css?)
- Text filtering -- string and regexp text filters applied to negative assertions