Posts
1150
Comments
891
Trackbacks
1
A sample NUnit SetUp class when using Watin

This post is largely for me, in case I need it again.

You know what NUnit is.  You probably know what Watin is. 

Watin is no longer under active development.  If you are in the business of writing browser-driven integration tests within the .NET space, you should probably be looking at Selenium.

However, if you are kicking it old school and using Watin, and you want to try to avoid all of those ‘false negatives’ (i.e., failing tests that fail because of quirks with COM, not because your test actually failed for ‘real’ reasons), I’ve found the following SetUp NUnit class useful:

[STAThread]
    [SetUp]
    public void Init()
    {
        bool ok = false; 
    try
        {
            ie = new IE();
        }
        catch (COMException)
        {
            ie = new IE();
        }
        object nil = null;
        ((InternetExplorer)ie.InternetExplorer).Navigate(
            Configuration.UrlRoot + Pages.Home, ref nil, ref nil, ref nil, ref nil);
        while (!ok)
        {
            try
            {
                ie.WaitForComplete();
                ok = true;
            }
            catch (TimeoutException tex)
            {
                if (!tex.Message.Contains("'Internet Explorer busy'"))
                {
                    throw;
                }
            }
        }
        ie.BringToFront();
        ie.SizeWindow(1280, 1024);
    }

All of the threading and COMException stuff is to get around the occasional COMException you get when the new IE() call bombs out (I’d say if I ran my current 100-ish integration test suite 5 times, I’d get 3 or 4 exceptions of that sort), just because it is COM, or, well, just because.  I navigate to the home page to make sure IE is up and running for each test.

And yes, I am launching a new instance of IE for each test.  These are integration tests, they are supposed to be slow.

posted on Wednesday, July 31, 2013 6:34 PM Print
Comments
No comments posted yet.

Post Comment

Title *
Name *
Email
Url
Comment *  
Please add 1 and 2 and type the answer here: