xUnit Browser Attribute – Run Cross-Browser WatiN tests effortlessly

Recently I wanted to run a web UI test across multiple different browsers using WatiN. WatiN supports FireFox, IE and Chrome so that wasn’t a problem, however in order to test the application on difference browsers I would have to duplicate the logic multiple times. This harms readability and maintainability of your tests and is just pain wrong. As such, I created a very similar xUnit Browser Attribute, code can be found on my GitHub repository along with the binaries.

The concept is simple, have the instance of the browser created for you outside of your test and provided the instance as a argument during execution. By using attributes, you can define multiple different browsers, each one will generate a separate test. The advantage of having the tests in this fashion means that if a test one particular browser fails then you can quickly see the problem and the results form the other browsers. If they where just a single test, if one part of  test failed, you wouldn’t be able to see the results for the other part. This can make identify where the problems lies much more difficult. An example of the attribute and a test is below.

public class Example
{
    [Theory]
    [Browser("IE")]
    [Browser("FireFox")]
    [Browser("Chrome")]
    public void Should_Click_Search_On_Google_And_Return_Results_For_AspNet(Browser browser)
    {
        browser.GoTo("http://www.google.co.uk");
        browser.TextField(Find.ByName("q")).Value = "asp.net";
        browser.Button(Find.ByName("btnG")).Click();
        Assert.True(browser.ContainsText("The Official Microsoft ASP.NET Site"));
    }
}

This example is made possible by taking advantage of the xUnit Extensibility model and being able to create objects dynamically in C#. The attribute code is below, as WatiN has a clean namespace structure creating an object based off the string of the name is easy.

public class BrowserAttribute : DataAttribute
{
    public string Browser { get; set; }
    public BrowserAttribute(string browser)
    {
        Browser = browser;
    }
 
    public override IEnumerable GetData(MethodInfo methodUnderTest, Type[] parameterTypes)
    {
        Type type = Assembly.GetAssembly(typeof(Browser)).GetType("WatiN.Core." + Browser);
        return new[] { new[] {Activator.CreateInstance(type) }};
    }
}

With this attribute, you can now quickly and easily define the browsers you want the test to run against, without having to duplicate the test itself.

2 thoughts on “xUnit Browser Attribute – Run Cross-Browser WatiN tests effortlessly”

  1. I think you could also do that with a PropertyData attribute, like:

    public static IEnumerableBrowsers {
    get {
    yield return new object[] { new IE(); };
    yield return new object[] { new FireFox(); };
    yield return new object[] { new Chrome(); };
    }
    }

    [Theory]
    [PropertyData(“Browsers”)]
    public void GoSomewhere(IBrowser browser) {
    using (browser) {
    // …
    }
    }

    I haven’t tried it, but should work…? One can also then trivially supply other test data too, which otherwise you’d need to extend the xBrowserAttribute to do.

Leave a Reply

Your email address will not be published. Required fields are marked *