Screenshots and Selenium

At some point when I wasn’t paying attention Selenium grew the ability to take screenshots. The two commands of interest are captureScreenshot which takes an image of what is viewable in the the browser and captureEntirePageScreenshot which captures the entire content of the browser; visible or not.

Screenshots and automation have a dubious history. Too often they get used to determine error conditions which leads to ‘the test failed because the pixel was off by one even through it actually passed functionality wise’. That is exactly opposite to how I would recommend you use this capability.

Instead I think you should incorporate it two ways.

  1. Ack! Something went wrong! – Any time an assertion fails, take a capture of what is being displayed on the screen. This could save you the time of running back through scripts to get to the error point and if it is an intermittent fail you can capture those states.
  2. As an aid for humans – The ability to drive different browsers is one of the great selling points of Selenium. But any tool like this is great for functionality, but really bad for layout and presentation problems. By wonderful coincidence though, humans are great at it. So why not make a script to capture each page in your application and stash it somewhere for a human to look at later?

This doesn’t mean that people have not been successful doing screenshot comparisons before. At CAST 2007 in Seattle, Harry Robinson demonstrated how Google Maps used image comparison when figuring out the driving directions algorithm. And at GTAC 2009 there was a presentation on Fighting Layout Bugs – Techniques to automatically verify the work of HTML and CSS programmers which Jeff Fry summarizes quite nicely. Here is the recording of that session.

So yes, it is possible to do automation using screenshots to determine pass/fail conditions. But for most projects they infrastructure and time to set it up is not worth the return. Instead, use them as tools to assist in figuring out what went wrong in a script or to capture information for later human analysis.

Post a Comment

Your email is never published nor shared. Required fields are marked *