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… ;)

Social tagging: >

14 Responses to Capturing Screenshots in Watir

  1. Sameh

    Thats great work Koops. Simple, effective, and extremley useful. Props to u.

  2. al3kc

    Have you any ideas how to attach this screenshot to RSpec report?

  3. Great work!
    Can you get it just to take a screenshot of IE, not the entire windows system?

  4. clarke

    this is fantastic – do you know if there’s any way to support dual screen screenshots? i don’t “need” it, I can certainly get away with making sure my test is running on screen 1, i was just wondering!

  5. Tim

    Maybe not dual / widescreen support… But I think the idea of just capturing foremost window, or even better, being able to specify process name of the window e.g. iexplore.exe would be useful.

    I’ve just re-installed Visual Studio C# Express on my windoze VM so might look at this after hours.

    Stay tuned on twitter/90kts for an update on this.

    Regards,
    Tim

  6. abhishek

    Hi,

    I have tried using above code.
    But i will get an error saying
    “snapIt has encountered a problem and needs to close.We are sorry for the inconvenience”

  7. finist

    Could not download the files. :( Please share them again.

  8. Prakash

    Hi,
    Great Work,I want to take snapshot in my own folder.I am running this program but the image copy only image folder in D: directory.How to take snapshot in own directory.

    Thanks in Advance

  9. vikas

    the snapit.exe file is available on the websie, can u please share that file again

    thanks

  10. vikas

    some typo in previous post

    the snapit.exe file is not available on the websie for download, can u please share that file again

    thanks

  11. thanks for the great post

  12. Hi Tim,

    Do you know if screenshots are supported in Chrome?

    Thanks

    Guido Tapia

  13. mszatanik

    need this badly, please reup