File: element.rb

package info (click to toggle)
ruby-capybara 3.40.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,368 kB
  • sloc: ruby: 23,988; javascript: 752; makefile: 11
file content (28 lines) | stat: -rw-r--r-- 937 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# frozen_string_literal: true

Capybara.add_selector(:element, locator_type: [String, Symbol]) do
  xpath do |locator, **|
    XPath.descendant.where(locator ? XPath.local_name == locator.to_s : nil)
  end

  expression_filter(:attributes, matcher: /.+/) do |xpath, name, val|
    builder(xpath).add_attribute_conditions(name => val)
  end

  node_filter(:attributes, matcher: /.+/) do |node, name, val|
    next true unless val.is_a?(Regexp)

    (val.match? node[name]).tap do |res|
      add_error("Expected #{name} to match #{val.inspect} but it was #{node[name]}") unless res
    end
  end

  describe_expression_filters do |**options|
    boolean_values = [true, false]
    booleans, values = options.partition { |_k, v| boolean_values.include? v }.map(&:to_h)
    desc = describe_all_expression_filters(**values)
    desc + booleans.map do |k, v|
      v ? " with #{k} attribute" : "without #{k} attribute"
    end.join
  end
end