Principle:Teamcapybara Capybara Selector Modification
| Knowledge Sources | |
|---|---|
| Domains | Testing, Selector_System |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
An extension pattern that reopens an existing selector definition to add new filters or modify behavior without replacing it.
Description
Selector Modification allows extending built-in or previously defined selectors without replacing their entire definition. This is useful for:
- Adding domain-specific filters to Capybara's built-in selectors (e.g., filtering buttons by style class)
- Augmenting third-party selectors
- Progressively building up selector capabilities
The modification block is evaluated via instance_eval on the existing Definition object, so it has access to all DSL methods (expression_filter, node_filter, label, etc.).
Usage
Use Capybara.modify_selector to add filters to existing selectors. This is preferred over redefining a selector from scratch when you only need to add functionality.
Theoretical Basis
# Abstract modification pattern (not actual code)
modify_selector(:button) do
node_filter(:style) do |node, value|
node[:class].include?("btn-#{value}")
end
end
# Now: find(:button, 'Submit', style: :primary) works