Class: Capybara::Selector
- Inherits:
-
Object
- Object
- Capybara::Selector
- Extended by:
- Forwardable
- Defined in:
- lib/capybara/selector/selector.rb,
lib/capybara/selector/css.rb,
lib/capybara/selector/filter_set.rb,
lib/capybara/selector/filters/base.rb,
lib/capybara/selector/filters/node_filter.rb,
lib/capybara/selector/filters/expression_filter.rb
Overview
Built-in Selectors
* **:xpath** - Select elements by XPath expression
* Locator: An XPath expression
* **:css** - Select elements by CSS selector
* Locator: A CSS selector
* **:id** - Select element by id
* Locator: The id of the element to match
* **:field** - Select field elements (input [not of type submit, image, or hidden], textarea, select)
* Locator: Matches against the id, name, or placeholder
* Filters:
* :id (String)
Defined Under Namespace
Modules: Filters Classes: CSS, FilterSet
Instance Attribute Summary collapse
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
Instance Method Summary collapse
- #call(locator, **options) ⇒ Object
-
#css(*allowed_filters, &block) ⇒ #call
Define a selector by a CSS selector.
- #custom_filters ⇒ Object
- #default_visibility(fallback = Capybara.ignore_hidden_elements) ⇒ Object
-
#description(options) ⇒ String
Description of the selector when used with the options passed.
- #expression_filter(name, *types, options = {}, &block) ⇒ Object
- #expression_filters ⇒ Object
-
#filter ⇒ Object
See #node_filter.
- #filter_set(name, filters_to_use = nil) ⇒ Object
-
#initialize(name, &block) ⇒ Selector
constructor
A new instance of Selector.
-
#label(label = nil) ⇒ String
Set/get a descriptive label for the selector.
-
#match {|locator| ... } ⇒ #call
Automatic selector detection.
-
#match?(locator) ⇒ Boolean
Should this selector be used for the passed in locator.
- #node_filter(name, *types, options = {}, &block) ⇒ Object
- #node_filters ⇒ Object
-
#visible(default_visibility) ⇒ Object
Set the default visibility mode that shouble be used if no visibile option is passed when using the selector.
-
#xpath(*allowed_filters, &block) ⇒ #call
Define a selector by an xpath expression.
Constructor Details
#initialize(name, &block) ⇒ Selector
Returns a new instance of Selector.
182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/capybara/selector/selector.rb', line 182 def initialize(name, &block) @name = name @filter_set = FilterSet.add(name) {} @match = nil @label = nil = nil @description = nil @format = nil @expression = nil @expression_filters = {} @default_visibility = nil instance_eval(&block) end |
Instance Attribute Details
#format ⇒ Object (readonly)
Returns the value of attribute format.
161 162 163 |
# File 'lib/capybara/selector/selector.rb', line 161 def format @format end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
161 162 163 |
# File 'lib/capybara/selector/selector.rb', line 161 def name @name end |
Class Method Details
.add(name, &block) ⇒ Object
169 170 171 |
# File 'lib/capybara/selector/selector.rb', line 169 def add(name, &block) all[name.to_sym] = ::Selector.new(name.to_sym, &block) end |
.all ⇒ Object
165 166 167 |
# File 'lib/capybara/selector/selector.rb', line 165 def all @selectors ||= {} # rubocop:disable Naming/MemoizedInstanceVariableName end |
.remove(name) ⇒ Object
177 178 179 |
# File 'lib/capybara/selector/selector.rb', line 177 def remove(name) all.delete(name.to_sym) end |
.update(name, &block) ⇒ Object
173 174 175 |
# File 'lib/capybara/selector/selector.rb', line 173 def update(name, &block) all[name.to_sym].instance_eval(&block) end |
Instance Method Details
#call(locator, **options) ⇒ Object
290 291 292 293 294 295 296 |
# File 'lib/capybara/selector/selector.rb', line 290 def call(locator, **) if format @expression.call(locator, ) else warn "Selector has no format" end end |
#css(*expression_filters) {|locator, options| ... } ⇒ #call #css ⇒ #call
Define a selector by a CSS selector
245 246 247 248 249 250 251 |
# File 'lib/capybara/selector/selector.rb', line 245 def css(*allowed_filters, &block) if block @format, @expression = :css, block allowed_filters.flatten.each { |ef| expression_filters[ef] = nil } end format == :css ? @expression : nil end |
#custom_filters ⇒ Object
196 197 198 199 |
# File 'lib/capybara/selector/selector.rb', line 196 def custom_filters warn "Deprecated: Selector#custom_filters is not valid when same named expression and node filter exist - don't use" node_filters.merge(expression_filters).freeze end |
#default_visibility(fallback = Capybara.ignore_hidden_elements) ⇒ Object
374 375 376 377 |
# File 'lib/capybara/selector/selector.rb', line 374 def default_visibility(fallback = .ignore_hidden_elements) return @default_visibility unless @default_visibility.nil? fallback end |
#description(options) ⇒ String
Returns Description of the selector when used with the options passed.
288 |
# File 'lib/capybara/selector/selector.rb', line 288 def_delegator :@filter_set, :description |
#expression_filter(name, *types, options = {}, &block) ⇒ Object
349 |
# File 'lib/capybara/selector/selector.rb', line 349 def_delegators :@filter_set, :node_filter, :expression_filter, :filter |
#expression_filters ⇒ Object
205 206 207 |
# File 'lib/capybara/selector/selector.rb', line 205 def expression_filters @filter_set.expression_filters end |
#filter_set(name, filters_to_use = nil) ⇒ Object
351 352 353 354 355 356 357 |
# File 'lib/capybara/selector/selector.rb', line 351 def filter_set(name, filters_to_use = nil) f_set = FilterSet.all[name] filter_selector = filters_to_use.nil? ? ->(*) { true } : ->(n, _) { filters_to_use.include? n } @filter_set.expression_filters.merge!(f_set.expression_filters.select(&filter_selector)) @filter_set.node_filters.merge!(f_set.node_filters.select(&filter_selector)) f_set.descriptions.each { |desc| @filter_set.describe(&desc) } end |
#label(label) ⇒ String #label ⇒ String
Set/get a descriptive label for the selector
276 277 278 279 |
# File 'lib/capybara/selector/selector.rb', line 276 def label(label = nil) @label = label if label @label end |
#match {|locator| ... } ⇒ #call
Automatic selector detection
262 263 264 265 |
# File 'lib/capybara/selector/selector.rb', line 262 def match(&block) @match = block if block @match end |
#match?(locator) ⇒ Boolean
Should this selector be used for the passed in locator
This is used by the automatic selector selection mechanism when no selector type is passed to a selector query
307 308 309 |
# File 'lib/capybara/selector/selector.rb', line 307 def match?(locator) @match&.call(locator) end |
#node_filter(name, *types, options = {}, &block) ⇒ Object
|
|
# File 'lib/capybara/selector/selector.rb', line 311
|
#node_filters ⇒ Object
201 202 203 |
# File 'lib/capybara/selector/selector.rb', line 201 def node_filters @filter_set.node_filters end |
#visible(default_visibility) ⇒ Object
Set the default visibility mode that shouble be used if no visibile option is passed when using the selector. If not specified will default to the behavior indicated by Capybara.ignore_hidden_elements
370 371 372 |
# File 'lib/capybara/selector/selector.rb', line 370 def visible(default_visibility) @default_visibility = default_visibility end |
#xpath(*expression_filters) {|locator, options| ... } ⇒ #call #xpath ⇒ #call
Define a selector by an xpath expression
223 224 225 226 227 228 229 |
# File 'lib/capybara/selector/selector.rb', line 223 def xpath(*allowed_filters, &block) if block @format, @expression = :xpath, block allowed_filters.flatten.each { |ef| expression_filters[ef] = Filters::IdentityExpressionFilter.new } end format == :xpath ? @expression : nil end |