Capturing Screenshots in Watir

Recently I noticed some discussion in the watir user group about trying to capture screenshots when running automated tests.

For such a simple requirement, it’s frustrating that solutions available either cost money (such as the purchase of SnagIt) or are a tad complicated in implementation requiring the installation of some out of date gems and dependencies. I’ve also come across this requirement in other automation tools such as QTP, so I thought it’s about time I rolled up my sleeves and hack together a simple solution with C#.NET 2.0.

First thing I did was created a small app called snapit.exe which you can download here. If anyone is interested in improving the code, you’re welcome to get a copy of my source code here. In short, all this app does is take a screenshot of the entire screen, and save it to a default location (currently C:\snapit.png). Repeated use of the app will increment the filename by 1 i.e. snapit1.png, snapit2.png …

You can change the default file path by passing it a parameter at runtime
e.g. D:\\snapit.exe D:\\images\\testcase.png

Next thing to do is call it from your watir code. In Ruby, this is as simple as:

1
2
cmd = 'D:\\snapit.exe'
puts %x{#{cmd}}

Putting it all together with some watir as an example:

1
2
3
4
5
6
7
require "watir"
test_site = "http://www.90kts.com"
ie = Watir::IE.new
ie.goto test_site
ie.link(:text, "Contact").click
cmd = 'D:\\snapit.exe D:\\images\\testsuite.png'
puts %x{#{cmd}}

Hopefully someone finds that useful. You can use this in any automation suite really, up to you. It’s free of course, unless you want to donate some money towards my appreciation for beer… ;)

14 comments to Capturing Screenshots in Watir

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">