public

Remote File Upload using Selenium 2's FileDetectors

  • Download Gist
TestingUploadSe2Sauce.java
Java
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
import junit.framework.Assert;
import junit.framework.TestCase;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.*;
import java.net.URL;
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.15.0");
capabillities.setCapability("name", "Remote File Upload using Selenium 2's FileDetectors");
 
driver = new RemoteWebDriver(
new URL("http://<username>:<api-key>@ondemand.saucelabs.com:80/wd/hub"),
capabillities);
driver.setFileDetector(new LocalFileDetector());
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
 
public void testSauce() throws Exception {
driver.get("http://sso.dev.saucelabs.com/test/guinea-file-upload");
WebElement upload = driver.findElement(By.id("myfile"));
upload.sendKeys("/Users/sso/the/local/path/to/darkbulb.jpg");
driver.findElement(By.id("submit")).click();
driver.findElement(By.tagName("img"));
Assert.assertEquals("darkbulb.jpg (image/jpeg)", driver.findElement(By.tagName("p")).getText());
}
 
public void tearDown() throws Exception {
driver.quit();
}
}
testing_upload_se2_sauce.rb
Ruby
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.16.1"
 
@driver = Selenium::WebDriver.for(
:remote,
:url => "http://<username>:<api-key>@saucelabs.com:4444/wd/hub",
:desired_capabilities => caps)
 
@driver.file_detector = lambda do |args|
# args => ["/path/to/file"]
str = args.first.to_s
str if File.exist?(str)
end
end
 
def test_sauce
@driver.navigate.to "http://sso.dev.saucelabs.com/test/guinea-file-upload"
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
end
 
def teardown
@driver.quit
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.