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 collapse

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 + %i[text id class visible exact exact_text 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.

%i[first smart prefer_exact one].freeze

Constants inherited from BaseQuery

BaseQuery::COUNT_KEYS

Instance Attribute Summary collapse

Attributes inherited from BaseQuery

#session_options

Instance Method Summary collapse

Methods inherited from BaseQuery

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

Constructor Details

#initialize(*args, session_options:, **options, &filter_block) ⇒ 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



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

def initialize(*args, session_options:, **options, &filter_block)
  @resolved_node = nil
  @options = options.dup
  super(@options)
  self.session_options = session_options
  @filter_block = filter_block

  @selector = if args[0].is_a?(Symbol)
    Selector.all.fetch(args.shift) do |selector_type|
      raise ArgumentError, "Unknown selector type (:#{selector_type})"
    end
  else
    Selector.all.values.find { |s| s.match?(args[0]) }
  end
  @locator = args.shift
  @selector ||= Selector.all[session_options.default_selector]

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

  @expression = @selector.call(@locator, @options.merge(enable_aria_label: session_options.enable_aria_label))

  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.



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

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.



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

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.



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

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.



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

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.



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

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.



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

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.



93
94
95
# File 'lib/capybara/queries/selector_query.rb', line 93

def css
  filtered_css(apply_expression_filters(@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.



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

def description
  @description = "".dup
  @description << "visible " if visible == :visible
  @description << "non-visible " if visible == :hidden
  @description << "#{label} #{locator.inspect}"
  @description << " with#{" exact" if exact_text == true} text #{options[:text].inspect}" if options[:text]
  @description << " with exact text #{options[:exact_text]}" if options[:exact_text].is_a?(String)
  @description << " with id #{options[:id]}" if options[:id]
  @description << " with classes [#{Array(options[:class]).join(',')}]" if options[:class]
  @description << selector.description(options)
  @description << " that also matches the custom filter block" if @filter_block
  @description << " within #{@resolved_node.inspect}" if describe_within?
  @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.

Returns:

  • (Boolean)


77
78
79
80
# File 'lib/capybara/queries/selector_query.rb', line 77

def exact?
  return false unless supports_exact?
  options.fetch(:exact, session_options.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.



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

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.



82
83
84
# File 'lib/capybara/queries/selector_query.rb', line 82

def match
  options.fetch(:match, session_options.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.

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/capybara/queries/selector_query.rb', line 55

def matches_filters?(node)
  return false if options[:text] && !matches_text_filter(node, options[:text])
  return false if exact_text.is_a?(String) && !matches_exact_text_filter(node, exact_text)

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

  matches_node_filters?(node) && matches_filter_block?(node)
rescue *(node.respond_to?(:session) ? node.session.driver.invalid_element_errors : [])
  return false
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.



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

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.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/capybara/queries/selector_query.rb', line 98

def resolve_for(node, exact = nil)
  @resolved_node = node
  node.synchronize do
    children = if selector.format == :css
      node.find_css(css)
    else
      node.find_xpath(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.

Returns:

  • (Boolean)


117
118
119
# File 'lib/capybara/queries/selector_query.rb', line 117

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.



69
70
71
72
73
74
75
# File 'lib/capybara/queries/selector_query.rb', line 69

def visible
  case (vis = options.fetch(:visible) { @selector.default_visibility(session_options.ignore_hidden_elements) })
  when true then :visible
  when false then :all
  else vis
  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.



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

def xpath(exact = nil)
  exact = exact? if exact.nil?
  expr = apply_expression_filters(@expression)
  expr = exact ? expr.to_xpath(:exact) : expr.to_s if expr.respond_to?(:to_xpath)
  filtered_xpath(expr)
end