Class: Capybara::Selector

Inherits:
Object
  • Object
show all
Defined in:
lib/capybara/selector/filter.rb,
lib/capybara/selector/selector.rb,
lib/capybara/selector/filter_set.rb

Defined Under Namespace

Classes: Filter, FilterSet

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ Selector



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/capybara/selector/selector.rb', line 42

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 = []
  instance_eval(&block)
end

Instance Attribute Details

#expression_filtersObject (readonly)

Returns the value of attribute expression_filters



22
23
24
# File 'lib/capybara/selector/selector.rb', line 22

def expression_filters
  @expression_filters
end

#formatObject (readonly)

Returns the value of attribute format



22
23
24
# File 'lib/capybara/selector/selector.rb', line 22

def format
  @format
end

#nameObject (readonly)

Returns the value of attribute name



22
23
24
# File 'lib/capybara/selector/selector.rb', line 22

def name
  @name
end

Class Method Details

.add(name, &block) ⇒ Object



29
30
31
# File 'lib/capybara/selector/selector.rb', line 29

def add(name, &block)
  all[name.to_sym] = Capybara::Selector.new(name.to_sym, &block)
end

.allObject



25
26
27
# File 'lib/capybara/selector/selector.rb', line 25

def all
  @selectors ||= {}
end

.remove(name) ⇒ Object



37
38
39
# File 'lib/capybara/selector/selector.rb', line 37

def remove(name)
  all.delete(name.to_sym)
end

.update(name, &block) ⇒ Object



33
34
35
# File 'lib/capybara/selector/selector.rb', line 33

def update(name, &block)
  all[name.to_sym].instance_eval(&block)
end

Instance Method Details

#call(locator, options = {}) ⇒ Object



136
137
138
139
140
141
142
143
# File 'lib/capybara/selector/selector.rb', line 136

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) {|locator, options| ... } ⇒ #call #css#call

Define a selector by a CSS selector

Overloads:

  • #css(*expression_filters) {|locator, options| ... } ⇒ #call

    Yields:

    • (locator, options)

      The block to use to generate the CSS selector

    Yield Parameters:

    • locator (String)

      The locator string passed to the query

    • options (Hash)

      The options hash passed to the query

    Yield Returns:

    • (#to_s)

      An object that can produce a CSS selector



92
93
94
95
# File 'lib/capybara/selector/selector.rb', line 92

def css(*expression_filters, &block)
  @format, @expression_filters, @expression = :css, expression_filters.flatten, block if block
  format == :css ? @expression : nil
end

#custom_filtersObject



55
56
57
# File 'lib/capybara/selector/selector.rb', line 55

def custom_filters
  @filter_set.filters
end

#describe(&block) ⇒ Object



184
185
186
# File 'lib/capybara/selector/selector.rb', line 184

def describe &block
  @filter_set.describe &block
end

#description(options = {}) ⇒ String

Description of the selector



132
133
134
# File 'lib/capybara/selector/selector.rb', line 132

def description(options={})
  @filter_set.description(options)
end

#filter(name, *types, options = {}, &block) ⇒ Object

Define a non-expression filter for use with this selector

Options Hash (options):

  • :valid_values (Array<>)

    Valid values for this filter

  • :default (Object)

    The default value of the filter (if any)

  • :skip_if (Object)

    Value of the filter that will cause it to be skipped



170
171
172
173
174
# File 'lib/capybara/selector/selector.rb', line 170

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) ⇒ Object



176
177
178
179
180
181
182
# File 'lib/capybara/selector/selector.rb', line 176

def filter_set(name, filters_to_use = nil)
  f_set = FilterSet.all[name]
  f_set.filters.each do | name, filter |
    custom_filters[name] = filter if filters_to_use.nil? || filters_to_use.include?(name)
  end
  f_set.descriptions.each { |desc| @filter_set.describe &desc }
end

#label(label) ⇒ String #labelString

Set/get a descriptive label for the selector



120
121
122
123
# File 'lib/capybara/selector/selector.rb', line 120

def label(label=nil)
  @label = label if label
  @label
end

#match {|locator| ... } ⇒ #call

Automatic selector detection

Yields:

  • (locator)

    This block takes the passed in locator string and returns whether or not it matches the selector

Yield Parameters:

  • , (String)

    locator The locator string used to determin if it matches the selector

Yield Returns:

  • (Boolean)

    Whether this selector matches the locator string



106
107
108
109
# File 'lib/capybara/selector/selector.rb', line 106

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


154
155
156
# File 'lib/capybara/selector/selector.rb', line 154

def match?(locator)
  @match and @match.call(locator)
end

#xpath(*expression_filters) {|locator, options| ... } ⇒ #call #xpath#call

Define a selector by an xpath expression

Overloads:

  • #xpath(*expression_filters) {|locator, options| ... } ⇒ #call

    Yields:

    • (locator, options)

      The block to use to generate the XPath expression

    Yield Parameters:

    • locator (String)

      The locator string passed to the query

    • options (Hash)

      The options hash passed to the query

    Yield Returns:

    • (#to_xpath, #to_s)

      An object that can produce an xpath expression



73
74
75
76
# File 'lib/capybara/selector/selector.rb', line 73

def xpath(*expression_filters, &block)
  @format, @expression_filters, @expression = :xpath, expression_filters.flatten, block if block
  format == :xpath ? @expression : nil
end