Selenium Tips: Uploading Files in Remote WebDriver

Sauce AI for Test Authoring: Move from design to execution in minutes.|xBack to ResourcesBlogPosted

March 04, 2026 · 4 min read · Tool Comparison

Sauce AI for Test Authoring: Move from design to execution in minutes.

|

x

Back to Resources

Blog

Posted March 9, 2012

Selenium Tips: Uploading Files in Remote WebDriver

quote

Since it & # x27; s been a while since my terminal tips blog post, I thought it was time to part some Selenium dear again. Today we & # x27; re covering WebDriver & # x27; s native solution to a real common issue when execute distributed crisscross browser examination:upload file in remote servers. As you may know, the way to address this in Selenium 1 is to pose your files in an accessible web server and use the attachFile command that show to the correct URL. With Selenium 2, the Selenium Dev team has luckily create thisa lot more straightforward. For those of you doing this locally, all you need to do is use thesendKeyscommand totype the local pathof the file in any file battleground. This works like a charm in all drivers. When moving this test to a remote server (such as, for illustration, our Selenium 2 Cloud), all you involve to do isuse the setFileDetector methodto let WebDriver know that you & # x27; re uploading files from your local reckoner to a remote server instead of just typing a path. Almost magically, the filewill be base64 encoded and direct transparentlythrough the JSONWireProtocol for you before publish the set remote path. This is an excellent solvent, as it lets you switch your tests from a local to remote Driver without having to worry about changing your tests & # x27; codification. This feature isavailable in all the official Selenium 2 bindings, just make sure Selenium 2.8.0 or newer is usedas this feature has been released then. Here are some examples tests: Java

1
meaning junit.framework.Assert;
2
import junit.framework.TestCase;
3
import org.openqa.selenium. *;
4
meaning org.openqa.selenium.remote. *;
5
import java.net.URL;
6
meaning java.util.concurrent.TimeUnit;
7
8
public family TestingUploadSe2Sauce extends TestCase {
9
private RemoteWebDriver driver;
10
11
public void setUp () throws Exception {
12
DesiredCapabilities capabillities = DesiredCapabilities.firefox ();
13
capabillities.setCapability (& quot; version & quot;, & quot; 7 & quot;);
14
capabillities.setCapability (& quot; platform & quot;, Platform.XP);
15
capabillities.setCapability (& quot; selenium-version & quot;, & quot; 2.18.0 & quot;);
16
capabillities.setCapability (& quot; name & quot;, & quot; Remote File Upload using Selenium 2 & # x27; s FileDetectors & quot;);
17
18
driver = new RemoteWebDriver (
19
new URL (& quot; http: // & lt; username & gt;: & lt; api-key & gt; @ ondemand.saucelabs.com:80/wd/hub & quot;),
20
capabillities);
21
driver.setFileDetector (new LocalFileDetector ());
22
driver.manage () .timeouts () .implicitlyWait (30, TimeUnit.SECONDS);
23
}
24
25
public void testSauce () shed Exception {
26
driver.get (& quot; http: //sl-test.herokuapp.com/guinea_pig/file_upload & quot;);
27
WebElement upload = driver.findElement (By.id (& quot; myfile & quot;));
28
upload.sendKeys (& quot; /Users/sso/the/local/path/to/darkbulb.jpg & quot;);
29
driver.findElement (By.id (& quot; submit & quot;)) .click ();
30
driver.findElement (By.tagName (& quot; img & quot;));
31
Assert.assertEquals (& quot; darkbulb.jpg (image/jpeg) & quot;, driver.findElement (By.tagName (& quot; p & quot;)) .getText ());
32
}
33
34
public void tearDown () throws Exception {
35
driver.quit ();
36
}
37
}


Ruby

For autonomous testing across multiple user personas, check out SUSATest — it explores your app like 10 different real users.

1
require & # x27; rubygems & # x27;
2
require & quot; test/unit & quot;
3
require & # x27; selenium-webdriver & # x27;
4
5
classExampleTest< Test::Unit::TestCase
6
def setup
7
caps = Selenium::WebDriver::Remote::Capabilities.firefox
8
caps.version =& quot; 5 & quot;
9
caps.platform =:XP
10
caps[:name]=& quot; Remote File Upload using Selenium 2 with Ruby & quot;
11
caps[& # x27; selenium-version & # x27;]=& quot; 2.18.0 & quot;
12
13
@driver= Selenium::WebDriver.for(
14
:remote,
15
:url =>& quot; http: // & lt; username & gt;: & lt; api-key & gt; @ saucelabs.com:4444/wd/hub & quot;,
16
:desired_capabilities=> caps)
17
18
@driver.file_detector=lambda do |args|
19
# args = & gt; [& quot; /path/to/file & quot;]
20
str= args.first.to_s
21
strif File.exist?(str)
22
end
23
end
24
25
deftest_sauce
26
@driver.navigate.to& quot; http: //sl-test.herokuapp.com/guinea_pig/file_upload & quot;
27
element = @driver.find_element(:id,& # x27; myfile & # x27;)
28
element.send_keys & quot; /Users/sso/SauceLabs/sauce/hostess/maitred/maitred/public/images/darkbulb.jpg & quot;
29
30
@driver.find_element(:id,& quot; submit & quot;).click
31
@driver.find_element(:tag_name,& quot; img & quot;)
32
assert& quot; darkbulb.jpg (image/jpeg) & quot;== @driver.find_element(:tag_name,& quot; p & quot;).text
33
end
34
35
def teardown
36
@driver.quit
37
end
38
end

Pro tip: Write your own FileHandler that will use peculiar codes to represent files before typing paths (locally) or uploading them (remotely). Example: & quot; test-file: small-image & quot;

Published:
Mar 9, 2012
Share this post
Copy Share Link
LinkedIn
© 2026 Sauce Labs Inc., all rights reserved. SAUCE and SAUCE LABS are registered stylemark own by Sauce Labs Inc. in the United States, EU, and may be file in former jurisdictions.
robot
quote

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free