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, Capybara.test_id attribute, name, or placeholder
* Filters:
* :id (String) — Matches the id attribute
* :name (String) — Matches the name attribute
* :placeholder (String) — Matches the placeholder attribute
* :type (String) — Matches the type attribute of the field or element type for 'textarea' and 'select'
* :readonly (Boolean)
* :with (String) — Matches the current value of the field
* :class (String, Array<String>) — Matches the class(es) provided
* :checked (Boolean) — Match checked fields?
* :unchecked (Boolean) — Match unchecked fields?
* :disabled (Boolean) — Match disabled field?
* :multiple (Boolean) — Match fields that accept multiple values
* **:fieldset** - Select fieldset elements
* Locator: Matches id or contents of wrapped legend
* Filters:
* :id (String) — Matches id attribute
* :legend (String) — Matches contents of wrapped legend
* :class (String, Array<String>) — Matches the class(es) provided
* **:link** - Find links ( <a> elements with an href attribute )
* Locator: Matches the id or title attributes, or the string content of the link, or the alt attribute of a contained img element
* Filters:
* :id (String) — Matches the id attribute
* :title (String) — Matches the title attribute
* :alt (String) — Matches the alt attribute of a contained img element
* :class (String) — Matches the class(es) provided
* :href (String, Regexp, nil) — Matches the normalized href of the link, if nil will find <a> elements with no href attribute
* **:button** - Find buttons ( input [of type submit, reset, image, button] or button elements )
* Locator: Matches the id, Capybara.test_id attribute, value, or title attributes, string content of a button, or the alt attribute of an image type button or of a descendant image of a button
* Filters:
* :id (String) — Matches the id attribute
* :title (String) — Matches the title attribute
* :class (String) — Matches the class(es) provided
* :value (String) — Matches the value of an input button
* :type
* **:link_or_button** - Find links or buttons
* Locator: See :link and :button selectors
* **:fillable_field** - Find text fillable fields ( textarea, input [not of type submit, image, radio, checkbox, hidden, file] )
* Locator: Matches against the id, Capybara.test_id attribute, name, or placeholder
* Filters:
* :id (String) — Matches the id attribute
* :name (String) — Matches the name attribute
* :placeholder (String) — Matches the placeholder attribute
* :with (String) — Matches the current value of the field
* :type (String) — Matches the type attribute of the field or element type for 'textarea'
* :class (String, Array<String>) — Matches the class(es) provided
* :disabled (Boolean) — Match disabled field?
* :multiple (Boolean) — Match fields that accept multiple values
* **:radio_button** - Find radio buttons
* Locator: Match id, Capybara.test_id attribute, name, or associated label text
* Filters:
* :id (String) — Matches the id attribute
* :name (String) — Matches the name attribute
* :class (String, Array<String>) — Matches the class(es) provided
* :checked (Boolean) — Match checked fields?
* :unchecked (Boolean) — Match unchecked fields?
* :disabled (Boolean) — Match disabled field?
* :option (String) — Match the value
* **:checkbox** - Find checkboxes
* Locator: Match id, Capybara.test_id attribute, name, or associated label text
* Filters:
* *:id (String) — Matches the id attribute
* *:name (String) — Matches the name attribute
* *:class (String, Array<String>) — Matches the class(es) provided
* *:checked (Boolean) — Match checked fields?
* *:unchecked (Boolean) — Match unchecked fields?
* *:disabled (Boolean) — Match disabled field?
* *:option (String) — Match the value
* **:select** - Find select elements
* Locator: Match id, Capybara.test_id attribute, name, placeholder, or associated label text
* Filters:
* :id (String) — Matches the id attribute
* :name (String) — Matches the name attribute
* :placeholder (String) — Matches the placeholder attribute
* :class (String, Array<String>) — Matches the class(es) provided
* :disabled (Boolean) — Match disabled field?
* :multiple (Boolean) — Match fields that accept multiple values
* :options (Array<String>) — Exact match options
* :with_options (Array<String>) — Partial match options
* :selected (String, Array<String>) — Match the selection(s)
* :with_selected (String, Array<String>) — Partial match the selection(s)
* **:option** - Find option elements
* Locator: Match text of option
* Filters:
* :disabled (Boolean) — Match disabled option
* :selected (Boolean) — Match selected option
* **:datalist_input**
* Locator:
* Filters:
* :disabled
* :name
* :placeholder
* **:datalist_option**
* Locator:
* **:file_field** - Find file input elements
* Locator: Match id, Capybara.test_id attribute, name, or associated label text
* Filters:
* :id (String) — Matches the id attribute
* :name (String) — Matches the name attribute
* :class (String, Array<String>) — Matches the class(es) provided
* :disabled (Boolean) — Match disabled field?
* :multiple (Boolean) — Match field that accepts multiple values
* **:label** - Find label elements
* Locator: Match id or text contents
* Filters:
* :for (Element, String) — The element or id of the element associated with the label
* **:table** - Find table elements
* Locator: id or caption text of table
* Filters:
* :id (String) — Match id attribute of table
* :caption (String) — Match text of associated caption
* :class (String, Array<String>) — Matches the class(es) provided
* **:frame** - Find frame/iframe elements
* Locator: Match id or name
* Filters:
* :id (String) — Match id attribute
* :name (String) — Match name attribute
* :class (String, Array<String>) — Matches the class(es) provided
* **:element**
* Locator: Type of element ('div', 'a', etc) - if not specified defaults to '*'
* Filters: Matches on any element attribute
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, selector_config: {}, **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
- #describe_expression_filters(&block) ⇒ Object
- #describe_node_filters(&block) ⇒ 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 195 196 197 198 |
# File 'lib/capybara/selector/selector.rb', line 182 def initialize(name, &block) @name = name @filter_set = FilterSet.add(name) {} @match = nil @label = nil @failure_message = nil @description = nil @format = nil @expression = nil @expression_filters = {} @default_visibility = nil @config = { enable_aria_label: false, test_id: 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] = Capybara::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, selector_config: {}, **options) ⇒ Object
294 295 296 297 298 299 300 301 |
# File 'lib/capybara/selector/selector.rb', line 294 def call(locator, selector_config: {}, **) @config.merge! selector_config 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
249 250 251 252 253 254 255 |
# File 'lib/capybara/selector/selector.rb', line 249 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
200 201 202 203 |
# File 'lib/capybara/selector/selector.rb', line 200 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
389 390 391 392 |
# File 'lib/capybara/selector/selector.rb', line 389 def default_visibility(fallback = Capybara.ignore_hidden_elements) return @default_visibility unless @default_visibility.nil? fallback end |
#describe_expression_filters(&block) ⇒ Object
362 363 364 365 366 367 368 369 370 |
# File 'lib/capybara/selector/selector.rb', line 362 def describe_expression_filters(&block) if block_given? describe(:expression_filters, &block) else describe(:expression_filters) do |**| describe_all_expression_filters() end end end |
#describe_node_filters(&block) ⇒ Object
372 373 374 |
# File 'lib/capybara/selector/selector.rb', line 372 def describe_node_filters(&block) describe(:node_filters, &block) end |
#description(options) ⇒ String
Returns Description of the selector when used with the options passed.
292 |
# File 'lib/capybara/selector/selector.rb', line 292 def_delegator :@filter_set, :description |
#expression_filter(name, *types, options = {}, &block) ⇒ Object
354 |
# File 'lib/capybara/selector/selector.rb', line 354 def_delegators :@filter_set, :node_filter, :expression_filter, :filter |
#expression_filters ⇒ Object
209 210 211 |
# File 'lib/capybara/selector/selector.rb', line 209 def expression_filters @filter_set.expression_filters end |
#filter_set(name, filters_to_use = nil) ⇒ Object
356 357 358 |
# File 'lib/capybara/selector/selector.rb', line 356 def filter_set(name, filters_to_use = nil) @filter_set.import(name, filters_to_use) end |
#label(label) ⇒ String #label ⇒ String
Set/get a descriptive label for the selector
280 281 282 283 |
# File 'lib/capybara/selector/selector.rb', line 280 def label(label = nil) @label = label if label @label end |
#match {|locator| ... } ⇒ #call
Automatic selector detection
266 267 268 269 |
# File 'lib/capybara/selector/selector.rb', line 266 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
312 313 314 |
# File 'lib/capybara/selector/selector.rb', line 312 def match?(locator) @match&.call(locator) end |
#node_filter(name, *types, options = {}, &block) ⇒ Object
|
# File 'lib/capybara/selector/selector.rb', line 316
|
#node_filters ⇒ Object
205 206 207 |
# File 'lib/capybara/selector/selector.rb', line 205 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
385 386 387 |
# File 'lib/capybara/selector/selector.rb', line 385 def visible(default_visibility) @default_visibility = default_visibility end |
#xpath(*expression_filters) {|locator, options| ... } ⇒ #call #xpath ⇒ #call
Define a selector by an xpath expression
227 228 229 230 231 232 233 |
# File 'lib/capybara/selector/selector.rb', line 227 def xpath(*allowed_filters, &block) if block @format, @expression = :xpath, block allowed_filters.flatten.each { |ef| expression_filters[ef] = Filters::IdentityExpressionFilter.new(ef) } end format == :xpath ? @expression : nil end |