class Capybara::Selector::Definition
Attributes
Public Class Methods
# File lib/capybara/selector/definition.rb, line 15 def initialize(name, locator_type: nil, raw_locator: false, supports_exact: nil, &block) @name = name @filter_set = Capybara::Selector::FilterSet.add(name) {} @match = nil @label = nil @failure_message = nil @expressions = {} @expression_filters = {} @locator_filter = nil @default_visibility = nil @locator_type = locator_type @raw_locator = raw_locator @supports_exact = supports_exact instance_eval(&block) end
Public Instance Methods
Description of the selector
@!method description(options)
@param [Hash] options The options of the query used to generate the description @return [String] Description of the selector when used with the options passed
# File lib/capybara/selector/definition.rb, line 115 def_delegator :@filter_set, :description
Define a selector by a CSS
selector
@overload css(*expression_filters, &block)
@param [Array<Symbol>] expression_filters ([]) Names of filters that can be implemented via this CSS selector @yield [locator, options] The block to use to generate the CSS selector @yieldparam [String] locator The locator string passed to the query @yieldparam [Hash] options The options hash passed to the query @yieldreturn [#to_s] An object that can produce a CSS selector
@overload css() @return [#call] The block that will be called to generate the CSS
selector
# File lib/capybara/selector/definition.rb, line 76 def css(*allowed_filters, &block) expression(:css, allowed_filters, &block) end
# File lib/capybara/selector/definition.rb, line 31 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
# File lib/capybara/selector/definition.rb, line 235 def default_format return nil if @expressions.keys.empty? if @expressions.size == 1 @expressions.keys.first else :xpath end end
# File lib/capybara/selector/definition.rb, line 216 def default_visibility(fallback = Capybara.ignore_hidden_elements, options = {}) vis = if @default_visibility&.respond_to?(:call) @default_visibility.call(options) else @default_visibility end vis.nil? ? fallback : vis end
# File lib/capybara/selector/definition.rb, line 189 def describe_all_expression_filters(**opts) expression_filters.map do |ef_name, ef| if ef.matcher? handled_custom_keys(ef, opts.keys).map { |key| " with #{ef_name}[#{key} => #{opts[key]}]" }.join elsif opts.key?(ef_name) " with #{ef_name} #{opts[ef_name]}" end end.join end
# File lib/capybara/selector/definition.rb, line 179 def describe_expression_filters(&block) if block_given? describe(:expression_filters, &block) else describe(:expression_filters) do |**options| describe_all_expression_filters(options) end end end
# File lib/capybara/selector/definition.rb, line 199 def describe_node_filters(&block) describe(:node_filters, &block) end
# File lib/capybara/selector/definition.rb, line 40 def expression_filters @filter_set.expression_filters end
# File lib/capybara/selector/definition.rb, line 173 def filter_set(name, filters_to_use = nil) @filter_set.import(name, filters_to_use) end
Set/get a descriptive label for the selector
@overload label(label)
@param [String] label A descriptive label for this selector - used in error messages
@overload label() @return [String] The currently set label
# File lib/capybara/selector/definition.rb, line 103 def label(label = nil) @label = label if label @label end
# File lib/capybara/selector/definition.rb, line 167 def locator_filter(*types, **options, &block) types.each { |type| options[type] = true } @locator_filter = Capybara::Selector::Filters::LocatorFilter.new(block, options) if block @locator_filter end
@api private
# File lib/capybara/selector/definition.rb, line 246 def locator_types return nil unless @locator_type Array(@locator_type) end
Automatic selector detection
@yield [locator] This block takes the passed in locator string and returns whether or not it matches the selector @yieldparam [String], locator The locator string used to determin if it matches the selector @yieldreturn [Boolean] Whether this selector matches the locator string @return [#call] The block that will be used to detect selector match
# File lib/capybara/selector/definition.rb, line 89 def match(&block) @match = block if block @match end
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
@param [String] locator The locator passed to the query @return [Boolean] Whether or not to use this selector
# File lib/capybara/selector/definition.rb, line 126 def match?(locator) @match&.call(locator) end
# File lib/capybara/selector/definition.rb, line 36 def node_filters @filter_set.node_filters end
@api private
# File lib/capybara/selector/definition.rb, line 226 def raw_locator? !!@raw_locator end
@api private
# File lib/capybara/selector/definition.rb, line 231 def supports_exact? @supports_exact end
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
@param [Symbol] default_visibility
Only find elements with the specified visibility:
* :all - finds visible and invisible elements. * :hidden - only finds invisible elements. * :visible - only finds visible elements.
# File lib/capybara/selector/definition.rb, line 212 def visible(default_visibility = nil, &block) @default_visibility = block || default_visibility end
Define a selector by an xpath expression
@overload xpath(*expression_filters, &block)
@param [Array<Symbol>] expression_filters ([]) Names of filters that are implemented via this expression, if not specified the names of any keyword parameters in the block will be used @yield [locator, options] The block to use to generate the XPath expression @yieldparam [String] locator The locator string passed to the query @yieldparam [Hash] options The options hash passed to the query @yieldreturn [#to_xpath, #to_s] An object that can produce an xpath expression
@overload xpath() @return [#call] The block that will be called to generate the XPath
expression
# File lib/capybara/selector/definition.rb, line 58 def xpath(*allowed_filters, &block) expression(:xpath, allowed_filters, &block) end
Private Instance Methods
# File lib/capybara/selector/definition.rb, line 264 def expression(type, allowed_filters, &block) if block @expressions[type] = block allowed_filters = parameter_names(block) if allowed_filters.empty? allowed_filters.flatten.each do |ef| expression_filters[ef] = Capybara::Selector::Filters::IdentityExpressionFilter.new(ef) end end @expressions[type] end
# File lib/capybara/selector/definition.rb, line 254 def handled_custom_keys(filter, keys) keys.select do |key| filter.handles_option?(key) && !::Capybara::Queries::SelectorQuery::VALID_KEYS.include?(key) end end
# File lib/capybara/selector/definition.rb, line 260 def parameter_names(block) block.parameters.select { |(type, _name)| %i[key keyreq].include? type }.map { |(_type, name)| name } end