In my automated tests using Ruby / Capybara / Cucumber / Selenium, it is necessary that when some step of my test fails, the following steps will be performed:
Know how to help me with this question?
Today the system takes a screenshot as evidence of the success of the scenario, but now I would like to take a failure screenshot without disturbing the execution of all my tests, following the execution of the next step or scenario.
Example code currently used as a hook for take screenshot:
After do | scenario |
screenshot = "log / screenshots / # {scenario.name} .png"
page.save_screenshot (screenshot)
embed (screenshot, 'image / png', 'Screen_Evidence')
end
This should create a screenshot at the point of failure for you.
config.after(:each) do |example|
if example.exception
screenshot = "log / screenshots / # {scenario.name} .png"
page.save_screenshot (screenshot) embed (screenshot, 'image / png', 'Screen_Evidence') end end
You will probably want to limit this just to feature tests and optionally you could try refactoring your existing screenshot code into this same function.
Try this way.
After do |scenario|
if scenario.failed?
timestamp = "#{Time.zone.now.strftime('%Y-%m-%d-%H:%M:%S')}"
screenshot_name = "screenshot-#{scenario.name}-#{timestamp}.png"
screenshot_path = "#{Rails.root.join(provide path to save screenshots)}/#{screenshot_name}"
Capybara.page.save_screenshot(screenshot_path, full: true)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.