Class: Capybara::Queries::SelectorQuery Private

Inherits:
BaseQuery
  • Object
show all
Defined in:
lib/capybara/queries/selector_query.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Direct Known Subclasses

MatchQuery

Constant Summary

VALID_KEYS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

COUNT_KEYS + [:text, :visible, :exact, :match, :wait, :filter_set]
VALID_MATCH =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

[:first, :smart, :prefer_exact, :one]

Constants inherited from BaseQuery

BaseQuery::COUNT_KEYS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseQuery

#expects_none?, #failure_message, #matches_count?, #negative_failure_message, #wait, wait

Constructor Details

#initialize(*args) ⇒ SelectorQuery

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of SelectorQuery



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/capybara/queries/selector_query.rb', line 10

def initialize(*args)
  @options = if args.last.is_a?(Hash) then args.pop.dup else {} end

  if args[0].is_a?(Symbol)
    @selector = Selector.all[args.shift]
    @locator = args.shift
  else
    @selector = Selector.all.values.find { |s| s.match?(args[0]) }
    @locator = args.shift
  end
  @selector ||= Selector.all[Capybara.default_selector]

  warn "Unused parameters passed to #{self.class.name} : #{args.to_s}" unless args.empty?

  # for compatibility with Capybara 2.0
  if Capybara.exact_options and @selector == Selector.all[:option]
    @options[:exact] = true
  end

  @expression = @selector.call(@locator, @options)

  warn_exact_usage

  assert_valid_keys
end

Instance Attribute Details

#expressionObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



5
6
7
# File 'lib/capybara/queries/selector_query.rb', line 5

def expression
  @expression
end

#findObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



5
6
7
# File 'lib/capybara/queries/selector_query.rb', line 5

def find
  @find
end

#locatorObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



5
6
7
# File 'lib/capybara/queries/selector_query.rb', line 5

def locator
  @locator
end

#negativeObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



5
6
7
# File 'lib/capybara/queries/selector_query.rb', line 5

def negative
  @negative
end

#optionsObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



5
6
7
# File 'lib/capybara/queries/selector_query.rb', line 5

def options
  @options
end

#selectorObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



5
6
7
# File 'lib/capybara/queries/selector_query.rb', line 5

def selector
  @selector
end

Instance Method Details

#cssObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



102
103
104
# File 'lib/capybara/queries/selector_query.rb', line 102

def css
  @expression
end

#descriptionObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



39
40
41
42
43
44
# File 'lib/capybara/queries/selector_query.rb', line 39

def description
  @description = String.new("#{label} #{locator.inspect}")
  @description << " with text #{options[:text].inspect}" if options[:text]
  @description << selector.description(options)
  @description
end

#exact?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



84
85
86
87
# File 'lib/capybara/queries/selector_query.rb', line 84

def exact?
  return false if !supports_exact?
  options.fetch(:exact, Capybara.exact)
end

#labelObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



37
# File 'lib/capybara/queries/selector_query.rb', line 37

def label; selector.label or selector.name; end

#matchObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



89
90
91
# File 'lib/capybara/queries/selector_query.rb', line 89

def match
  options.fetch(:match, Capybara.match)
end

#matches_filters?(node) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/capybara/queries/selector_query.rb', line 46

def matches_filters?(node)
  if options[:text]
    regexp = options[:text].is_a?(Regexp) ? options[:text] : Regexp.escape(options[:text].to_s)
    return false if not node.text(visible).match(regexp)
  end

  case visible
    when :visible then return false unless node.visible?
    when :hidden then return false if node.visible?
  end

  query_filters.all? do |name, filter|
    if options.has_key?(name)
      filter.matches?(node, options[name])
    elsif filter.default?
      filter.matches?(node, filter.default)
    else
      true
    end
  end
end

#nameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



36
# File 'lib/capybara/queries/selector_query.rb', line 36

def name; selector.name; end

#resolve_for(node, exact = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/capybara/queries/selector_query.rb', line 107

def resolve_for(node, exact = nil)
  node.synchronize do
    children = if selector.format == :css
      node.find_css(self.css)
    else
      node.find_xpath(self.xpath(exact))
    end.map do |child|
      if node.is_a?(Capybara::Node::Base)
        Capybara::Node::Element.new(node.session, child, node, self)
      else
        Capybara::Node::Simple.new(child)
      end
    end
    Capybara::Result.new(children, self)
  end
end

#supports_exact?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



125
126
127
# File 'lib/capybara/queries/selector_query.rb', line 125

def supports_exact?
  @expression.respond_to? :to_xpath
end

#visibleObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/capybara/queries/selector_query.rb', line 68

def visible
  if options.has_key?(:visible)
    case @options[:visible]
      when true then :visible
      when false then :all
      else @options[:visible]
    end
  else
    if Capybara.ignore_hidden_elements
      :visible
    else
      :all
    end
  end
end

#xpath(exact = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



93
94
95
96
97
98
99
100
# File 'lib/capybara/queries/selector_query.rb', line 93

def xpath(exact=nil)
  exact = self.exact? if exact.nil?
  if @expression.respond_to?(:to_xpath) and exact
    @expression.to_xpath(:exact)
  else
    @expression.to_s
  end
end