Homepage | About Me | Testing ASP.net Book | Best Blog Posts | Personal Projects | Follow me on Twitter | GitHub | SlideShare | RSS
Blog.BenHall.me.uk

WatiN Integration into MbUnit\Gallio Reports

Thursday, December 06, 2007

One of the cool items I didn't mentioned in my announcement post is that we now have the ability to integrate screenshots from a WatiN test into the reports.

Here we have a simple test which searches for MbUnit on Google and then asserts to check that NUnit is mentioned on the page (this fails).

[Test]
public void DemoCaptureOnFailure()
{
    using (Log.BeginSection("Go to Google, enter MbUnit as a search term and click I'm Feeling Lucky"))
    {
        ie.GoTo("
http://www.google.com");

        ie.TextField(Find.ByName("q")).TypeText("MbUnit");
        ie.Button(Find.ByName("btnI")).Click();
    }

    // Of course this is ridiculous, we'll be on the MbUnit homepage...
    Assert.IsTrue(ie.ContainsText("NUnit"), "Expected to find NUnit on the page.");
}

Normally, it would be hard to find out what was actually on the webpage at that point to see why the test decided to fail. In the tear down method, there is a check if the test passed or failed, if it failed then it takes a snapshot and includes it in the report.

[TearDown]
public void DisposeBrowser()
{
    if (Context.CurrentContext.Outcome == TestOutcome.Failed)
        Snapshot("Final screen when failure occurred.", LogStreamNames.Failures);

    if (ie != null)
        ie.Dispose();
}

When you view the report, on the test that failed you can see that the screenshot of the webpage has been included.  This will make debugging the error a lot easier, making the tests more useful.

image 

I think this is a great feature, well done to Jeff and the team for implementing this.

Technorati Tags: ,

Labels: ,

Blogger comments

At 10:33 PM, Anonymous Anonymous said...
I've installed both MbUnit and Gallio: can you tell me what dll's to reference and what namespaces I need to include so I can gain access to these object?

Thank you very much.    
At 5:18 PM, Anonymous Anonymous said...
Where is the SNAPSHOT function located?    
At 8:13 PM, Blogger Ben Hall said...
The sample code can be accessed from the MbUnit source tree.

http://www.google.com/codesearch?hl=en&q=watin+package:http://mb-unit%5C.googlecode%5C.com+show:HVFcEU151Ro:y8Mlc6HebE4:HVFcEU151Ro&sa=N&cd=1&ct=rc&cs_p=http://mb-unit.googlecode.com/svn&cs_f=trunk/v3/src/MbUnit/Samples/MbUnit.Samples/WatiNDemo.cs

Code for the snapshot is this:

private void Snapshot(string caption)
{
Snapshot(caption, Log.Default);
}

private void Snapshot(string caption, LogStreamWriter logStreamWriter)
{
using (logStreamWriter.BeginSection(caption))
{
logStreamWriter.WriteLine("Url: {0}", ie.Url);
logStreamWriter.EmbedImage(null, new CaptureWebPage(ie).CaptureWebPageImage(false, false, 100));
}
}

private class WatiNStreamLogger : ILogWriter
{
public void LogAction(string message)
{
Log.WriteLine(message);
}
}

Hope this helps.

Ben    
At 10:17 PM, Anonymous Anonymous said...
Excellent suggestion. Appreciate the posting of the code snippet!    
At 2:20 AM, Blogger tk said...
hi i seem to be getting a blank image when i capture the screen shot it is all black any idea why it is happening