Friday, December 21, 2007

WatiN testing ASP.NET - app startup time issues

I started a test project to test a web app. One thing I noticed was that due to application startup, occasionally the first couple of tests fail with TimeoutExceptions because the application takes too long to compile some of the pages.

So, I came up with the following to work around this:

using SHDocVw;
using WatiN.Core;
using WatiN.Core.Exceptions;

namespace Tests {
public class TestBase {
private static bool _siteSetupRun = false;

public TestBase() {
if (!_siteSetupRun) {
setupSite();
_siteSetupRun = true;
}
}

private static void setupSite() {
bool ok = false;
using (IE ie = new IE()) {
object nil = null;
((InternetExplorer)ie.InternetExplorer).Navigate("http://testsite/",
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;
}
}
}
}
}
}
}

This needs to run before any unit tests.

2 comments:

Anonymous said...

Hi,

Saw your post on technorati and thought I give you some tips.
The Navigate code line can be replaced by:
ie.GoTo("http://testsite");

The time out period is configurable through IE.Settings.WaitForCompleteTimeOut. The default is 30 seconds, but you can set this to any value you like/need.

HTH,
Jeroen van Menen
Lead developer WatiN

fallout said...

In this case I have no idea what the timeout should be and it doesn't matter; no matter what the timeout is, it is possible for the site start-up time to be longer, and setting the property is setting yourself up for failure at some point in the future.

Perhaps more detail would have been good with this post. I originally used the GoTo method, but since it calls WaitForComplete, I needed to either put a try-catch around it or muck with the setting. If there was a GoTo method overload that could be called which didn't trigger the WaitForComplete method, I would use that instead. I'll make a post about how I got to this point.