Skip to content

Instantly share code, notes, and snippets.

Created December 22, 2011 04:44
Show Gist options
  • Save santiycr/1508946 to your computer and use it in GitHub Desktop.
Save santiycr/1508946 to your computer and use it in GitHub Desktop.
Remote File Upload using Selenium 2's FileDetectors
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
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 {
Copy link

Does anyone how you could solve uploading a file to a remote server on js?

Copy link

Hi Santi,
I am using selenium with java and want to upload file.My application does not supports sendkeys because after clicking select file windows popup gets open up. So,I am using AutoIt in this case to handle waindowspop but want to run on remote machine(Suacebrowser)
Please help!

Copy link

Hi Santi,

I am using this implementation in JAVA, with latest version of Selenium and Firefox. It works fine in Chrome, but
I am getting the below error in firefox -

org.openqa.selenium.UnsupportedCommandException: Unrecognized command: POST /session/fd481a91-f664-114b-8363-f4a64ee30518/file
Command duration or timeout: 18 milliseconds
Build info: version: '2.53.0', revision: '35ae25b1534ae328c771e0856c93e187490ca824', time: '2016-03-15 10:43:46'
System info: host: 'Vaibhav.local', ip: '', 'Mac OS X', os.arch: 'x86_64', os.version: '10.10.5', java.version: '1.8.0_66'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=46.0.1, platform=MAC, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=false, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: fd481a91-f664-114b-8363-f4a64ee30518

Can you help me out?

Copy link

This procedure fails for me when the file in question is large. In my case, it's ~1.6GB. When I do this to a remote Selenium grid I have, it get an error that Java is out of heap space. Not sure how to fix that since I've already given both the hubs and the nodes a 16G max heap.

Copy link

file_detector needs updated to str if File.exist?(str) and File.file?(str) to follow a bug with string "." being treated as a folder

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment