O'Reilly logo

The Cucumber Book, 2nd Edition by Steve Tooke, Aslak Hellesoy, Matt Wynne

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required