Skip to content

Instantly share code, notes, and snippets.

@mcls
Last active November 22, 2016 19:10
Show Gist options
  • Save mcls/3d0887df48d8cf2620a1e8e0fa998841 to your computer and use it in GitHub Desktop.
Save mcls/3d0887df48d8cf2620a1e8e0fa998841 to your computer and use it in GitHub Desktop.
# Allows you to run rspec with SCREENSHOT=1 to take screenshots.
# The images are stored under tmp/screenshots/
#
module ScreenshotHelpers
SCREENSHOT_KEY = 'SCREENSHOT'
DIR_PATH = Rails.root.join('tmp', 'screenshots')
# @example
# screenshot("dashboard_clean_slate")
# screenshot("shopping_cart_multiple_items")
#
def screenshot(name)
return unless ScreenshotHelpers.screenshots_enabled?
filename = [name, 'png'].join('.')
path = DIR_PATH.join(filename)
page.save_screenshot(path)
end
class << self
def screenshots_enabled?
!['', '0', 'false'].include?(ENV[SCREENSHOT_KEY].to_s)
end
def screenshots_enabled_message
"\u{1F4F8} Screenshots are enabled! Unset #{SCREENSHOT_KEY} to disable."
end
end
end
RSpec.configure do |config|
config.include ScreenshotHelpers, type: :feature
config.before :suite do
if ScreenshotHelpers.screenshots_enabled?
Capybara.default_driver = Capybara.javascript_driver
puts ScreenshotHelpers.screenshots_enabled_message
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment