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.
blog comments powered by Disqus
tag cloud
.NET AAAE aeroweb agile airport becamp becamp2008 CBIC CI citcon cms Conference continuous integration estimation expoQA expoQA2008 FOSE gocho Government HighTechCville java Javascript Lucene MySQL open source Open Source for the Government PHP python rails railsrumble rspec Ruby scrum security selenium semantic web ShRUG social media software solr Speaking svn testing Web 3.0 for the government whichrubycmsshouldisuse
categories
Business Case Study Community Conference Continuous Integration Government News Opinion Productivity Programming Project Management Ruby scrum Site Search Analytics software solr Speaking System Administration Uncategorized