When doing automatic testing of Web apps using unit testing frameworks, it can be a pain in the butt to pinpoint the proper HTML element. A lot of times tests will fail because you used a wrong locator, but since the browser will automatically close after the test, you don’t have an access to the HTML code of the page to look at what’s actually there.
Fortunately Gallio provides a class called TestContext which contains the current information about the running test and which you can use to determine if the latest test is successful or not. This can then be used to run your custom handling code during the test teardown:
[TearDown]
protected virtual void Teardown()
{
if (TestContext.CurrentContext.Outcome.Status == TestStatus.Failed)
{
using (TestLog.BeginSection("Failed web page HTML"))
TestLog.Write(WebDriver.PageSource);
}
}
In the above snippet, we record the current Web page’s HTML code into Gallio’s log (the TestLog class). To avoid spamming the log, we do this for failed tests only.
Gallio provides a powerful framework which I think is very much underused, mostly because the documentation is not very detailed (to say the least).