AOP, NUnit and Selenium: clever screenshots of failures

Last week I did struggle finding out why a test was failing only on our Cruise box, only in the Cruise build.

I wanted to get a screenshot of each failing tests, and I think I found a clever solution: aop.

We already use PostSharp on our code base for logging, transaction demarcation and hibernate session support, so I wrote a simple annotation for our acceptance tests:

  1. using System;
  2. using PostSharp.Laos;
  3.  
  4. namespace Web.UI.AcceptanceTests
  5. {
  6.  [Serializable]
  7.  public class ScreenCaptureAttribute : OnExceptionAspect
  8.  {
  9.   public override void OnException(MethodExecutionEventArgs eventArgs)
  10.   {
  11.    SeleniumManager.Selenium.CaptureScreenshot(string.Format("C:\\{0}.jpg", eventArgs.Method.Name));
  12.   }
  13.  }
  14. }

Every time a test will throw an Exception Selenium will take a screenshot of the screen: pretty simple.

The only caveat is that it won’t work if Cruise is running as a service, and to get a decent result you’ll probably need to maximize the web browser windows.

This entry was posted in work and tagged , , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

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

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>