summaryrefslogtreecommitdiff
path: root/qa/qa/page/component/blob_content.rb
blob: 5ee5d4978e53cc735acecdbcf5e83d3ccd993ae6 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# frozen_string_literal: true

module QA
  module Page
    module Component
      module BlobContent
        extend QA::Page::PageConcern

        def self.included(base)
          super

          base.view 'app/assets/javascripts/blob/components/blob_header_filepath.vue' do
            element :file_title_content
          end

          base.view 'app/assets/javascripts/blob/components/blob_content.vue' do
            element :blob_viewer_file_content
          end

          base.view 'app/assets/javascripts/blob/components/blob_header_default_actions.vue' do
            element :default_actions_container
            element :copy_contents_button
          end

          base.view 'app/assets/javascripts/vue_shared/components/source_viewer/source_viewer_deprecated.vue' do
            element :blob_viewer_file_content
          end
        end

        def has_file?(name)
          has_file_name?(name)
        end

        def has_no_file?(name)
          has_no_file_name?(name)
        end

        def has_file_name?(file_name, file_number = nil)
          within_file_by_number(:file_title_content, file_number) { has_text?(file_name) }
        end

        def has_no_file_name?(file_name)
          within_element(:file_title_content) do
            has_no_text?(file_name)
          end
        end

        def has_file_content?(file_content, file_number = nil)
          within_file_by_number(:blob_viewer_file_content, file_number) { has_text?(file_content) }
        end

        def has_no_file_content?(file_content)
          within_element(:blob_viewer_file_content) do
            has_no_text?(file_content)
          end
        end

        def has_normalized_ws_text?(text, wait: Capybara.default_max_wait_time)
          if has_element?(:blob_viewer_file_content, wait: 1)
            # The blob viewer renders line numbers and whitespace in a way that doesn't match the source file
            # This isn't a visual validation test, so we ignore line numbers and whitespace
            find_element(:blob_viewer_file_content, wait: 0).text.gsub(/^\d+\s|\s*/, '')
              .start_with?(text.gsub(/\s*/, ''))
          else
            has_text?(text.gsub(/\s+/, " "), wait: wait)
          end
        end

        def click_copy_file_contents(file_number = nil)
          within_file_by_number(:default_actions_container, file_number) { click_element(:copy_contents_button) }
        end

        private

        def within_file_by_number(element, file_number, &block)
          if file_number
            within_element_by_index(element, file_number - 1, &block)
          else
            within_element(element, &block)
          end
        end
      end
    end
  end
end