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.
- 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.
- 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
- 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
DefaultSelenium selenium = new DefaultSelenium("localhost",4444,"*iexplore","http://www.example.com"); selenium.start();
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.