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.
I think this is a great feature, well done to Jeff and the team for implementing this.
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.
Where is the SNAPSHOT function located?
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
Excellent suggestion. Appreciate the posting of the code snippet!
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