Overview
Provides RSpec matchers for asserting the presence, absence, or count of CSS/XPath selectors on a Capybara node or page.
Description
HaveSelector extends CountableWrappedElementMatcher and is the primary matcher behind have_selector, have_css, and have_xpath expectations. It delegates to assert_selector and assert_no_selector on the wrapped element and builds a SelectorQuery for description and introspection. Its constructor handles the special case where string-keyed keyword arguments are present with fewer than two positional arguments, merging them into the args array. HaveAllSelectors asserts that all given selectors are present and raises ArgumentError when used with negation. HaveNoSelectors asserts that none of the given selectors are present, also disallowing negation. HaveAnySelectors asserts that at least one selector is present and supports negation by delegating to assert_none_of_selectors.
Usage
Use HaveSelector for single-selector assertions with optional count, minimum, maximum, and spatial constraints. Use HaveAllSelectors when every selector in a set must be present. Use HaveNoSelectors to assert none are present. Use HaveAnySelectors to assert at least one from a set is present.
Code Reference
Source Location
Signature
# HaveSelector
class Capybara::RSpecMatchers::Matchers::HaveSelector < CountableWrappedElementMatcher
def initialize(*args, **kw_args, &filter_block)
def element_matches?(el)
def element_does_not_match?(el)
def description
def query
end
# HaveAllSelectors
class Capybara::RSpecMatchers::Matchers::HaveAllSelectors < WrappedElementMatcher
def element_matches?(el)
def does_not_match?(_actual) # raises ArgumentError
def description
end
# HaveNoSelectors
class Capybara::RSpecMatchers::Matchers::HaveNoSelectors < WrappedElementMatcher
def element_matches?(el)
def does_not_match?(_actual) # raises ArgumentError
def description
end
# HaveAnySelectors
class Capybara::RSpecMatchers::Matchers::HaveAnySelectors < WrappedElementMatcher
def element_matches?(el)
def does_not_match?(el)
def description
end
Import
require 'capybara/rspec/matchers/have_selector'
I/O Contract
HaveSelector#initialize
| Parameter |
Type |
Description
|
*args |
Array |
Selector type and locator (e.g., :css, '.foo' or :xpath, '//div')
|
**kw_args |
Hash |
Options such as count, minimum, maximum, text, visible, etc.
|
&filter_block |
Proc |
Optional block to further filter matched elements
|
| Returns |
Type |
Description
|
| Instance |
HaveSelector |
A new matcher configured with the given selector and options
|
HaveSelector#element_matches?
| Parameter |
Type |
Description
|
el |
Capybara::Node::Base |
The wrapped Capybara node to assert against
|
| Returns |
Type |
Description
|
| result |
true |
Returns true if assertion passes; raises Capybara::ExpectationNotMet otherwise
|
HaveSelector#query
| Returns |
Type |
Description
|
| query |
Capybara::Queries::SelectorQuery |
The underlying selector query built from the matcher arguments
|
HaveAllSelectors#does_not_match?
| Raises |
Description
|
ArgumentError |
Always raised; have_all_selectors does not support negation with not_to / should_not
|
HaveNoSelectors#does_not_match?
| Raises |
Description
|
ArgumentError |
Always raised; have_none_of_selectors does not support negation with not_to / should_not
|
Usage Examples
Basic selector matching
expect(page).to have_selector(:css, '.alert.success')
expect(page).to have_selector(:xpath, '//div[@class="alert"]')
Selector with count constraints
expect(page).to have_selector('.item', count: 5)
expect(page).to have_selector('.item', minimum: 1, maximum: 10)
Asserting all selectors are present
expect(page).to have_all_of_selectors(:css, '.header', '.footer', '.sidebar')
Asserting no selectors are present
expect(page).to have_none_of_selectors(:css, '.error', '.warning')
Asserting any selector is present
expect(page).to have_any_of_selectors(:css, '.success', '.info', '.notice')
# Negation is also supported:
expect(page).not_to have_any_of_selectors(:css, '.error', '.fatal')
Related Pages