Welcome to mirror list, hosted at ThFree Co, Russian Federation.

element.rb « page « qa « qa - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ec8aca76250d852e526f47f1294c051c5111b929 (plain)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# frozen_string_literal: true

require 'active_support/core_ext/array/extract_options'

module QA
  module Page
    # Gitlab element css selector builder using data-testid attribute
    #
    class Element
      attr_reader :name, :attributes

      def initialize(name, *options)
        @name = name
        @attributes = options.extract_options!

        options.each do |option|
          @attributes[:pattern] = option if option.is_a?(String) || option.is_a?(Regexp)
        end
      end

      def required?
        !!@attributes[:required]
      end

      def selector_css
        [
          %([data-testid="#{name}"]#{additional_selectors}),
          %([data-qa-selector="#{name}"]#{additional_selectors})
        ].join(',')
      end

      def matches?(line)
        if expression
          !!(line =~ /["']#{name}['"]|#{expression}/)
        else
          !!(line =~ /["']#{name}['"]/)
        end
      end

      private

      def expression
        if @attributes[:pattern].is_a?(String)
          @_regexp ||= Regexp.new(Regexp.escape(@attributes[:pattern]))
        else
          @attributes[:pattern]
        end
      end

      def additional_selectors
        @attributes.dup.delete_if { |attr| attr == :pattern || attr == :required }.map do |key, value|
          %([data-qa-#{key.to_s.tr('_', '-')}="#{value}"])
        end.join
      end
    end
  end
end