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

AncestorQuery, 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 normalize_ws 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?, #matches_count?, wait, #wait

Constructor Details

#initialize(*args, session_options:, enable_aria_label: session_options.enable_aria_label, test_id: session_options.test_id, **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.

Raises:

  • (ArgumentError)


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

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

  @selector = find_selector(args[0].is_a?(Symbol) ? args.shift : args[0])
  @locator = args.shift
  @filter_block = filter_block

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

  @expression = selector.call(@locator, @options.merge(selector_config: { enable_aria_label: enable_aria_label, test_id: test_id }))

  warn_exact_usage

  assert_valid_keys
end

Instance Attribute Details

#expressionObject (readonly)

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

#locatorObject (readonly)

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

#optionsObject (readonly)

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 (readonly)

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

#applied_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.



56
57
58
# File 'lib/capybara/queries/selector_query.rb', line 56

def applied_description
  description(true)
end

#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.



95
96
97
# File 'lib/capybara/queries/selector_query.rb', line 95

def css
  filtered_css(apply_expression_filters(@expression))
end

#description(applied = false) ⇒ 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.



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

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


80
81
82
# File 'lib/capybara/queries/selector_query.rb', line 80

def exact?
  supports_exact? ? options.fetch(:exact, session_options.exact) : false
end

#failure_messageObject

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.



114
115
116
# File 'lib/capybara/queries/selector_query.rb', line 114

def failure_message
  +"expected to find #{applied_description}" << count_message
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.



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

def label; selector.label || 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.



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

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)


60
61
62
63
64
65
66
67
68
69
70
# File 'lib/capybara/queries/selector_query.rb', line 60

def matches_filters?(node)
  return true if (@resolved_node&.== node) && options[:allow_self]

  @applied_filters ||= :system
  return false unless matches_system_filters?(node)

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



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

def name; selector.name; end

#negative_failure_messageObject

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.



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

def negative_failure_message
  +"expected not to find #{applied_description}" << count_message
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.



100
101
102
103
104
105
106
107
# File 'lib/capybara/queries/selector_query.rb', line 100

def resolve_for(node, exact = nil)
  @applied_filters = false
  @resolved_node = node
  node.synchronize do
    children = find_nodes_by_selector_format(node, exact).map(&method(:to_element))
    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)


110
111
112
# File 'lib/capybara/queries/selector_query.rb', line 110

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.



72
73
74
75
76
77
78
# File 'lib/capybara/queries/selector_query.rb', line 72

def visible
  case (vis = options.fetch(:visible) { @selector.default_visibility(session_options.ignore_hidden_elements, options) })
  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.



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

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