Skip to content

Commit

Permalink
Dont always display inline screenshots in system testing (#28133)
Browse files Browse the repository at this point in the history
3 output types are supported:
- simple: only display the screenshot path
- artifact: display the screenshot in the terminal, using the artifact protocol (supported by some CI)
- inline (default): display the screenshot in the terminal, inline (supported by some terminals)

You can force the output type by setting the `RAILS_SYSTEM_TESTING_SCREENSHOT` environment variable
  • Loading branch information
renchap committed Mar 11, 2017
1 parent c186bf6 commit 79435c0
Showing 1 changed file with 34 additions and 5 deletions.
Expand Up @@ -8,9 +8,20 @@ module ScreenshotHelper
# +take_screenshot+ can be used at any point in your system tests to take # +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 # a screenshot of the current state. This can be useful for debugging or
# automating visual testing. # automating visual testing.
#
# 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:
# * [+inline+ (default)] display the screenshot in the terminal using the
# iTerm image protocol (http://iterm2.com/documentation-images.html).
# * [+simple+] only display the screenshot path.
# This is the default value if the +CI+ environment variables
# is defined.
# * [+artifact+] display the screenshot in the terminal, using the terminal
# artifact format (http://buildkite.github.io/terminal/inline-images/).
def take_screenshot def take_screenshot
save_image save_image
puts "[Screenshot]: #{image_path}"
puts display_image puts display_image
end end


Expand Down Expand Up @@ -38,14 +49,32 @@ def save_image
page.save_screenshot(Rails.root.join(image_path)) page.save_screenshot(Rails.root.join(image_path))
end end


def output_type
# Environment variables have priority
output_type = ENV["RAILS_SYSTEM_TESTING_SCREENSHOT"] || ENV["CAPYBARA_INLINE_SCREENSHOT"]

# If running in a CI environment, default to simple
output_type ||= "simple" if ENV["CI"]

# Default
output_type ||= "inline"

output_type
end

def display_image def display_image
if ENV["CAPYBARA_INLINE_SCREENSHOT"] == "artifact" message = "[Screenshot]: #{image_path}\n"
"\e]1338;url=artifact://#{image_path}\a"
else case output_type
when "artifact"
message << "\e]1338;url=artifact://#{image_path}\a\n"
when "inline"
name = inline_base64(File.basename(image_path)) name = inline_base64(File.basename(image_path))
image = inline_base64(File.read(image_path)) image = inline_base64(File.read(image_path))
"\e]1337;File=name=#{name};height=400px;inline=1:#{image}\a" message << "\e]1337;File=name=#{name};height=400px;inline=1:#{image}\a\n"
end end

message
end end


def inline_base64(path) def inline_base64(path)
Expand Down

0 comments on commit 79435c0

Please sign in to comment.