Taking Screenshots

Taking a screenshot of the browser when a step fails can help you diagnose failures quicker. These screenshots can be embedded directly into an HTML report. The trick is to set up an After hook, as described in Using Hooks. Try adding the following code to a new file in features/support/screenshots.rb:

 After(​'@javascript'​) ​do​ |scenario|
 if​(scenario.failed?)
  page.driver.browser.save_screenshot(​"html-report/​​#{​scenario.__id__​}​​.png"​)
  embed(​"html-report/​​#{​scenario.__id__​}​​.png"​, ​"image/png"​, ​"SCREENSHOT"​)
 end
 end

We can take screenshots only when there is a browser, which is why we tagged the hook with @javascript. A hook can receive an optional ...

Get The Cucumber Book, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.