Created
October 12, 2018 09:29
-
-
Save knu/6b50b3372954b688a6505d17e4f3368e to your computer and use it in GitHub Desktop.
Enable HTML Source snapshot in RSpec's system specs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
return unless Rails.env.test? | |
require 'highline' | |
require "action_dispatch/system_testing/test_helpers/screenshot_helper" | |
module ActionDispatch | |
module SystemTesting | |
module TestHelpers | |
module ScreenshotHelper | |
module HtmlSaver | |
private | |
def rack_test? | |
Capybara.current_driver == :rack_test | |
end | |
def image_path | |
# Rails 6+ | |
@image_path ||= absolute_image_path.to_s | |
end | |
def html_path | |
@html_path ||= absolute_html_path.to_s | |
end | |
def absolute_html_path | |
Rails.root.join("tmp/screenshots/#{image_name}.html") | |
end | |
def save_image | |
super unless rack_test? | |
page.save_page(absolute_html_path) | |
end | |
def cli | |
@cli ||= HighLine.new | |
end | |
def display_image | |
message = | |
if rack_test? | |
<<~EOS | |
[HTML Source]: file://#{html_path} | |
EOS | |
else | |
super.sub(/^\[Screenshot\].*\n/, "[Screenshot]: file://#{image_path}\n[HTML Source]: file://#{html_path}\n") | |
end | |
cli.color(message, :yellow) | |
end | |
def supports_screenshot? | |
true | |
end | |
end | |
prepend HtmlSaver | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment