screenshot_helper.rb 3.3 KB
Newer Older
1 2
# frozen_string_literal: true

E
eileencodes 已提交
3 4 5
module ActionDispatch
  module SystemTesting
    module TestHelpers
6
      # Screenshot helper for system testing.
E
eileencodes 已提交
7 8 9 10 11 12
      module ScreenshotHelper
        # Takes a screenshot of the current page in the browser.
        #
        # +take_screenshot+ can be used at any point in your system tests to take
        # a screenshot of the current state. This can be useful for debugging or
        # automating visual testing.
13 14 15 16 17
        #
        # The screenshot will be displayed in your console, if supported.
        #
        # You can set the +RAILS_SYSTEM_TESTING_SCREENSHOT+ environment variable to
        # control the output. Possible values are:
18 19 20
        # * [+simple+ (default)]    Only displays the screenshot path.
        #                           This is the default value.
        # * [+inline+]              Display the screenshot in the terminal using the
21
        #                           iTerm image protocol (https://iterm2.com/documentation-images.html).
22
        # * [+artifact+]            Display the screenshot in the terminal, using the terminal
23
        #                           artifact format (https://buildkite.github.io/terminal/inline-images/).
E
eileencodes 已提交
24 25 26 27 28 29 30 31
        def take_screenshot
          save_image
          puts display_image
        end

        # Takes a screenshot of the current page in the browser if the test
        # failed.
        #
32 33 34
        # +take_failed_screenshot+ is included in <tt>application_system_test_case.rb</tt>
        # that is generated with the application. To take screenshots when a test
        # fails add +take_failed_screenshot+ to the teardown block before clearing
E
eileencodes 已提交
35 36
        # sessions.
        def take_failed_screenshot
37
          take_screenshot if failed? && supports_screenshot?
E
eileencodes 已提交
38 39 40
        end

        private
41
          def image_name
42
            failed? ? "failures_#{method_name}" : method_name
43 44
          end

E
eileencodes 已提交
45
          def image_path
46
            @image_path ||= absolute_image_path.to_s
47 48 49 50
          end

          def absolute_image_path
            Rails.root.join("tmp/screenshots/#{image_name}.png")
E
eileencodes 已提交
51 52 53
          end

          def save_image
54
            page.save_screenshot(absolute_image_path)
E
eileencodes 已提交
55 56
          end

57 58 59 60
          def output_type
            # Environment variables have priority
            output_type = ENV["RAILS_SYSTEM_TESTING_SCREENSHOT"] || ENV["CAPYBARA_INLINE_SCREENSHOT"]

61 62
            # Default to outputting a path to the screenshot
            output_type ||= "simple"
63 64 65 66

            output_type
          end

E
eileencodes 已提交
67
          def display_image
68
            message = "[Screenshot]: #{image_path}\n".dup
69 70 71

            case output_type
            when "artifact"
72
              message << "\e]1338;url=artifact://#{absolute_image_path}\a\n"
73
            when "inline"
74 75
              name = inline_base64(File.basename(absolute_image_path))
              image = inline_base64(File.read(absolute_image_path))
76
              message << "\e]1337;File=name=#{name};height=400px;inline=1:#{image}\a\n"
E
eileencodes 已提交
77
            end
78 79

            message
E
eileencodes 已提交
80 81 82
          end

          def inline_base64(path)
83
            Base64.strict_encode64(path)
E
eileencodes 已提交
84
          end
85 86 87 88

          def failed?
            !passed? && !skipped?
          end
89 90

          def supports_screenshot?
91
            Capybara.current_driver != :rack_test
92
          end
E
eileencodes 已提交
93 94 95 96
      end
    end
  end
end