class Capybara::Selector

Attributes

format[R]
name[R]

Public Class Methods

add(name, &block) click to toggle source
# File lib/capybara/selector/selector.rb, line 31
def add(name, &block)
  all[name.to_sym] = Capybara::Selector.new(name.to_sym, &block)
end
all() click to toggle source
# File lib/capybara/selector/selector.rb, line 27
def all
  @selectors ||= {}
end
new(name, &block) click to toggle source
# File lib/capybara/selector/selector.rb, line 44
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
  instance_eval(&block)
end
remove(name) click to toggle source
# File lib/capybara/selector/selector.rb, line 39
def remove(name)
  all.delete(name.to_sym)
end
update(name, &block) click to toggle source
# File lib/capybara/selector/selector.rb, line 35
def update(name, &block)
  all[name.to_sym].instance_eval(&block)
end

Public Instance Methods

call(locator, options={}) click to toggle source
# File lib/capybara/selector/selector.rb, line 153
def call(locator, options={})
  if format
    # @expression.call(locator, options.select {|k,v| @expression_filters.include?(k)})
    @expression.call(locator, options)
  else
    warn "Selector has no format"
  end
end
css(*expression_filters, &block) click to toggle source

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/selector.rb, line 106
def css(*expression_filters, &block)
  if block
    @format, @expression = :css, block
    expression_filters.flatten.each { |ef| custom_filters[ef] = nil }
  end
  format == :css ? @expression : nil
end
custom_filters() click to toggle source
# File lib/capybara/selector/selector.rb, line 58
def custom_filters
  @filter_set.filters
end
default_visibility(fallback = Capybara.ignore_hidden_elements) click to toggle source
# File lib/capybara/selector/selector.rb, line 224
def default_visibility(fallback = Capybara.ignore_hidden_elements)
  if @default_visibility.nil?
    fallback
  else
    @default_visibility
  end
end
describe(&block) click to toggle source
# File lib/capybara/selector/selector.rb, line 207
def describe &block
  @filter_set.describe(&block)
end
description(options={}) click to toggle source

Description of the selector

@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/selector.rb, line 149
def description(options={})
  @filter_set.description(options)
end
expression_filter(name, *types_and_options, &block) click to toggle source
# File lib/capybara/selector/selector.rb, line 193
def expression_filter(name, *types_and_options, &block)
  options = types_and_options.last.is_a?(Hash) ? types_and_options.pop.dup : {}
  types_and_options.each { |k| options[k] = true }
  custom_filters[name] = ExpressionFilter.new(name, block, options)
end
expression_filters() click to toggle source
# File lib/capybara/selector/selector.rb, line 66
def expression_filters
  @filter_set.expression_filters
end
filter(name, *types_and_options, &block) click to toggle source

Define a non-expression filter for use with this selector

@overload filter(name, *types, options={}, &block)

@param [Symbol] name            The filter name
@param [Array<Symbol>] types    The types of the filter - currently valid types are [:boolean]
@param [Hash] options ({})      Options of the filter
@option options [Array<>] :valid_values Valid values for this filter
@option options :default        The default value of the filter (if any)
@option options :skip_if        Value of the filter that will cause it to be skipped
# File lib/capybara/selector/selector.rb, line 187
def filter(name, *types_and_options, &block)
  options = types_and_options.last.is_a?(Hash) ? types_and_options.pop.dup : {}
  types_and_options.each { |k| options[k] = true }
  custom_filters[name] = Filter.new(name, block, options)
end
filter_set(name, filters_to_use = nil) click to toggle source
# File lib/capybara/selector/selector.rb, line 199
def filter_set(name, filters_to_use = nil)
  f_set = FilterSet.all[name]
  f_set.filters.each do |n, filter|
    custom_filters[n] = filter if filters_to_use.nil? || filters_to_use.include?(n)
  end
  f_set.descriptions.each { |desc| @filter_set.describe(&desc) }
end
label(label=nil) click to toggle source

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/selector.rb, line 137
def label(label=nil)
  @label = label if label
  @label
end
match(&block) click to toggle source

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/selector.rb, line 123
def match(&block)
  @match = block if block
  @match
end
match?(locator) click to toggle source
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/selector.rb, line 171
def match?(locator)
  @match and @match.call(locator)
end
node_filters() click to toggle source
# File lib/capybara/selector/selector.rb, line 62
def node_filters
  @filter_set.node_filters
end
visible(default_visibility) click to toggle source

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/selector.rb, line 220
def visible(default_visibility)
  @default_visibility = default_visibility
end
xpath(*expression_filters, &block) click to toggle source

Define a selector by an xpath expression

@overload xpath(*expression_filters, &block)

@param [Array<Symbol>] expression_filters ([])  Names of filters that can be implemented via this expression
@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/selector.rb, line 84
def xpath(*expression_filters, &block)
  if block
    @format, @expression = :xpath, block
    expression_filters.flatten.each { |ef| custom_filters[ef] = IdentityExpressionFilter.new }
  end
  format == :xpath ? @expression : nil
end

Private Instance Methods

describe_all_expression_filters(opts={}) click to toggle source
# File lib/capybara/selector/selector.rb, line 252
def describe_all_expression_filters(opts={})
  expression_filters.map { |ef| " with #{ef} #{opts[ef]}" if opts.has_key?(ef) }.join
end
find_by_attr(attribute, value) click to toggle source
# File lib/capybara/selector/selector.rb, line 256
def find_by_attr(attribute, value)
  finder_name = "find_by_#{attribute}_attr"
  if respond_to?(finder_name, true)
    send(finder_name, value)
  else
    value ? XPath.attr(attribute).equals(value) : nil
  end
end
find_by_class_attr(classes) click to toggle source
# File lib/capybara/selector/selector.rb, line 265
def find_by_class_attr(classes)
  if classes
    Array(classes).map do |klass|
      "contains(concat(' ',normalize-space(@class),' '),' #{klass} ')"
    end.join(" and ").to_sym
  else
    nil
  end
end
locate_field(xpath, locator, options={}) click to toggle source
# File lib/capybara/selector/selector.rb, line 234
def locate_field(xpath, locator, options={})
  locate_xpath = xpath #need to save original xpath for the label wrap
  if locator
    locator = locator.to_s
    attr_matchers =  XPath.attr(:id).equals(locator).or(
                     XPath.attr(:name).equals(locator)).or(
                     XPath.attr(:placeholder).equals(locator)).or(
                     XPath.attr(:id).equals(XPath.anywhere(:label)[XPath.string.n.is(locator)].attr(:for)))
    attr_matchers = attr_matchers.or XPath.attr(:'aria-label').is(locator) if options[:enable_aria_label]

    locate_xpath = locate_xpath[attr_matchers]
    locate_xpath = locate_xpath.union(XPath.descendant(:label)[XPath.string.n.is(locator)].descendant(xpath))
  end

  # locate_xpath = [:name, :placeholder].inject(locate_xpath) { |memo, ef| memo[find_by_attr(ef, options[ef])] }
  locate_xpath
end