File Uploads with Selenium
A colleague pinged my on how to do a file upload from a Selenium script. My smart ass reply was “Did you look at the file_upload tag?”. And then I realized that File Upload is one of those things that the JavaScript security model in browsers makes very difficult. You have to explicitly select a file, which makes sense since you wouldn’t want to visit a site that had some evil JavaScript on it that magically uploaded your Quicken data file or some other file that is in a well known location.
There are some workarounds, including mucking around with your security model:
[http://cakebaker.wordpress.com/2006/03/29/file-upload-with-selenium/](http://cakebaker.wordpress.com/2006/03/29/file-upload-with-selenium/)
The [Selenium FAQ](http://wiki.openqa.org/display/SEL/Selenium+Core+FAQ) also talks about using SeleniumIDE to work around the JavaScript security model. This is also a good solution when testing across multiple servers as well!
However, the solution I find preferable is to take advantage of the 80% solution/20% effort that Selenium is out of the box, and not test file uploads directly. Instead, add a helper action like `/fileupload?file=path/to/my/file.txt` that you can invoke from Selenium. Have the helper action do the basic file upload, just like the webpage does, and then return to testing your site post file upload! This is the simplest, most cross platform approach. It’s also a very good pattern for working around other gotchas in Selenium.
June 15th, 2007 at 9:34 am
[…] File Uploads with Selenium- Eric’s got some copasetic advice here! […]
August 15th, 2007 at 7:44 am
This only works if you have the server running on the same machine where the file lies, though, doesn’t it? If I want to upload a local file to a remote server under test, such a helper action doesn’t work because it can’t access the file.
August 16th, 2007 at 3:34 pm
Well, can you hack around it? In other words, can your remote server do something like \\someip\mydesktop\testfile.txt? You are right that it’s not a perfect solution, and adds a lot of brittleness.
I would argue however that your Selenium tests if they are being run in any sort of regular way should be using the same file over and over, and not relying on your local desktop anyway! If you are running Selenium under any kind of Continous Integration environment, having a dependency on a local file will cause all sorts of nightmares!
July 23rd, 2008 at 11:27 am
I’m still stuck with a model form that gets validated, for instance, if i want to force a user to upload a picture on account creation, i still would have to adjust my production code in order to be able to test it…
@Eric: Sure, this could be possible, but requires extra setup with the machines. And I hope your Server won’t do anything with \\ URLs unless its not a Windows Server, which i hope even more less
May 4th, 2009 at 10:14 am
Good solution in Java described here:
http://gusiev.com/?p=3
Works for all browsers.
May 30th, 2009 at 5:10 am
This works
http://seleniumdeal.blogspot.com/2009/05/uploading-files-using-selenium-with-c.html