Capturing Screenshots in Watir
2 June, 2008 – 5:27 pmRecently 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…









3 Responses to “Capturing Screenshots in Watir”
Thats great work Koops. Simple, effective, and extremley useful. Props to u.
By Sameh on Jun 3, 2008
way cool!
By George on Jun 3, 2008
Have you any ideas how to attach this screenshot to RSpec report?
By al3kc on Sep 1, 2008