Skip to content

Instantly share code, notes, and snippets.

@henrik
Last active August 29, 2015 14:06
Show Gist options
  • Save henrik/c72e55d6bb808b4ec50d to your computer and use it in GitHub Desktop.
Save henrik/c72e55d6bb808b4ec50d to your computer and use it in GitHub Desktop.
show_screenshot helper for PhantomJS tests.
public/screenshot-*.png
describe "Things" do
it "does stuff", :js do
visit my_path
show_screenshot # Writes screenshot-4.png, for line 4.
show_screenshot :my_name # Writes screenshot-my_name.png
end
end
module FeatureHelpers
def show_screenshot(given_suffix = :default_suffix)
if given_suffix == :default_suffix
# The line may be e.g. "/dir/my_spec.rb:123:in …"
called_from_line = caller.first[/.*?:(\d+):/, 1]
suffix = "line-#{called_from_line}"
else
suffix = given_suffix
end
file_name = "screenshot-#{suffix}.png"
url = "http://our-app.dev/#{file_name}"
# https://github.com/teampoltergeist/poltergeist#taking-screenshots-with-some-extensions
save_screenshot(Rails.root.join("public/#{file_name}"), full: true)
# https://github.com/busyloop/lolcat - Colorizes the output, in our case like a rainbow.
Lol.println("Screenshot can be found at: #{url}", os: 10, freq: 0.1, spread: 1)
end
end
RSpec.configure do |config|
config.include FeatureHelpers, type: :feature
end
@henrik
Copy link
Author

henrik commented Sep 16, 2014

We write to public and output a URL since we develop inside a VM. If you develop on OS X straight on the host machine, you could e.g. write to /tmp and even open /tmp/file.png to auto-open it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment