Selenium is the most popular framework for web browser automation. We use selenium for automating our UI test cases. Selenium works perfectly fine in most web browsers with the exception of the obviously most problematic “Internet Explorer”. Although most of the features of selenium work fine with internet explorer, there are a few hiccups particularly associated with Internet Explorer e.g. capturing Screenshot, file upload, etc. Here I’ll be talking about automating the file upload feature in Internet Explorer using Selenium.

In Firefox, you can automate file upload by simple using the “selenium.type” method over the file selector.

Selenium.type("/input[@name=image_file]");

However, the type method doesn’t work in Internet explorer because of security restrictions. Internet Explorer doesn’t allow you to change the value of file inputs through JavaScript. So, I did a little research on how I can automate the process in Internet Explorer. There seems to be a few solutions to this problem but none of them really solved mine because all of the solutions are for those who are not using selenium RC or are not using RC server in a remote machine. In my case, selenium RC is located in a remote machine and all my java classes in a separate controller machine. This makes it difficult to invoke autoit scripts as it is mentioned in most of the available solutions. Some of the methods I tried are:

  1. Using selenium.attachFIle: Selenium has an api for automating file uploads and is provided as the attachFile method. However, this method is supported in Firefox only and doesn’t work with Internet Explorer.
  2. Using native keys to type in the characters: Selenium RC has apis for generating native keyboard events. This method can be used to automate keyboard events in the OS level and outside the web browser. I also tried this in Internet Explorer, but once I click the browse button and the file selector popups, selenium gets blocked and the keys could not be typed without manual intervention
  3. Using AutoIT with Selenium: One most popular method of automating the file upload is to use an autoit script in conjuction with selenium. However this also had it’s own problem. I couldn’t invoke the autoit script from my test script as I was running selenium RC in a remote machine. I found another interesting post about this method here – http://automationtricks.blogspot.com/2010/09/how-to-upload-file-in-selenium.html

THE FIX

After a series of trials, I could finally automate the file upload scenario in Internet explorer. The fix was to upgrade my selenium from 1.x to 2.x (or webdriver). Unlike selenium 1.x, webdriver is not javascript driven and is not as restricted as javascript driven selenium 1.x. However, upgrading from 1.x to webdriver was a mess because all my test cases were then not working as expected in firefox. So, i did a little hack here to make sure web driver is used only in case of Internet explorer. Given below are the changes I made.

Before:

DefaultSelenium selenium = new DefaultSelenium("localhost",4444,"*iexplore","http://www.example.com");
selenium.start();

After:

WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),DesiredCapabilities.internetExplorer());
DefaultSelenium selenium = new WebDriverBackedSelenium(driver,"http://www.example.com"));

With this code change, we are now using web driver backed selenium, and because of this, the selenium.type method can be used without any security restrictions. As a result, my file upload automation was run successfully. Well, thanks to those guys working on web driver.

In fact, many of the limitations of selenium 1.x have been addressed by web driver and there’s a properly documented section on migrating selenium 1.x to webdriver. If you are facing any issues with selenium 1.x, you might try upgrading to web driver. More details on the upgrade process are available here – migrating from selenium 1.x to web driver. Well, if you find anything interesting, feel free to share it here.