File Uploads with Selenium

Eric PughJune 6, 2007

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/

The Selenium 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.




More blog articles:


Let's do a project together!

We provide tailored search, discovery and personalization solutions using Solr and Elasticsearch. Learn more about our service offerings