Browser based web testing with WatiN, Gallio, and MbUnit (Part 2)
In my previous post, I discussed the basics of using Gallio, MbUnit, and WatiN to test web applications. In the previous example you could continue to add tests to the class by creating additional test methods and marking them with the test attribute. However, you could not guarantee what kind of order those tests would run in. Fortunately, MbUnit/Gallio is a flexible enough framework to allow you to create orderd test suites which is a common need for functional testing.
In order to create a test suite that will run tests in a specific order, you can use the SaticTestFactory attribute. StaticTestFactory allows you to create setup and tear down functions in addition to defining as many tests as you need. The syntax is a little clunky, but that can be managed by calling out to functions instead of writing the tests right in the StaticTestFactory like I have in this example.
A few more notes about the example below.
- GallioLogWatinWriter is defined in my previous blog post.
- Gallio does not show the tests in the order they would be run, thus the 001, 002, etc... prefixes to each test name.
- DynamicTestFactory is available that can be used to specify the tests in the test case at runtime, potentially allowing you to organize your tests in a much cleaner way than used here.
And here is the code
using System;
using System.Collections.Generic;
using System.Text;
using WatiN.Core;
using MbUnit.Framework;
using Gallio.Framework;
using System.IO;
using System.Drawing;
using System.Text.RegularExpressions;
namespace WatiN_Demo_Project
{
class TheRoamingCoder2
{
public static IE window;
[StaticTestFactory]
public static IEnumerable<Test> CreateTests()
{
yield return new TestSuite("Test Group Type Members")
{
SetUp = () => { },
TearDown = () => { },
SuiteSetUp = () =>
{
window = new IE("about:blank");
WatiN.Core.Logging.Logger.LogWriter = new GallioLogWatinWriter();
},
SuiteTearDown = () =>
{
window.Close();
window.Dispose();
},
Children = {
new TestCase("001 Test Blog Link", () => {
window.GoTo("http://www.theroamingcoder.com");
//Click the blog link
Link lnk_blog = window.Link(Find.ByText("Blog"));
lnk_blog.Click();
//Ensure that at least one post is present by looking for the submitted element
Span span_created = window.Span(Find.ByText(new Regex("Submitted by .* on .*")));
Assert.IsTrue(span_created.Exists);
//Capture and log a screenshot on failure
//NOTE this feature is broken in WatiN 2.0 beta1 (screenshots are all black),
// but is supposed to be fixed in WatiN 2.0 beta2
if (TestContext.CurrentContext.Outcome == Gallio.Model.TestOutcome.Failed)
{
string filename = Path.GetTempFileName();
filename = Path.GetTempPath() + Path.GetFileNameWithoutExtension(filename) + ".bmp";
window.CaptureWebPageToFile(filename);
TestLog.EmbedImage("Test Screenshot", System.Drawing.Image.FromFile(filename));
}
}){
Description = "Makes sure the blog link on my home page works."
},
new TestCase("002 Test Resume Link", () => {
window.GoTo("http://www.theroamingcoder.com");
//Click the resume link
Link lnk_resume = window.Link(Find.ByText("Resume"));
lnk_resume.Click();
//Make sure the resume is showing
Assert.Contains(window.Text, "University of Colorado 2002 Boulder, CO");
//Capture and log a screenshot on failure
//NOTE this feature is broken in WatiN 2.0 beta1 (screenshots are all black),
// but is supposed to be fixed in WatiN 2.0 beta2
if (TestContext.CurrentContext.Outcome == Gallio.Model.TestOutcome.Failed)
{
string filename = Path.GetTempFileName();
filename = Path.GetTempPath() + Path.GetFileNameWithoutExtension(filename) + ".bmp";
window.CaptureWebPageToFile(filename);
TestLog.EmbedImage("Test Screenshot", System.Drawing.Image.FromFile(filename));
}
}){
Description = "Makes sure the resume link on my home page works."
},
}
};
}
}
}
| Attachment | Size | Hits | Last download |
|---|---|---|---|
| TheRoamingCoder2.cs | 3.82 KB | 235 | 14 hours 13 min ago |


