Principle:Teamcapybara Capybara Expression Filtering
| Knowledge Sources | |
|---|---|
| Domains | Testing, Selector_System |
| Last Updated | 2026-02-12 00:00 GMT |
Overview
A pre-query filter mechanism that modifies CSS or XPath expressions based on filter options before the DOM is queried.
Description
Expression Filters narrow the generated selector expression before it hits the DOM, making the initial query more efficient. Instead of fetching all matching elements and filtering in Ruby, expression filters bake filter criteria directly into the CSS/XPath expression.
For example, a :disabled expression filter on the :button selector appends [disabled] to the CSS expression, so the DOM query only returns disabled buttons — rather than returning all buttons and filtering in Ruby.
Expression filters are more efficient than node filters because they reduce the number of elements returned by the driver, but they can only filter on criteria expressible in CSS/XPath.
Usage
Define expression filters inside a selector definition when the filter criteria can be expressed in CSS or XPath. The block receives the current expression and the filter value, and must return a modified expression.
Theoretical Basis
# Abstract expression filter flow (not actual code)
expression_filter(:disabled, :boolean) do |expression, value|
if value
expression + "[disabled]" # narrow CSS before query
else
expression + ":not([disabled])"
end
end
# Pipeline: CSS expression -> apply expression filters -> DOM query