Wednesday, September 24, 2008

Watin FileUpload issues

Usually when I blog something I am writing the problem and the solution I have found. In this case, based also on the lack of pages describing the same problem, I have decided to blog about the problem only. If you guys find the solution, please let me know. I will post it here as soon as I find it myself. So here it is:

We started creating some tests for one of our web applications. My colleague created the tests, amongst them one that does a simple file upload. She used the following code:
var fu = ie.FileUpload(Find.ByName("ctl00$ContentPlaceHolder1$tcContent$tpAddItem$uplGalleryItem$fuGalleryItem"));

and it worked perfectly. She was using WatiN 1.2.4 and MBUnit 2.4.

I had Watin 2.0 installed and MBUnit 3.0. Downloaded the tests, removed the ApartmentState thing that seems not to be necessary in MBUnit 3.0, ran them.
On my computer the FileUpload Set method opens a file upload dialog and stops. I've tried a lot of code variants, to no avail; I've uninstalled both MBUnit and WatiN and installed the 1.2.4 and 2.4 versions. Tried all possible combinations actually, using .NET 1.1 and 2.0 libraries and changing the code. Nothing helped. On my computer the setting of the file name doesn't work.

I've examined the WatiN source and I've noticed that it used a FileUploadDialogHandler that determines if a window is a file upload window or not by checking a Style property. I have no idea if that is the correct solution, but just to be sure I inherited my own class from FileUploadDialogHandler and I've instructed it to throw an exception with a message containing the style of the first window it handles. The exception never fired, so I am inclined to believe that the handler mechanism somehow fails on my computer!

I have no idea what to do. I have a Windows XP SP3 with the latest updates and I am running these tests in Visual Studio 2008 Professional.

The only possible explanation left to me is that Internet Explorer 8 is the culprit, since my colleagues all have IE7. The maker of WatiN himself declared that identifying the windows by style is not the most elegant method possible, but he had no other way of doing it. My suspicion is that the window handling doesn't work at all in IE8, but I have no proof for it and so far I have found no solution for this problem.


Jeff Brown said...

IE is a moving target as far as WatiN is concerned.

The file upload dialog is particularly tricky because there isn't a nice way to automate it besides sending keypresses or other such tricks.

Please get ahold of Jeroen and let him know about this issue with as much detail as you can provide.

Vignesh Marthandan said...

var fu = ie.FileUpload(Find.ByName("ctl00$ContentPlaceHolder1$tcContent$tpAddItem$uplGalleryItem$fuGalleryItem"));
string newfilepath = Path.Combine(Environment.CurrentDirectory.ToString(), UploadImageFile);

The above code works for me (WinXP SP3 IE7.).

Anonymous said...

Wonder if this may be part of the problem?

Siderite said...

It must be! Good catch. Thank you!

Carsten Kaeding said...

What works for Vignesh works for me, but it does not work anymore with IE8. When viewing the test, one has the opportunity to manually choose a file with the UploadDialog the test pops up, but that can´t be a solution in an automated test. Even more of a problem are IE8´s MessageBoxes. I haven´t found a way to click 'OK' with WatiN neither. And code works fine for IE6 and IE7.