Testing File Upload Functionality

How to test your web app’s File Upload functionality on BrowserStack Automate.

Introduction

This article shows how you can test your web app’s File Upload functionality in Automate by:

  • Using files on your local machine.
  • Using default media files on remote machines.
  • Using files downloaded from the web in the same Automate session.

Supported Browsers and Operating Systems

Component List
Supported OS All desktop OS (not supported on mobile devices)
Supported Browser All (Chrome, Firefox, Safari, IE, Edge)

Uploading files directly from your local machine

You can send a file to remote browsers—directly from your workstation—in Automate, through the LocalFileDetector method.

Usage

import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.LocalFileDetector;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.URL;

public class JavaSample {

  public static final String AUTOMATE_USERNAME = "YOUR_USERNAME";
  public static final String AUTOMATE_KEY = "YOUR_ACCESS_KEY";
  public static final String URL = "https://" + AUTOMATE_USERNAME + ":" + AUTOMATE_KEY + "@hub-cloud.browserstack.com/wd/hub";

  public static void main(String[] args) throws Exception {

    DesiredCapabilities caps = new DesiredCapabilities();
    caps.setCapability("browser", "internet explorer");
    caps.setCapability("os", "windows");
    caps.setCapability("browser_version", "11.0");
    caps.setCapability("os_version", "10");
    caps.setCapability("browserstack.sendKeys", "true");
    caps.setCapability("name", "Bstack-[Java] Sample Test");

    WebDriver driver = new RemoteWebDriver(new URL(URL), caps);
    driver.setFileDetector(new LocalFileDetector());
    driver.get("http://www.fileconvoy.com/");
    driver.findElement(By.id("upfile_0")).sendKeys("C:\\Users\\hello\\url.txt");
    driver.findElement(By.id("readTermsOfUse")).click();
    driver.findElement(By.name("form_upload")).submit();
    driver.quit();

  }
}
var webdriver = require('selenium-webdriver');
var remote = require('selenium-webdriver/remote');

// Input capabilities
var capabilities = {
  'browserName' : 'Chrome',
  'browser_version' : '75.0',
  'os' : 'Windows',
  'os_version' : '10',
  'name': 'Bstack-[NodeJS] Upload Test',
  'browserstack.sendKeys': 'true',
  'browserstack.debug': "true",
  'browserstack.user' : 'YOUR_USERNAME',
  'browserstack.key' : 'YOUR_ACCESS_KEY'
}

var driver = new webdriver.Builder().
  usingServer('http://hub-cloud.browserstack.com/wd/hub').
  withCapabilities(capabilities).
  build();

var fs = require('fs');

webdriver.WebDriver.prototype.saveScreenshot = function(filename) {
    return driver.takeScreenshot().then(function(data) {
        fs.writeFile(filename, data.replace(/^data:image\/png;base64,/,''), 'base64', function(err) {
            if(err) throw err;
        });
    })
};

//This will detect your local file
driver.setFileDetector(new remote.FileDetector);


driver.get('http://www.fileconvoy.com/').then(function(){
  // File path specific to Linux
  driver.findElement(webdriver.By.id('upfile_0')).sendKeys('/home/user/file_name').then(function(){
    driver.findElement(webdriver.By.id('readTermsOfUse')).click().then(function(){
      driver.findElement(webdriver.By.id('upload_button')).click().then(function(){
        driver.getTitle().then(function(title) {
          console.log(title);
          driver.quit();
        });
      });
    });
  });
});
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;

namespace SeleniumTest {
  class Program {
    static void Main(string[] args) {
      IWebDriver driver;
      DesiredCapabilities capability = new DesiredCapabilities();
      capability.SetCapability("browser", "Chrome");
      capability.SetCapability("browser_version", "75.0");
      capability.SetCapability("os", "Windows");
      capability.SetCapability("os_version", "10");
      capability.SetCapability("browserstack.sendKeys", "true");
      capability.SetCapability("browserstack.user", "YOUR_USERNAME");
      capability.SetCapability("browserstack.key", "YOUR_ACCESS_KEY");
      capability.SetCapability("name", "Bstack-[C_sharp] Sample Test");

      driver = new RemoteWebDriver(
        new Uri("http://hub-cloud.browserstack.com/wd/hub/"), capability
      );
      driver.Navigate().GoToUrl("http://www.fileconvoy.com");
      IWebElement uploadFile = driver.FindElement(By.Id("upfile_0"));
      Console.WriteLine(driver.Title);
      String path = "//local//file//path";
      LocalFileDetector detector =  new LocalFileDetector();
      driver.FileDetector = detector;
      uploadFile.SendKeys(path);
      driver.FindElement(By.Id("readTermsOfUse")).Click();
      driver.FindElement(By.Id("upload_button")).Click();
      String text = driver.FindElement(By.CssSelector("center b")).Text;
      Console.WriteLine(text);
      driver.Quit();
    }
  }
}
<?php

  require_once('vendor/autoload.php');
  use Facebook\WebDriver\Remote\RemoteWebDriver;
  use Facebook\WebDriver\WebDriverBy;

  $caps = array(
 "browser" => "Chrome",
 "browser_version" => "75.0",
 "os" => "Windows",
 "os_version" => "10",
 "browserstack.sendKeys" => "true",
 "name" => "Bstack-[Php] Sample Test"
);
  $web_driver = RemoteWebDriver::create(
    "https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub",
    $caps
  );
  $web_driver->get("http://www.fileconvoy.com");
  $file_input = $web_driver->findElement(WebDriverBy::id("upfile_0"));
  $file_input->setFileDetector(new LocalFileDetector());
  $file_input->sendKeys("C:\\Users\\hello\\url.txt");

  $web_driver->findElement(WebDriverBy::id("readTermsOfUse"))->click();
  $web_driver->findElement(WebDriverBy::name("form_upload"))->submit();
  sleep(5);
  $web_driver->quit();
?>
import time

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

desired_cap = {
 'browser': 'Internet Explorer',
 'browser_version': '11.0',
 'os': 'Windows',
 'os_version': '10',
 'browserstack.sendKeys': 'true',
 'name': 'Bstack-[Python] Sample Test'
}

driver = webdriver.Remote(
    command_executor='http://YOUR_USERNAME:YOUR_ACCESS_KEY@hub.browserstack.com/wd/hub',
    desired_capabilities=desired_cap)
driver.get('http://www.fileconvoy.com')
driver.find_element_by_id('upfile_0').send_keys('C:\\Users\\hello\\url.txt')
driver.find_element_by_id('readTermsOfUse').click()
driver.find_element_by_name('form_upload').submit()
driver.quit()
require 'rubygems'
require 'selenium-webdriver'

# Input capabilities
caps = Selenium::WebDriver::Remote::Capabilities.new
caps['browser'] = 'Internet Explorer'
caps['browser_version'] = '11.0'
caps['os'] = 'Windows'
caps['os_version'] = '10'
caps['name'] = 'Bstack-[Ruby] Sample Test'
caps['browserstack.sendKeys'] = 'true'


driver = Selenium::WebDriver.for(:remote,
  :url => "http://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub",
  :desired_capabilities => caps)
driver.file_detector = lambda do |args|
  str = args.first.to_s
  str if File.exist?(str)
end
driver.navigate.to "http://www.fileconvoy.com"
driver.find_element(:id, "upfile_0").send_keys("C:\\Users\\hello\\url.txt");
driver.find_element(:id, "readTermsOfUse").click;
driver.find_element(:name, "form_upload").submit;
sleep(5)
driver.quit
use Selenium::Remote::Driver;

#Input capabilities
my $extraCaps = {
  "browser" => "Chrome",
  "browser_version" => "75.0",
  "os" => "Windows",
  "os_version" => "10",
  "browserstack.sendKeys" => "true",
  "name" => "Bstack-[Perl] Sample Test"
};

my $login = "YOUR_USERNAME";
my $key = "YOUR_ACCESS_KEY";
my $host = "$login:$key\@hub-cloud.browserstack.com";

my $driver = new Selenium::Remote::Driver('remote_server_addr' => $host,
  'port' => '80', 'extra_capabilities' => $extraCaps);
my $remote_fname = $driver->upload_file("C:\\Users\\hello\\url.txt");
$driver->get('http://www.fileconvoy.com');
$driver->find_element('upfile_0','id')->send_keys($remote_fname);
$driver->find_element('readTermsOfUse', 'id')->click();
$driver->find_element('form_upload', 'name')->submit();
sleep 40;
$driver->quit();
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.LocalFileDetector;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.URL;

public class JavaSample {

  public static final String AUTOMATE_USERNAME = "YOUR_USERNAME";
  public static final String AUTOMATE_KEY = "YOUR_ACCESS_KEY";
  public static final String URL = "https://" + AUTOMATE_USERNAME + ":" + AUTOMATE_KEY + "@hub-cloud.browserstack.com/wd/hub";

  public static void main(String[] args) throws Exception {

    DesiredCapabilities caps = new DesiredCapabilities();
    caps.setCapability("browser", "chrome");
    caps.setCapability("os", "windows");
    caps.setCapability("browser_version", "75.0");
    caps.setCapability("os_version", "10");
    caps.setCapability("name", "Bstack-[Java] Sample Test");

    WebDriver driver = new RemoteWebDriver(new URL(URL), caps);
    driver.setFileDetector(new LocalFileDetector());
    driver.get("http://www.fileconvoy.com/");
    driver.findElement(By.id("upfile_0")).sendKeys("C:\\Users\\hello\\url.txt");
    driver.findElement(By.id("readTermsOfUse")).click();
    driver.findElement(By.name("form_upload")).submit();
    driver.quit();

  }
}
var webdriver = require('selenium-webdriver');
var remote = require('selenium-webdriver/remote');

// Input capabilities
var capabilities = {
  'browserName' : 'Chrome',
  'browser_version' : '75.0',
  'os' : 'Windows',
  'os_version' : '10',
  'name': 'Bstack-[NodeJS] Upload Test',
  'browserstack.debug': "true",
  'browserstack.user' : 'YOUR_USERNAME',
  'browserstack.key' : 'YOUR_ACCESS_KEY'
}

var driver = new webdriver.Builder().
  usingServer('http://hub-cloud.browserstack.com/wd/hub').
  withCapabilities(capabilities).
  build();

var fs = require('fs');

webdriver.WebDriver.prototype.saveScreenshot = function(filename) {
    return driver.takeScreenshot().then(function(data) {
        fs.writeFile(filename, data.replace(/^data:image\/png;base64,/,''), 'base64', function(err) {
            if(err) throw err;
        });
    })
};

//This will detect your local file
driver.setFileDetector(new remote.FileDetector);


driver.get('http://www.fileconvoy.com/').then(function(){
  // File path specific to Linux
  driver.findElement(webdriver.By.id('upfile_0')).sendKeys('/home/user/file_name').then(function(){
    driver.findElement(webdriver.By.id('readTermsOfUse')).click().then(function(){
      driver.findElement(webdriver.By.id('upload_button')).click().then(function(){
        driver.getTitle().then(function(title) {
          console.log(title);
          driver.quit();
        });
      });
    });
  });
})
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;

namespace SeleniumTest {
  class Program {
    static void Main(string[] args) {
      IWebDriver driver;
      DesiredCapabilities capability = new DesiredCapabilities();
      capability.SetCapability("browser", "Chrome");
      capability.SetCapability("browser_version", "75.0");
      capability.SetCapability("os", "Windows");
      capability.SetCapability("os_version", "10");
      capability.SetCapability("browserstack.user", "YOUR_USERNAME");
      capability.SetCapability("browserstack.key", "YOUR_ACCESS_KEY");
      capability.SetCapability("name", "Bstack-[C_sharp] Sample Test");

      driver = new RemoteWebDriver(
        new Uri("http://hub-cloud.browserstack.com/wd/hub/"), capability
      );
      driver.Navigate().GoToUrl("http://www.fileconvoy.com");
      IWebElement uploadFile = driver.FindElement(By.Id("upfile_0"));
      Console.WriteLine(driver.Title);
      String path = "//local//file//path";
      LocalFileDetector detector =  new LocalFileDetector();
      driver.FileDetector = detector;
      uploadFile.SendKeys(path);
      driver.FindElement(By.Id("readTermsOfUse")).Click();
      driver.FindElement(By.Id("upload_button")).Click();
      String text = driver.FindElement(By.CssSelector("center b")).Text;
      Console.WriteLine(text);
      driver.Quit();
    }
  }
}
<?php

  require_once('vendor/autoload.php');
  use Facebook\WebDriver\Remote\RemoteWebDriver;
  use Facebook\WebDriver\WebDriverBy;

  $caps = array(
 "browser" => "Chrome",
 "browser_version" => "75.0",
 "os" => "Windows",
 "os_version" => "10",
 "name" => "Bstack-[Php] Sample Test"
);
  $web_driver = RemoteWebDriver::create(
    "https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub",
    $caps
  );
  $web_driver->get("http://www.fileconvoy.com");
  $file_input = $web_driver->findElement(WebDriverBy::id("upfile_0"));
  $file_input->setFileDetector(new LocalFileDetector());
  $file_input->sendKeys("C:\\Users\\hello\\url.txt");

  $web_driver->findElement(WebDriverBy::id("readTermsOfUse"))->click();
  $web_driver->findElement(WebDriverBy::name("form_upload"))->submit();
  sleep(5);
  $web_driver->quit();
?>
import time

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

desired_cap = {
 'browser': 'Chrome',
 'browser_version': '75.0',
 'os': 'Windows',
 'os_version': '10',
 'name': 'Bstack-[Python] Sample Test'
}

driver = webdriver.Remote(
    command_executor='http://YOUR_USERNAME:YOUR_ACCESS_KEY@hub.browserstack.com/wd/hub',
    desired_capabilities=desired_cap)
driver.get('http://www.fileconvoy.com')
driver.find_element_by_id('upfile_0').send_keys('C:\\Users\\hello\\url.txt')
driver.find_element_by_id('readTermsOfUse').click()
driver.find_element_by_name('form_upload').submit()
driver.quit()
require 'rubygems'
require 'selenium-webdriver'

# Input capabilities
caps = Selenium::WebDriver::Remote::Capabilities.new
caps['browser'] = 'Chrome'
caps['browser_version'] = '75.0'
caps['os'] = 'Windows'
caps['os_version'] = '10'
caps['name'] = 'Bstack-[Ruby] Sample Test'


driver = Selenium::WebDriver.for(:remote,
  :url => "http://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub",
  :desired_capabilities => caps)
driver.file_detector = lambda do |args|
  str = args.first.to_s
  str if File.exist?(str)
end
driver.navigate.to "http://www.fileconvoy.com"
driver.find_element(:id, "upfile_0").send_keys("C:\\Users\\hello\\url.txt");
driver.find_element(:id, "readTermsOfUse").click;
driver.find_element(:name, "form_upload").submit;
sleep(5)
driver.quit
use Selenium::Remote::Driver;

#Input capabilities
my $extraCaps = {
  "browser" => "Chrome",
  "browser_version" => "75.0",
  "os" => "Windows",
  "os_version" => "10",
  "name" => "Bstack-[Perl] Sample Test"
};

my $login = "YOUR_USERNAME";
my $key = "YOUR_ACCESS_KEY";
my $host = "$login:$key\@hub-cloud.browserstack.com";

my $driver = new Selenium::Remote::Driver('remote_server_addr' => $host,
  'port' => '80', 'extra_capabilities' => $extraCaps);
my $remote_fname = $driver->upload_file("C:\\Users\\hello\\url.txt");
$driver->get('http://www.fileconvoy.com');
$driver->find_element('upfile_0','id')->send_keys($remote_fname);
$driver->find_element('readTermsOfUse', 'id')->click();
$driver->find_element('form_upload', 'name')->submit();
sleep 40;
$driver->quit();

Using default media files on remote desktop machines

By default, desktop platforms have some sample media files. This is also true for remote desktop instances spun up on BrowserStack.

You can use those files to test your web app’s file upload functionality—using Selenium’s LocalFileDetector and the file path.

These are the file paths you can use:

Windows 10, 8.1, 8, 7

*Video*
C:\Users\hello\Documents\video\saper.avi
C:\Users\hello\Documents\video\sample_mpeg4.mp4
C:\Users\hello\Documents\video\sample_iTunes.mov
C:\Users\hello\Documents\video\sample_mpeg2.m2v

*Images*
C:\Users\hello\Documents\images\wallpaper1.jpg
C:\Users\hello\Documents\images\icon.png
C:\Users\hello\Documents\images\cartoon-animation.gif

*Documents*
C:\Users\hello\url.txt
C:\Users\hello\Documents\documents\xls-sample1.xls
C:\Users\hello\Documents\documents\text-sample1.txt
C:\Users\hello\Documents\documents\pdf-sample1.pdf
C:\Users\hello\Documents\documents\doc-sample1.doc

*Audio*
C:\Users\hello\Documents\audio\first_noel.mp3
C:\Users\hello\Documents\audio\BachCPE_SonataAmin_1.wma
C:\Users\hello\Documents\audio\250Hz_44100Hz_16bit_05sec.wav

*Zip files*
C:\Users\hello\Documents\4KBzipFile.zip
C:\Users\hello\Documents\1MBzipFile.zip

Windows XP

C:\Users\hello\url.txt
*Video*
/Users/test1/Documents/video/saper.avi
/Users/test1/Documents/video/sample_mpeg4.mp4
/Users/test1/Documents/video/sample_iTunes.mov
/Users/test1/Documents/video/sample_mpeg2.m2v

*Images*
/Users/test1/Documents/images/wallpaper1.jpg
/Users/test1/Documents/images/icon.png
/Users/test1/Documents/images/cartoon-animation.gif

*Documents*
/Users/test1/url.txt
/Users/test1/Documents/documents/xls-sample1.xls
/Users/test1/Documents/documents/text-sample1.txt
/Users/test1/Documents/documents/pdf-sample1.pdf
/Users/test1/Documents/documents/doc-sample1.doc

*Audio*
/Users/test1/Documents/audio/first_noel.mp3
/Users/test1/Documents/audio/BachCPE_SonataAmin_1.wma
/Users/test1/Documents/audio/250Hz_44100Hz_16bit_05sec.wav

*Zip files*
/Users/test1/Documents/4KBzipFile.zip
/Users/test1/Documents/1MBzipFile.zip

Usage

import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.LocalFileDetector;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.URL;

public class JavaSample {

  public static final String AUTOMATE_USERNAME = "YOUR_USERNAME";
  public static final String AUTOMATE_KEY = "YOUR_ACCESS_KEY";
  public static final String URL = "https://" + AUTOMATE_USERNAME + ":" + AUTOMATE_KEY + "@hub-cloud.browserstack.com/wd/hub";

  public static void main(String[] args) throws Exception {

    DesiredCapabilities caps = new DesiredCapabilities();
    caps.setCapability("browser", "internet explorer");
    caps.setCapability("os", "windows");
    caps.setCapability("browser_version", "11.0");
    caps.setCapability("os_version", "10");
    caps.setCapability("browserstack.sendKeys", "true");
    caps.setCapability("name", "Bstack-[Java] Sample Test");

    WebDriver driver = new RemoteWebDriver(new URL(URL), caps);
    driver.setFileDetector(new LocalFileDetector());
    driver.get("http://www.fileconvoy.com/");
    driver.findElement(By.id("upfile_0")).sendKeys("C:\\Users\\hello\\url.txt");
    driver.findElement(By.id("readTermsOfUse")).click();
    driver.findElement(By.name("form_upload")).submit();
    driver.quit();

  }
}
var webdriver = require('selenium-webdriver');
var remote = require('selenium-webdriver/remote');

// Input capabilities
var capabilities = {
  'browserName' : 'Chrome',
  'browser_version' : '75.0',
  'os' : 'Windows',
  'os_version' : '10',
  'name': 'Bstack-[NodeJS] Upload Test',
  'browserstack.sendKeys': 'true',
  'browserstack.debug': "true",
  'browserstack.user' : 'YOUR_USERNAME',
  'browserstack.key' : 'YOUR_ACCESS_KEY'
}

var driver = new webdriver.Builder().
  usingServer('http://hub-cloud.browserstack.com/wd/hub').
  withCapabilities(capabilities).
  build();

var fs = require('fs');

webdriver.WebDriver.prototype.saveScreenshot = function(filename) {
    return driver.takeScreenshot().then(function(data) {
        fs.writeFile(filename, data.replace(/^data:image\/png;base64,/,''), 'base64', function(err) {
            if(err) throw err;
        });
    })
};

//This will detect your local file
driver.setFileDetector(new remote.FileDetector);


driver.get('http://www.fileconvoy.com/').then(function(){
  // File path specific to Linux
  driver.findElement(webdriver.By.id('upfile_0')).sendKeys('/home/user/file_name').then(function(){
    driver.findElement(webdriver.By.id('readTermsOfUse')).click().then(function(){
      driver.findElement(webdriver.By.id('upload_button')).click().then(function(){
        driver.getTitle().then(function(title) {
          console.log(title);
          driver.quit();
        });
      });
    });
  });
});
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;

namespace SeleniumTest {
  class Program {
    static void Main(string[] args) {
      IWebDriver driver;
      DesiredCapabilities capability = new DesiredCapabilities();
      capability.SetCapability("browser", "Chrome");
      capability.SetCapability("browser_version", "75.0");
      capability.SetCapability("os", "Windows");
      capability.SetCapability("os_version", "10");
      capability.SetCapability("browserstack.sendKeys", "true");
      capability.SetCapability("browserstack.user", "YOUR_USERNAME");
      capability.SetCapability("browserstack.key", "YOUR_ACCESS_KEY");
      capability.SetCapability("name", "Bstack-[C_sharp] Sample Test");

      driver = new RemoteWebDriver(
        new Uri("http://hub-cloud.browserstack.com/wd/hub/"), capability
      );
      driver.Navigate().GoToUrl("http://www.fileconvoy.com");
      IWebElement uploadFile = driver.FindElement(By.Id("upfile_0"));
      Console.WriteLine(driver.Title);
      String path = "//local//file//path";
      LocalFileDetector detector =  new LocalFileDetector();
      driver.FileDetector = detector;
      uploadFile.SendKeys(path);
      driver.FindElement(By.Id("readTermsOfUse")).Click();
      driver.FindElement(By.Id("upload_button")).Click();
      String text = driver.FindElement(By.CssSelector("center b")).Text;
      Console.WriteLine(text);
      driver.Quit();
    }
  }
}
<?php

  require_once('vendor/autoload.php');
  use Facebook\WebDriver\Remote\RemoteWebDriver;
  use Facebook\WebDriver\WebDriverBy;

  $caps = array(
 "browser" => "Chrome",
 "browser_version" => "75.0",
 "os" => "Windows",
 "os_version" => "10",
 "browserstack.sendKeys" => "true",
 "name" => "Bstack-[Php] Sample Test"
);
  $web_driver = RemoteWebDriver::create(
    "https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub",
    $caps
  );
  $web_driver->get("http://www.fileconvoy.com");
  $file_input = $web_driver->findElement(WebDriverBy::id("upfile_0"));
  $file_input->setFileDetector(new LocalFileDetector());
  $file_input->sendKeys("C:\\Users\\hello\\url.txt");

  $web_driver->findElement(WebDriverBy::id("readTermsOfUse"))->click();
  $web_driver->findElement(WebDriverBy::name("form_upload"))->submit();
  sleep(5);
  $web_driver->quit();
?>
import time

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

desired_cap = {
 'browser': 'Internet Explorer',
 'browser_version': '11.0',
 'os': 'Windows',
 'os_version': '10',
 'browserstack.sendKeys': 'true',
 'name': 'Bstack-[Python] Sample Test'
}

driver = webdriver.Remote(
    command_executor='http://YOUR_USERNAME:YOUR_ACCESS_KEY@hub.browserstack.com/wd/hub',
    desired_capabilities=desired_cap)
driver.get('http://www.fileconvoy.com')
driver.find_element_by_id('upfile_0').send_keys('C:\\Users\\hello\\url.txt')
driver.find_element_by_id('readTermsOfUse').click()
driver.find_element_by_name('form_upload').submit()
driver.quit()
require 'rubygems'
require 'selenium-webdriver'

# Input capabilities
caps = Selenium::WebDriver::Remote::Capabilities.new
caps['browser'] = 'Internet Explorer'
caps['browser_version'] = '11.0'
caps['os'] = 'Windows'
caps['os_version'] = '10'
caps['name'] = 'Bstack-[Ruby] Sample Test'
caps['browserstack.sendKeys'] = 'true'


driver = Selenium::WebDriver.for(:remote,
  :url => "http://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub",
  :desired_capabilities => caps)
driver.file_detector = lambda do |args|
  str = args.first.to_s
  str if File.exist?(str)
end
driver.navigate.to "http://www.fileconvoy.com"
driver.find_element(:id, "upfile_0").send_keys("C:\\Users\\hello\\url.txt");
driver.find_element(:id, "readTermsOfUse").click;
driver.find_element(:name, "form_upload").submit;
sleep(5)
driver.quit
use Selenium::Remote::Driver;

#Input capabilities
my $extraCaps = {
  "browser" => "Chrome",
  "browser_version" => "75.0",
  "os" => "Windows",
  "os_version" => "10",
  "browserstack.sendKeys" => "true",
  "name" => "Bstack-[Perl] Sample Test"
};

my $login = "YOUR_USERNAME";
my $key = "YOUR_ACCESS_KEY";
my $host = "$login:$key\@hub-cloud.browserstack.com";

my $driver = new Selenium::Remote::Driver('remote_server_addr' => $host,
  'port' => '80', 'extra_capabilities' => $extraCaps);
my $remote_fname = $driver->upload_file("C:\\Users\\hello\\url.txt");
$driver->get('http://www.fileconvoy.com');
$driver->find_element('upfile_0','id')->send_keys($remote_fname);
$driver->find_element('readTermsOfUse', 'id')->click();
$driver->find_element('form_upload', 'name')->submit();
sleep 40;
$driver->quit();
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.LocalFileDetector;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.URL;

public class JavaSample {

  public static final String AUTOMATE_USERNAME = "YOUR_USERNAME";
  public static final String AUTOMATE_KEY = "YOUR_ACCESS_KEY";
  public static final String URL = "https://" + AUTOMATE_USERNAME + ":" + AUTOMATE_KEY + "@hub-cloud.browserstack.com/wd/hub";

  public static void main(String[] args) throws Exception {

    DesiredCapabilities caps = new DesiredCapabilities();
    caps.setCapability("browser", "chrome");
    caps.setCapability("os", "windows");
    caps.setCapability("browser_version", "75.0");
    caps.setCapability("os_version", "10");
    caps.setCapability("name", "Bstack-[Java] Sample Test");

    WebDriver driver = new RemoteWebDriver(new URL(URL), caps);
    driver.setFileDetector(new LocalFileDetector());
    driver.get("http://www.fileconvoy.com/");
    driver.findElement(By.id("upfile_0")).sendKeys("C:\\Users\\hello\\url.txt");
    driver.findElement(By.id("readTermsOfUse")).click();
    driver.findElement(By.name("form_upload")).submit();
    driver.quit();

  }
}
var webdriver = require('selenium-webdriver');
var remote = require('selenium-webdriver/remote');

// Input capabilities
var capabilities = {
  'browserName' : 'Chrome',
  'browser_version' : '75.0',
  'os' : 'Windows',
  'os_version' : '10',
  'name': 'Bstack-[NodeJS] Upload Test',
  'browserstack.debug': "true",
  'browserstack.user' : 'YOUR_USERNAME',
  'browserstack.key' : 'YOUR_ACCESS_KEY'
}

var driver = new webdriver.Builder().
  usingServer('http://hub-cloud.browserstack.com/wd/hub').
  withCapabilities(capabilities).
  build();

var fs = require('fs');

webdriver.WebDriver.prototype.saveScreenshot = function(filename) {
    return driver.takeScreenshot().then(function(data) {
        fs.writeFile(filename, data.replace(/^data:image\/png;base64,/,''), 'base64', function(err) {
            if(err) throw err;
        });
    })
};

//This will detect your local file
driver.setFileDetector(new remote.FileDetector);


driver.get('http://www.fileconvoy.com/').then(function(){
  // File path specific to Linux
  driver.findElement(webdriver.By.id('upfile_0')).sendKeys('/home/user/file_name').then(function(){
    driver.findElement(webdriver.By.id('readTermsOfUse')).click().then(function(){
      driver.findElement(webdriver.By.id('upload_button')).click().then(function(){
        driver.getTitle().then(function(title) {
          console.log(title);
          driver.quit();
        });
      });
    });
  });
})
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;

namespace SeleniumTest {
  class Program {
    static void Main(string[] args) {
      IWebDriver driver;
      DesiredCapabilities capability = new DesiredCapabilities();
      capability.SetCapability("browser", "Chrome");
      capability.SetCapability("browser_version", "75.0");
      capability.SetCapability("os", "Windows");
      capability.SetCapability("os_version", "10");
      capability.SetCapability("browserstack.user", "YOUR_USERNAME");
      capability.SetCapability("browserstack.key", "YOUR_ACCESS_KEY");
      capability.SetCapability("name", "Bstack-[C_sharp] Sample Test");

      driver = new RemoteWebDriver(
        new Uri("http://hub-cloud.browserstack.com/wd/hub/"), capability
      );
      driver.Navigate().GoToUrl("http://www.fileconvoy.com");
      IWebElement uploadFile = driver.FindElement(By.Id("upfile_0"));
      Console.WriteLine(driver.Title);
      String path = "//local//file//path";
      LocalFileDetector detector =  new LocalFileDetector();
      driver.FileDetector = detector;
      uploadFile.SendKeys(path);
      driver.FindElement(By.Id("readTermsOfUse")).Click();
      driver.FindElement(By.Id("upload_button")).Click();
      String text = driver.FindElement(By.CssSelector("center b")).Text;
      Console.WriteLine(text);
      driver.Quit();
    }
  }
}
<?php

  require_once('vendor/autoload.php');
  use Facebook\WebDriver\Remote\RemoteWebDriver;
  use Facebook\WebDriver\WebDriverBy;

  $caps = array(
 "browser" => "Chrome",
 "browser_version" => "75.0",
 "os" => "Windows",
 "os_version" => "10",
 "name" => "Bstack-[Php] Sample Test"
);
  $web_driver = RemoteWebDriver::create(
    "https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub",
    $caps
  );
  $web_driver->get("http://www.fileconvoy.com");
  $file_input = $web_driver->findElement(WebDriverBy::id("upfile_0"));
  $file_input->setFileDetector(new LocalFileDetector());
  $file_input->sendKeys("C:\\Users\\hello\\url.txt");

  $web_driver->findElement(WebDriverBy::id("readTermsOfUse"))->click();
  $web_driver->findElement(WebDriverBy::name("form_upload"))->submit();
  sleep(5);
  $web_driver->quit();
?>
import time

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

desired_cap = {
 'browser': 'Chrome',
 'browser_version': '75.0',
 'os': 'Windows',
 'os_version': '10',
 'name': 'Bstack-[Python] Sample Test'
}

driver = webdriver.Remote(
    command_executor='http://YOUR_USERNAME:YOUR_ACCESS_KEY@hub.browserstack.com/wd/hub',
    desired_capabilities=desired_cap)
driver.get('http://www.fileconvoy.com')
driver.find_element_by_id('upfile_0').send_keys('C:\\Users\\hello\\url.txt')
driver.find_element_by_id('readTermsOfUse').click()
driver.find_element_by_name('form_upload').submit()
driver.quit()
require 'rubygems'
require 'selenium-webdriver'

# Input capabilities
caps = Selenium::WebDriver::Remote::Capabilities.new
caps['browser'] = 'Chrome'
caps['browser_version'] = '75.0'
caps['os'] = 'Windows'
caps['os_version'] = '10'
caps['name'] = 'Bstack-[Ruby] Sample Test'


driver = Selenium::WebDriver.for(:remote,
  :url => "http://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub",
  :desired_capabilities => caps)
driver.file_detector = lambda do |args|
  str = args.first.to_s
  str if File.exist?(str)
end
driver.navigate.to "http://www.fileconvoy.com"
driver.find_element(:id, "upfile_0").send_keys("C:\\Users\\hello\\url.txt");
driver.find_element(:id, "readTermsOfUse").click;
driver.find_element(:name, "form_upload").submit;
sleep(5)
driver.quit
use Selenium::Remote::Driver;

#Input capabilities
my $extraCaps = {
  "browser" => "Chrome",
  "browser_version" => "75.0",
  "os" => "Windows",
  "os_version" => "10",
  "name" => "Bstack-[Perl] Sample Test"
};

my $login = "YOUR_USERNAME";
my $key = "YOUR_ACCESS_KEY";
my $host = "$login:$key\@hub-cloud.browserstack.com";

my $driver = new Selenium::Remote::Driver('remote_server_addr' => $host,
  'port' => '80', 'extra_capabilities' => $extraCaps);
my $remote_fname = $driver->upload_file("C:\\Users\\hello\\url.txt");
$driver->get('http://www.fileconvoy.com');
$driver->find_element('upfile_0','id')->send_keys($remote_fname);
$driver->find_element('readTermsOfUse', 'id')->click();
$driver->find_element('form_upload', 'name')->submit();
sleep 40;
$driver->quit();

Using a file downloaded from the web in the same Automate session

Through your test script, you can instruct the remote browsers to:

  1. Navigate to a URL.
  2. Download a specific file.
  3. Upload that file to the web app under test.

To do this, you’ll need the file path. The file will be downloaded in these locations on the remote desktops:

Win XP

C:\Documents and Settings\hello\Downloads

Win 7 / 8 / 8.1 / 10

C:\Users\hello\Downloads

Mac

/Users/test1/Downloads

You can then upload it to the web app under test using LocalFileDetector and sendkeys method.

We're sorry to hear that. Please share your feedback so we can do better






Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked





Thank you for your valuable feedback