We don't formally offer a way to upload files to test file upload functionally, but there are several ways this can be done.
1. Use the LocalFileDetector
function that is native to Selenium
Here are some example tests:
JAVA
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 TestingUploadSauce extends TestCase { private RemoteWebDriver driver; public void setUp() throws Exception { DesiredCapabilities capabillities = DesiredCapabilities.firefox(); capabillities.setCapability("version", "7"); capabillities.setCapability("platform", Platform.WIN10); capabillities.setCapability("name", "Remote File Upload Test"); driver = new RemoteWebDriver( new URL("https://<username>:<api-key>@ondemand.us-west-1.saucelabs.com:443/wd/hub"), capabillities); driver.setFileDetector(new LocalFileDetector()); driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); } public void testSauce() throws Exception { driver.get("http://sl-test.herokuapp.com/guinea_pig/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(); } } |
RUBY
require 'rubygems' require "test/unit" require 'selenium-webdriver' class ExampleTest < Test::Unit::TestCase def setup caps = Selenium::WebDriver::Remote::Capabilities.firefox caps.version = "54" caps.platform = :WIN10 caps[:name] = "Remote File Upload with Ruby" @driver = Selenium::WebDriver.for( :remote, :url => "https://<username>:<api-key>@ondemand.saucelabs.com:443/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://sl-test.herokuapp.com/guinea_pig/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 |
2. Use a pre-run executable to download a file from the internet
There is some information on this here , although that doc still references old Sauce Storage. We recommend using the new Sauce Storage to store your pre-run.
NOTE: If you want to download a file that is only accessible on your network, this won't work even if you have Sauce Connect running. This is because the connection from the VM to your network is limited only to the browsers and doesn't work on all outbound connections
As part of your test, navigate to a site that contains your file and download it automatically to the Downloads file
The downloaded files can be found here:
Windows = C:\Users\sauce\Downloads
Mac = /Users/chef/Downloads
Linux = /home/chef/Downloads
NOTE: The expected behavior and download path can vary between OS/browser combination and file type (for example, the file may be displayed on the browser instead of downloading).