File Upload Testing in IE8 with WatiN

A .NET MVC application we’re currently working on contains a feature involving image uploads. We’ve had an excellent experience so far using WatiN and SpecFlow together for system testing, but testing file uploads always adds an extra degree of complication. This was no exception, but I’m quite pleased with the outcome.

The first hurdle I had to cross was an incompatibility between WatiN 2.0.20 and Internet Explorer 8 dialogs. It fails to find the dialog and hangs indefinitely (see this question on stackoverflow.com for more information). Fortunately, this was fixed some time ago in the source and compiling WatiN from trunk proved more or less trivial. I swapped the newly compiled DLL into our project and reran the system test suite — FAIL.

We are also using the WatiN CSS Selector Extensions posted by David Kirk which was looking for the old version of WatiN, which I had to deduce from a series of cryptic runtime exceptions. The source is available, which I downloaded, rebuilt, and copied the DLL into our project. Existing system tests returned to their joyful, verdant state.

The next hurdle was setting the test up with a file guaranteed to be available on the system where the tests were running. I didn’t want to rely on everyone having an image file at a specific location (C:\toomuchwant.jpg, for example) so I decided that an image embedded in the assembly would be best (as I’d done in our random test data generator — a different story). I created a directory TestData inside our system tests project and added an image within, then set its Build Action to Embedded Resource (under the image file’s properties). But, I needed the file to exist on the filesystem where the browser could get to it. Writing it out to a temp file worked well; see the code below which is an example for a SpecFlow step to upload a specific image.

All that’s left is to verify that the image upload made it all the way through to the database. The test will verify that our upload works and will run on any developer’s system without additional configuration. Hurdles aside, this has worked out really well for a testing problem that has historically been hard-won, if won at all.