Implementation:Teamcapybara Capybara Capybara Modify Selector
Appearance
| Knowledge Sources | |
|---|---|
| Domains | Testing, Selector_System |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
Concrete tool for modifying existing selector definitions provided by Capybara.modify_selector.
Description
Capybara.modify_selector(name, &block) delegates to Capybara::Selector.update(name, &block). This looks up the existing Selector::Definition by name and calls instance_eval(&block) on it, allowing new filters, labels, or expression modifications to be added to the existing definition.
Usage
Call in test configuration to extend built-in selectors with application-specific filters.
Code Reference
Source Location
- Repository: capybara
- File: lib/capybara.rb (L200-202), lib/capybara/selector/selector.rb (L18-19)
Signature
def modify_selector(name, &block)
# @param name [Symbol] Name of existing selector to modify
# @yield Block evaluated in context of the existing Selector::Definition
Capybara::Selector.update(name, &block)
end
Import
require 'capybara'
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| name | Symbol | Yes | Name of existing selector to modify |
| block | Block | Yes | Definition DSL block to apply to existing selector |
Outputs
| Name | Type | Description |
|---|---|---|
| updated definition | Selector::Definition | Existing definition updated with new filters/behavior |
Usage Examples
Add Filter to Built-In Selector
Capybara.modify_selector(:button) do
node_filter(:btn_style, valid_values: [:primary, :secondary, :danger]) do |node, style|
node[:class].split.include?("btn-#{style}")
end
end
# Now works:
find(:button, 'Submit', btn_style: :primary)
click_button 'Delete', btn_style: :danger
Extend Field Selector
Capybara.modify_selector(:field) do
node_filter(:required, :boolean) do |node, value|
node[:required] == value.to_s || (value && node.has_attribute?('required'))
end
end
find(:field, 'Email', required: true)
Related Pages
Implements Principle
Page Connections
Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment