Last active
February 26, 2016 12:21
-
-
Save kennethkalmer/3278204 to your computer and use it in GitHub Desktop.
Save a PNG/HTML copy of the current page in Poltergeist when a scenario failed
This file contains 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
# Save some data when a feature failed | |
After('@javascript') do |scenario| | |
# Only failed, and only if we can render | |
if scenario.failed? && page.driver.respond_to?(:render) | |
# Get a name | |
name = case scenario | |
when Cucumber::Ast::OutlineTable::ExampleRow | |
[ scenario.scenario_outline.name, scenario.name ].join(' ') | |
when Cucumber::Ast::Scenario | |
scenario.name | |
else | |
raise "Don't know how to name #{scenario.class}" | |
end | |
# Prevent paths from being created | |
name.gsub!(/\//, "--") | |
# base path | |
output = Rails.root.join("tmp/phantom/#{name}") | |
# Full PNG | |
page.driver.render( "#{output}.png", full: true ) | |
# Full HTML | |
File.open( "#{output}.html", "w+" ) { |f| f.write page.html } | |
end | |
end | |
# Example for resizing the screen | |
Before('@widescreen') do | |
case Capybara.current_driver | |
when :selenium | |
window = Capybara.current_session.driver.browser.manage.window | |
window.resize_to( 1440, 900 ) | |
when :poltergeist | |
page.driver.resize( 1440, 900 ) | |
else | |
raise "Don't know how to resize #{Capybara.current_driver} driver windows" | |
end | |
end | |
# Save screenshots/html after each step for inspection | |
AfterStep('@snap') do |scenario| | |
@_snap_number ||= 0 | |
@_snap_number += 1 | |
# base path | |
output = Rails.root.join("tmp/phantom/#{scenario.name} #{@_snap_number}") | |
if page.driver.respond_to?(:render) | |
page.driver.render( "#{output}.png", full: true ) | |
end | |
File.open( "#{output}.html", "w+" ) { |f| f.write page.html } | |
end | |
After('@inspect') do |scenario| | |
if scenario.failed? && page.driver.respond_to?(:debug) | |
page.driver.debug | |
end | |
end | |
Around('@patience') do |scenario, block| | |
previous_wait_time = Capybara.default_wait_time | |
Capybara.default_wait_time = previous_wait_time * 2 | |
begin | |
block.call | |
ensure | |
Capybara.default_wait_time = previous_wait_time | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment