class Capybara::Selector::Definition

def css(*allowed_filters, &block)

Returns:
  • (#call) - The block that will be called to generate the CSS selector

Overloads:
  • css()
  • css(*expression_filters, &block)

Other tags:
    Yieldreturn: - An object that can produce a CSS selector

Other tags:
    Yieldparam: options - The options hash passed to the query
    Yieldparam: locator - The locator string passed to the query

Other tags:
    Yield: - The block to use to generate the CSS selector

Parameters:
  • expression_filters (Array) -- ([]) Names of filters that can be implemented via this CSS selector
def css(*allowed_filters, &block)
  expression(:css, allowed_filters, &block)
end

def custom_filters

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

def default_format

def default_format
  return nil if @expressions.keys.empty?
  if @expressions.size == 1
    @expressions.keys.first
  else
    :xpath
  end
end

def default_visibility(fallback = Capybara.ignore_hidden_elements, options = {})

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

def describe_all_expression_filters(**opts)

def describe_all_expression_filters(**opts)
  expression_filters.map do |ef_name, ef|
    if ef.matcher?
      handled_custom_options(ef, opts).map { |option, value| " with #{ef_name}[#{option} => #{value}]" }.join
    elsif opts.key?(ef_name)
      " with #{ef_name} #{opts[ef_name]}"
    end
  end.join
end

def describe_expression_filters(&block)

def describe_expression_filters(&block)
  if block
    describe(:expression_filters, &block)
  else
    describe(:expression_filters) do |**options|
      describe_all_expression_filters(**options)
    end
  end
end

def describe_node_filters(&block)

def describe_node_filters(&block)
  describe(:node_filters, &block)
end

def expression(type, allowed_filters, &block)

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

def expression_filters

def expression_filters
  @filter_set.expression_filters
end

def filter_set(name, filters_to_use = nil)

def filter_set(name, filters_to_use = nil)
  @filter_set.import(name, filters_to_use)
end

def handled_custom_options(filter, options)

def handled_custom_options(filter, options)
  options.select do |option, _|
    filter.handles_option?(option) && !::Capybara::Queries::SelectorQuery::VALID_KEYS.include?(option)
  end
end

def initialize(name, locator_type: nil, raw_locator: false, supports_exact: nil, &block)

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

def label(label = nil)

Returns:
  • (String) - The currently set label

Overloads:
  • label()
  • label(label)

Parameters:
  • label (String) -- A descriptive label for this selector - used in error messages
def label(label = nil)
  @label = label if label
  @label
end

def locator_filter(*types, **options, &block)

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

def locator_types

Other tags:
    Api: - private
def locator_types
  return nil unless @locator_type
  Array(@locator_type)
end

def match(&block)

Returns:
  • (#call) - The block that will be used to detect selector match

Other tags:
    Yieldreturn: - Whether this selector matches the locator string

Other tags:
    Yieldparam: , - locator The locator string used to determine if it matches the selector

Other tags:
    Yield: - This block takes the passed in locator string and returns whether or not it matches the selector
def match(&block)
  @match = block if block
  @match
end

def match?(locator)

Returns:
  • (Boolean) - Whether or not to use this selector

Parameters:
  • locator (String) -- The locator passed to the query
def match?(locator)
  @match&.call(locator)
end

def node_filters

def node_filters
  @filter_set.node_filters
end

def parameter_names(block)

def parameter_names(block)
  key_types = %i[key keyreq]
  # user filter_map when we drop dupport for 2.6
  # block.parameters.select { |(type, _name)| key_types.include? type }.map { |(_, name)| name }
  block.parameters.filter_map { |(type, name)| name if key_types.include? type }
end

def raw_locator?

Other tags:
    Api: - private
def raw_locator?
  !!@raw_locator
end

def supports_exact?

Other tags:
    Api: - private
def supports_exact?
  @supports_exact
end

def visible(default_visibility = nil, &block)

Parameters:
  • default_visibility (Symbol) -- Only find elements with the specified visibility:
def visible(default_visibility = nil, &block)
  @default_visibility = block || default_visibility
end

def xpath(*allowed_filters, &block)

Returns:
  • (#call) - The block that will be called to generate the XPath expression

Overloads:
  • xpath()
  • xpath(*expression_filters, &block)

Other tags:
    Yieldreturn: - An object that can produce an xpath expression

Other tags:
    Yieldparam: options - The options hash passed to the query
    Yieldparam: locator - The locator string passed to the query

Other tags:
    Yield: - The block to use to generate the XPath expression

Parameters:
  • expression_filters (Array) -- ([]) Names of filters that are implemented via this expression, if not specified the names of any keyword parameters in the block will be used
def xpath(*allowed_filters, &block)
  expression(:xpath, allowed_filters, &block)
end