Embed URL


SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Remote File Upload using Selenium 2's FileDetectors

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
import junit.framework.Assert;
import junit.framework.TestCase;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.*;
import java.util.concurrent.TimeUnit;
public class TestingUploadSe2Sauce extends TestCase {
private RemoteWebDriver driver;
public void setUp() throws Exception {
DesiredCapabilities capabillities = DesiredCapabilities.firefox();
capabillities.setCapability("version", "7");
capabillities.setCapability("platform", Platform.XP);
capabillities.setCapability("selenium-version", "2.18.0");
capabillities.setCapability("name", "Remote File Upload using Selenium 2's FileDetectors");
driver = new RemoteWebDriver(
new URL("http://<username>:<api-key>"),
driver.setFileDetector(new LocalFileDetector());
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
public void testSauce() throws Exception {
WebElement upload = driver.findElement("myfile"));
Assert.assertEquals("darkbulb.jpg (image/jpeg)", driver.findElement(By.tagName("p")).getText());
public void tearDown() throws Exception {
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
require 'rubygems'
require "test/unit"
require 'selenium-webdriver'
class ExampleTest < Test::Unit::TestCase
def setup
caps = Selenium::WebDriver::Remote::Capabilities.firefox
caps.version = "5"
caps.platform = :XP
caps[:name] = "Remote File Upload using Selenium 2 with Ruby"
caps['selenium-version'] = "2.18.0"
@driver = Selenium::WebDriver.for(
:url => "http://<username>:<api-key>",
:desired_capabilities => caps)
@driver.file_detector = lambda do |args|
# args => ["/path/to/file"]
str = args.first.to_s
str if File.exist?(str)
def test_sauce ""
element = @driver.find_element(:id, 'myfile')
element.send_keys "/Users/sso/SauceLabs/sauce/hostess/maitred/maitred/public/images/darkbulb.jpg"
@driver.find_element(:id, "submit").click
@driver.find_element(:tag_name, "img")
assert "darkbulb.jpg (image/jpeg)" == @driver.find_element(:tag_name, "p").text
def teardown

The relevant lines in this gist for the upload are #21 (where the LocalFileDetector is set) and #28 (where the file input is typed into).
Here's the result job in Sauce Labs:

This is great. Thanks for this Santi. I have some questions regarding the actual path here, upload.sendKeys("/Users/sso/the/local/path/to/darkbulb.jpg");

If I'm using Jenkins and GitHub as my repo, would the file path be relative to the Jenkins box? Also, is there a way to reference the file to upload from the GitHub repo? Any help would be greatly appreciated, thanks!


Happy to hear it's useful. Reading Java's implementation, seems like relative paths can also be used:

The file path probably needs to be relative to the test case file, so something like the following would get you to the root of your repo: "../../../whereever/the/file/is/located"

Hope that helps.

Thanks for your help!

Hi Santi,

I'm getting the following error when I attempt to upload a .jpg to onDemand. I'm doing the same thing as the above in respect to line 21, 28.
Is there an issue on your end? Any help would be greatly appreciated. Thanks!!

Exception in thread "main" org.openqa.selenium.UnsupportedCommandException:
Command duration or timeout: 567 milliseconds
Build info: version: '2.15.0', revision: '15105', time: '2011-12-08 09:57:28'
System info: 'Mac OS X', os.arch: 'x86_64', os.version: '10.7.2', java.version: '1.6.0_29'
Driver info: driver.version: RemoteWebDriver
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(
at java.lang.reflect.Constructor.newInstance(
at org.openqa.selenium.remote.ErrorHandler.createThrowable(
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(
at org.openqa.selenium.remote.RemoteWebDriver.execute(
at org.openqa.selenium.remote.RemoteWebElement.execute(
at org.openqa.selenium.remote.RemoteWebElement.upload(
at org.openqa.selenium.remote.RemoteWebElement.sendKeys(
at core.AssetsPage.uploadImageAsset(
at tests.ImageAssetUploadTest.(
at core.TestSuite.executeRegressionTestSuite(
at core.JenkinsMain.runTestSuite(
at core.JenkinsMain.main(

This requires some of the later versions of selenium. As near as I can tell, you'll need an updated client and server. I've tested using v2.19.0 of both client and server, and it worked for me; your mileage may vary.

If you're testing through sauce, you'll need to set the desired capabilities for your driver to use the latest version, as so:

Thanks yurodivule. Santi got back to me and said the same thing and it worked. Thanks again.

Santi. can you provide a sample code for python web driver implementation?


daluu commented

/would also be nice to figure out how to do this with JSONWireProtocol, so that can use this with PHP and other 3rd party bindings.

Hey Santi,

I have a question. Say you are running the same test on windows and linux machine in that case the path to the images will change right? For eg the path to image on a windows will be different from path to image on linux. So if I have some image paths in my tests and when i am running them on saucelabs how will the test pick up my images from the sauce VM which is running my test?

i'm wondering if this technique has issues with test run on iOS browsers? works great in 'regular' browsers, but i think i am seeing that it is not working as expected when testing against ipad and iphone browsers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.