So, you can do this with both cucumbers and Capybara, but the path of the cucumber is much easier. First, create two folders in the project file: (1) failed_png and (2) success_png. Then in the env.rb file, paste the following code:
After do |scenario|
take_screenshot(@browser, scenario)
end
def take_screenshot(browser, scenario)
if scenario.failed?
scenario_name = scenario.name.gsub /[^\w\-]/, ' '
time = Time.now.strftime("%Y-%m-%d %H%M")
screenshot_path = './failed_png/' + time + ' - ' + scenario_name + '.png'
else
scenario_name = scenario.name.gsub /[^\w\-]/, ' '
time = Time.now.strftime("%Y-%m-%d %H%M")
screenshot_path = './success_png/' + time + ' - ' + scenario_name + '.png'
end
browser.save_screenshot(screenshot_path)
end
This takes a screenshot after each script and places it either in the SUCCESS folder or in the FAILED folder corresponding to the timestamp on it.
For a complete env.rb file and additional documentation, see my blog at whitneytaylorimura.wordpress.com
source
share