Sunday, September 26, 2010

Recently, I stumble upon a task on how to automatically capture a screenshot when one of my cucumber scenario fails. I use capybara and webdriver for my automation test.

I found several solutions which include to use a program called scrot in ubuntu and to use another gem http://github.com/mocoso/cucumber-screenshot. However, I run my automation test in both Windows and Ubuntu and therefore I need a way to be able to capture screenshot in both OS.

Webdriver fortunately already has this module implemented in the driver class. Although this only supports firefox browser, it is enough for me as I run the automation in Hudson with firefox and I would want to embed the screenshot in the cucumber html report.

After do |scenario| if scenario.failed? File.open("./screenshots/#{scenario.name}.jpeg",'wb') do |f| f.write(Base64.decode64(page.driver.browser.screenshot_as(:base64))) end endend

The webdriver class already has the save_screenshot methods, but it does not work in Windows and therefore you need to write the file as byte.

The above code will automatically capture a screenshot whenever the scenario.failed? is true. This is very useful for error reporting because if you run your test in Hudson, you would not be able to see your test running.

I am not sure about that error. But it seems that your scenario filename has a space "Login to Ma.jpeg"'support/patches/screenshots/Login to Ma.jpeg' and that could be the reason for 'no such file or directory' errors.

You can try changing the code to use the same name first instead of using 'scenario.name' to see if it works.

'After do |scenario| if scenario.failed? File.open("./screenshots/test.jpeg",'wb') do |f| f.write(Base64.decode64(page.driver.browser.screenshot_as(:base64))) end endend'

Let me know if it works or not. Could you also provide me with more information on how you run your script or any other information about the errors?