Make cucumber open the browser with the current page

Woodcut from Doré. Purely illustrative
Doré Woodcut. Its only function is to make the layout look better. And these images are really nice themselves

The Cucumber Book describes a really nifty trick when testing web-pages: open the browser when a step fails. This is a feature provided by cucumber itself.

Add a support file features/support/debugging.rb:

After do |scenario|
  save_and_open_page if scenario.failed?
end

And add launchy to your gemfile, and bundle install. (or install it with whatever else you use).

group :test do
   #...
   gem "launchy", "~> 2.1.2"
end

This will save the page that cucumber is looking at, then open it in your browser. Works fine, untill you have a large suite of features and some refactoring breaks many features. Having to close twenty tabs in your browser after each run is counterproductive and often really frustrating.

I solved this with a flag that allows me to fire this debugging-trick only when I need it. When I have a failing scenario, and I want to investigate it by inspecting the page, I run my cucumber with an additional environment-variable:

$ cucumber debug=open
After do |scenario|
  save_and_open_page if scenario.failed? and (ENV["debug"] == "open")
end

The debug= syntax allows for more simple tricks too. Like debug=pp:

require "pp"
After do |scenario|
  save_and_open_page if scenario.failed? and (ENV["debug"] == "open")
  pp(page) if ENV["debug"] == "pp"
end

Simple trick, works like a charm.

About the author: Bèr Kessels is an experienced webdeveloper with a great passion for technology and Open Source. A golden combination to implement that technology in a good and efficient way. Follow @berkes on Twitter. Or read more about Bèr.

blog comments powered by Disqus