Suppose your test includes downloading a file from the web. This file is downloaded to the following location on the BrowserStack remote computer:
Downloading a file from the web considerably slows down the execution of your test.
Within the same Automate TurboScale session, the file is available in later test steps. As a result, you can use the downloaded file to test your file upload feature.
When testing, you have to similarly modify your script to access the recently downloaded file and upload it to the web app you want to test. See the following sample scripts:
Selenium 4 W3C test scripts to test on desktops
```java
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.LocalFileDetector;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
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://hub-cloud.browserstack.com/wd/hub";
public static void main(String[] args) throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("browserName", "Chrome");
capabilities.setCapability("browserVersion", latest);
HashMap<String, Object> browserstackOptions = new HashMap<String, Object>();
browserstackOptions.put("os", "Windows");
browserstackOptions.put("projectName", "Sample Test");
browserstackOptions.put("buildName", "Sample_test");
capabilities.setCapability("bstack:options", browserstackOptions);
RemoteWebDriver driver = new RemoteWebDriver(new URL(URL), caps);
driver.setFileDetector(new LocalFileDetector());
driver.get("https://www.fileconvoy.com/");
driver.findElement(By.id("upfile_0")).sendKeys("C:\\Users\\hello\\Documents\\audio\\first_noel.mp3"); //File path in remote machine
driver.findElement(By.id("readTermsOfUse")).click();
driver.findElement(By.name("upload_button")).submit();
JavascriptExecutor jse = (JavascriptExecutor)driver;
try {
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("TopMessage")));
if(driver.findElementById("TopMessage").getText().contains("successfully uploaded")) {
jse.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \"passed\", \"reason\": \"File uploaded successfully\"}}");
} else {
jse.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \"failed\", \"reason\": \"File upload failed\"}}");
}
}
catch(Exception e) {
jse.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \"failed\", \"reason\": \"File could not be uploaded in 5 seconds\"}}");
}
driver.quit();
}
}
```
Copy icon
Copy
```javascript
const webdriver = require("selenium-webdriver");
const remote = require("selenium-webdriver/remote");
// Input capabilities
var capabilities = {
'bstack:options' : {
"os" : "Windows",
"projectName" : "Sample Test",
"buildName" : "Sample_test",
"userName" : "YOUR_USERNAME",
"accessKey" : "YOUR_ACCESS_KEY",
},
"browserName" : "Chrome",
"browserVersion" : latest,
}
let driver = new webdriver.Builder()
.usingServer('https://hub-cloud.browserstack.com/wd/hub')
.withCapabilities(capabilities)
.build();
//This will detect your local file
driver.setFileDetector(new remote.FileDetector());
(async () => {
await driver.get("https://www.fileconvoy.com");
const filePathElement = await driver.findElement(webdriver.By.id("upfile_0"));
await filePathElement.sendKeys("C:\\Users\\hello\\Documents\\audio\\first_noel.mp3");
await (await driver.findElement(webdriver.By.id("readTermsOfUse"))).click();
await (await driver.findElement(webdriver.By.name("upload_button"))).click();
try {
await driver.wait(webdriver.until.elementIsVisible((await driver.findElement(webdriver.By.id('TopMessage')))), 5000);
if((await driver.findElement(webdriver.By.id('TopMessage')).getText()).includes('successfully uploaded')) {
await driver.executeScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed","reason": "File upload successful"}}');
} else {
await driver.executeScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed","reason": "File upload failed"}}');
}
} catch (e) {
await driver.executeScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed","reason": "File could not be uploaded in time"}}');
}
await driver.quit();
})();
```
Copy icon
Copy
```csharp
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
namespace SeleniumTest
{
class Program
{
static void Main(string[] args)
{
IWebDriver driver;
ChromeOptions capabilities = new ChromeOptions();
capabilities.BrowserVersion = latest;
Dictionary<string, object> browserstackOptions = new Dictionary<string, object>();
browserstackOptions.Add("os", "Windows");
browserstackOptions.Add("projectName", "Sample Test");
browserstackOptions.Add("buildName", "Sample_test");
browserstackOptions.Add("userName", "YOUR_USERNAME");
browserstackOptions.Add("accessKey", "YOUR_ACCESS_KEY");
browserstackOptions.Add("browserName", "Chrome");
capabilities.AddAdditionalOption("bstack:options", browserstackOptions);
driver = new RemoteWebDriver(
new Uri("https://hub-cloud.browserstack.com/wd/hub"), capabilities
);
driver.Navigate().GoToUrl("https://www.fileconvoy.com");
IWebElement uploadFile = driver.FindElement(By.Id("upfile_0"));
Console.WriteLine(driver.Title);
String path = "C:\\Users\\hello\\Documents\\audio\\first_noel.mp3"; //File path in remote machine
LocalFileDetector detector = new LocalFileDetector();
var allowsDetection = driver as IAllowsFileDetection;
if (allowsDetection != null)
{
allowsDetection.FileDetector = new LocalFileDetector();
}
uploadFile.SendKeys(path);
driver.FindElement(By.Id("readTermsOfUse")).Click();
driver.FindElement(By.Id("upload_button")).Click();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
if (driver.FindElement(By.CssSelector("#TopMessage")).Text.Contains("successfully uploaded"))
{
((IJavaScriptExecutor)driver).ExecuteScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"passed\", \"reason\": \"File uploaded successfully!\"}}");
}
else
{
((IJavaScriptExecutor)driver).ExecuteScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"failed\", \"reason\": \"File upload failed!\"}}");
}
driver.Quit();
}
}
}
```
Copy icon
Copy
```python
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
desired_cap = {
'bstack:options' : {
"os" : "Windows",
"projectName" : "Sample Test",
"buildName" : "Sample_test",
"userName" : "YOUR_USERNAME",
"accessKey" : "YOUR_ACCESS_KEY",
},
"browserName" : "Chrome",
"browserVersion" : latest,
}
options.set_capability('bstack:options', desired_cap)
driver = webdriver.Remote(
command_executor='https://hub-cloud.browserstack.com/wd/hub',
options=options)
driver.get('https://www.fileconvoy.com')
driver.find_element_by_id('upfile_0').send_keys('C:\\Users\\hello\\Documents\\audio\\first_noel.mp3') #File path in remote machine
driver.find_element_by_id('readTermsOfUse').click()
driver.find_element_by_name('upload_button').submit()
try:
WebDriverWait(driver, 5).until(lambda x: x.find_element_by_id('TopMessage'))
if(driver.find_element_by_id('TopMessage').text == "Your file(s) have been successfully uploaded."):
# Setting the status of test as 'passed' or 'failed' based on the condition; if title of the web page starts with 'BrowserStack'
driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed", "reason": "File uploaded!"}}')
else:
driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "File upload failed"}}')
except TimeoutException:
driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "File failed to upload in 5 seconds"}}')
driver.quit()
```
Copy icon
Copy
```ruby
require 'rubygems'
require 'selenium-webdriver'
# Input capabilities
capabilities = {
'bstack:options' => {
"os" => "Windows",
"projectName" => "Sample Test",
"buildName" => "Sample_test",
"javascriptEnabled" => "true"
"userName" => "YOUR_USERNAME",
"accessKey" => "YOUR_ACCESS_KEY",
},
"browserName" => "Chrome",
"browserVersion" => latest,
}
driver = Selenium::WebDriver.for(
:remote,
:url => "https://hub-cloud.browserstack.com/wd/hub",
:capabilities => capabilities
)
driver.file_detector = lambda do |args|
str = args.first.to_s
str if File.exist?(str)
end
driver.navigate.to "https://www.fileconvoy.com"
driver.find_element(:id, "upfile_0").send_keys("C:\\Users\\hello\\Documents\\audio\\first_noel.mp3") #File path in remote machine
driver.execute_script('document.getElementById("readTermsOfUse").click();')
driver.find_element(:name, "upload_button").submit
sleep(5)
driver.quit
```
Copy icon
Copy
If you want to use your own files for a test, there are two ways to do it:
Upload files from your computer to the remote BrowserStack computer during the test.
Preload files from your computer to the BrowserStack server, and then access the files during the test.
You can upload up to 10 media files to the BrowserStack server. By default, files are deleted from the server after 30 days from the date of upload.
Upload files to the remote computer
You can upload files to the remote computer by adding these two methods in your test script:
Local File Detector , to detect files on a local computer
Send Keys , to specify the location of the files
The Local File Detector method works for testing on Windows and Linux computers.
See the following sample scripts for reference:
Selenium 4 W3C test scripts to test on desktops
```java
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.LocalFileDetector;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
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://hub-cloud.browserstack.com/wd/hub";
public static void main(String[] args) throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("browserName", "Chrome");
capabilities.setCapability("browserVersion", latest);
HashMap<String, Object> browserstackOptions = new HashMap<String, Object>();
browserstackOptions.put("os", "Windows");
browserstackOptions.put("projectName", "Upload Files");
browserstackOptions.put("buildName", "Upload_file");
capabilities.setCapability("bstack:options", browserstackOptions);
RemoteWebDriver driver = new RemoteWebDriver(new URL(URL), capabilities);
driver.setFileDetector(new LocalFileDetector());
driver.get("https://www.fileconvoy.com/");
driver.findElement(By.id("upfile_0")).sendKeys("//local//file//path");
driver.findElement(By.id("readTermsOfUse")).click();
driver.findElement(By.name("upload_button")).submit();
JavascriptExecutor jse = (JavascriptExecutor)driver;
try {
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("TopMessage")));
if(driver.findElementById("TopMessage").getText().contains("successfully uploaded")) {
jse.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \"passed\", \"reason\": \"File uploaded successfully\"}}");
} else {
jse.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \"failed\", \"reason\": \"File upload failed\"}}");
}
}
catch(Exception e) {
jse.executeScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\": \"failed\", \"reason\": \"File could not be uploaded in 5 seconds\"}}");
}
driver.quit();
}
}
```
Copy icon
Copy
```javascript
const webdriver = require("selenium-webdriver");
const remote = require("selenium-webdriver/remote");
// Input capabilities
var capabilities = {
'bstack:options' : {
"os" : "Windows",
"projectName" : "Upload Files",
"buildName" : "Upload_file",
"userName" : "YOUR_USERNAME",
"accessKey" : "YOUR_ACCESS_KEY"
},
"browserName" : "Chrome",
"browserVersion" : latest,
}
let driver = new webdriver.Builder()
.usingServer('https://hub-cloud.browserstack.com/wd/hub')
.withCapabilities(capabilities)
.build();
//This will detect your local file
driver.setFileDetector(new remote.FileDetector());
(async () => {
await driver.get("https://www.fileconvoy.com");
const filePathElement = await driver.findElement(webdriver.By.id("upfile_0"));
await filePathElement.sendKeys("//local//file//path");
await (await driver.findElement(webdriver.By.id("readTermsOfUse"))).click();
await (await driver.findElement(webdriver.By.name("upload_button"))).click();
try {
await driver.wait(webdriver.until.elementIsVisible((await driver.findElement(webdriver.By.id('TopMessage')))), 5000);
if((await driver.findElement(webdriver.By.id('TopMessage')).getText()).includes('successfully uploaded')) {
await driver.executeScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed","reason": "File upload successful"}}');
} else {
await driver.executeScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed","reason": "File upload failed"}}');
}
} catch (e) {
await driver.executeScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed","reason": "File could not be uploaded in time"}}');
}
await driver.quit();
})();
```
Copy icon
Copy
```csharp
using System;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
namespace SeleniumTest
{
class Program
{
static void Main(string[] args)
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
IWebDriver driver;
ChromeOptions capabilities = new ChromeOptions();
capabilities.BrowserVersion = latest;
Dictionary<string, object> browserstackOptions = new Dictionary<string, object>();
browserstackOptions.Add("os", "Windows");
browserstackOptions.Add("projectName", "Upload Files");
browserstackOptions.Add("buildName", "Upload_file");
browserstackOptions.Add("userName", "YOUR_USERNAME");
browserstackOptions.Add("accessKey", "YOUR_ACCESS_KEY");
browserstackOptions.Add("browserName", "Chrome");
capabilities.AddAdditionalOption("bstack:options", browserstackOptions);
driver = new RemoteWebDriver(
new Uri("https://hub-cloud.browserstack.com/wd/hub"), capabilities
);
driver.Navigate().GoToUrl("https://www.fileconvoy.com");
IWebElement uploadFile = driver.FindElement(By.Id("upfile_0"));
Console.WriteLine(driver.Title);
String path = "//path//to//your//local//file"; //File path in your local machine
LocalFileDetector detector = new LocalFileDetector();
var allowsDetection = driver as IAllowsFileDetection;
if (allowsDetection != null)
{
allowsDetection.FileDetector = detector;
}
uploadFile.SendKeys(path);
driver.FindElement(By.Id("readTermsOfUse")).Click();
driver.FindElement(By.Id("upload_button")).Click();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
if (driver.FindElement(By.CssSelector("#TopMessage")).Text.Contains("successfully uploaded"))
{
((IJavaScriptExecutor)driver).ExecuteScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"passed\", \"reason\": \"File uploaded successfully!\"}}");
}
else
{
((IJavaScriptExecutor)driver).ExecuteScript("browserstack_executor: {\"action\": \"setSessionStatus\", \"arguments\": {\"status\":\"failed\", \"reason\": \"File upload failed!\"}}");
}
driver.Quit();
}
}
}
```
Copy icon
Copy
```python
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
desired_cap = {
'bstack:options' : {
"os" : "Windows",
"projectName" : "Upload Files",
"buildName" : "Upload_file",
"userName" : "YOUR_USERNAME",
"accessKey" : "YOUR_ACCESS_KEY"
},
"browserName" : "Chrome",
"browserVersion" : latest,
}
options.set_capability('bstack:options', desired_cap)
driver = webdriver.Remote(
command_executor='https://hub-cloud.browserstack.com/wd/hub',
options=options)
driver.get('https://www.fileconvoy.com')
driver.find_element_by_id('upfile_0').send_keys('//local//file//path')
driver.find_element_by_id('readTermsOfUse').click()
driver.find_element_by_name('upload_button').submit()
try:
WebDriverWait(driver, 5).until(lambda x: x.find_element_by_id('TopMessage'))
if(driver.find_element_by_id('TopMessage').text == "Your file(s) have been successfully uploaded."):
# Setting the status of test as 'passed' or 'failed' based on the condition; if title of the web page starts with 'BrowserStack'
driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed", "reason": "File uploaded!"}}')
else:
driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "File upload failed"}}')
except TimeoutException:
driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed", "reason": "File failed to upload in 5 seconds"}}')
driver.quit()
```
Copy icon
Copy
```ruby
require 'rubygems'
require 'selenium-webdriver'
# Input capabilities
capabilities = {
'bstack:options' => {
"os" => "Windows",
"projectName" => "Upload Files",
"buildName" => "Upload_file",
"javascriptEnabled" => "true",
"userName" => "YOUR_USERNAME",
"accessKey" => "YOUR_ACCESS_KEY"
},
"browserName" => "Chrome",
"browserVersion" => latest,
}
driver = Selenium::WebDriver.for(
:remote,
:url => "https://hub-cloud.browserstack.com/wd/hub",
:capabilities => capabilities
)
driver.file_detector = lambda do |args|
str = args.first.to_s
str if File.exist?(str)
end
driver.navigate.to "https://www.fileconvoy.com"
driver.find_element(:id, "upfile_0").send_keys("//local//file//path")
driver.execute_script('document.getElementById("readTermsOfUse").click();')
driver.find_element(:name, "upload_button").submit
sleep(5)
driver.quit
```
Copy icon
Copy
Preload files to the BrowserStack server
When testing on Windows and Linux computers, the Local File Detector and the Send Keys methods do not work as expected in some cases. For such cases, BrowserStack has the following three-step workaround:
This method works for loading media files only, such as audio, image, and video files. You can test the file upload feature by uploading up to 5 files.
(1) Preload your media files to the BrowserStack server using the Upload media file REST API :
```cURL
curl -u "YOUR_USERNAME:YOUR_ACCESS_KEY" -X POST "https://api-cloud.browserstack.com/automate/upload-media" -F "file=@/path/to/app/file/test.jpg"
```
Copy icon
Copy
BrowserStack returns a media_url
(hash ID) of each file that you upload:
```json
{
"media_url": "media://90c7a8h8dc82308108734e9a46c24d8f01de12881"
}
```
Copy icon
Copy
(2) In your test script, specify the media_url
for each file using the uploadMedia
capability for the W3C protocol or the browserstack.uploadMedia
for the JSON wire protocol:
Capability
Description
Values
uploadMedia
Set this capability if you want to use your uploaded images, videos, or audios in the test. Upload your media files to BrowserStack servers using REST API. Use the media_url
value returned as a result of the upload request to set this capability.
The media_url returned on successful upload. Example: ["media://hashedid", "media://hashedid"]
```java
MutableCapabilities capabilities = new MutableCapabilities();
HashMap<String, Object> browserstackOptions = new HashMap<String, Object>();
browserstackOptions.put("uploadMedia", "media://21d66a8a0471097bbf5789330129e9ab97e467e3","media://4d5f6w4h6dq19fg4nl9o5e9ab97d1s1y0i9kl2d3d");
capabilities.setCapability("bstack:options", browserstackOptions);
```
Copy icon
Copy
```javascript
var capabilities = {
'bstack:options' : {
"uploadMedia" : ['media://21d66a8a0471097bbf5789330129e9ab97e467e3','media://4d5f6w4h6dq19fg4nl9o5e9ab97d1s1y0i9kl2d3d']
},
}
```
Copy icon
Copy
```csharp
ChromeOptions capabilities = new ChromeOptions();
Dictionary<string, object> browserstackOptions = new Dictionary<string, object>();
browserstackOptions.Add("uploadMedia", "media://21d66a8a0471097bbf5789330129e9ab97e467e3","media://4d5f6w4h6dq19fg4nl9o5e9ab97d1s1y0i9kl2d3d");
capabilities.AddAdditionalOption("bstack:options", browserstackOptions);
```
Copy icon
Copy
```python
desired_cap = {
'bstack:options' : {
"uploadMedia" : ['media://21d66a8a0471097bbf5789330129e9ab97e467e3','media://4d5f6w4h6dq19fg4nl9o5e9ab97d1s1y0i9kl2d3d'],
},
}
```
Copy icon
Copy
```ruby
capabilities = {
'bstack:options' => {
"uploadMedia" => ['media://21d66a8a0471097bbf5789330129e9ab97e467e3','media://4d5f6w4h6dq19fg4nl9o5e9ab97d1s1y0i9kl2d3d'],
},
}
```
Copy icon
Copy
At the time of the test, each specified file is uploaded from the BrowserStack server to the BrowserStack remote computer or device used for the test. The remote directory to which a file is uploaded depends on the type of file. For example, if it’s an image file, it’s uploaded to /Documents/images
on Mac and to C:\\Users\\hello\\Documents\\images\\
on Windows.
For a list of all directories where files are stored on a BrowserStack remote computer, see Test with preloaded files .
(3) Specify the corresponding fully qualified <MEDIA_DIRECTORY>
location on the remote computer as the argument to the Send Keys method. The specified files on this <MEDIA_DIRECTORY>
location are uploaded during the test to check the file upload functionality.
The following scripts show how to access a media file previously loaded to the BrowserStack server from a BrowserStack remote computer:
Selenium W3C test scripts for testing on desktops
```java
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("bstack:options", new JSONObject()
.put("os", "Windows")
.put("projectName", "Sample Test")
.put("buildName", "Sample_test")
.put("uploadMedia", new JSONArray().put("media://<FILE_HASHED_ID>"))
.put("userName", "YOUR_USERNAME")
.put("accessKey", "YOUR_ACCESS_KEY")
);
capabilities.setCapability("browserName", "Chrome");
capabilities.setCapability("browserVersion", latest);
WebDriver driver = new ChromeDriver(new URL("https://hub-cloud.browserstack.com/wd/hub"), capabilities);
try {
driver.get("https://www.fileconvoy.com");
WebElement uploadElement = driver.findElement(By.id("upfile_0"));
uploadElement.sendKeys("C:\\Users\\hello\\Documents\\<MEDIA_FOLDER>\\<IMAGE_NAME>.<IMAGE_EXT>");
((JavascriptExecutor) driver).executeScript("document.getElementById('readTermsOfUse').click();");
driver.findElement(By.name("upload_button")).submit();
WebElement topMessage = driver.findElement(By.id("TopMessage"));
if (topMessage.getText().contains("successfully uploaded")) {
((JavascriptExecutor) driver).executeScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed","reason": "File upload successful"}}');
} else {
((JavascriptExecutor) driver).executeScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed","reason": "File upload failed"}}');
}
} catch (Exception e) {
((JavascriptExecutor) driver).executeScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed","reason": "Something wrong with script"}}');
} finally {
driver.quit();
}
```
Copy icon
Copy
```javascript
const {Builder, By, Key, until} = require('selenium-webdriver');
let capabilities = {
'bstack:options': {
'os': 'Windows',
'projectName': 'Sample Test',
'buildName': 'Sample_test',
'uploadMedia': ['media://<FILE_HASHED_ID>']
'userName' : 'YOUR_USERNAME',
'accessKey' : 'YOUR_ACCESS_KEY',
},
'browserName': 'Chrome',
'browserVersion': latest,
};
let driver = new Builder().usingServer('https://hub-cloud.browserstack.com/wd/hub').withCapabilities(capabilities).build();
try {
driver.get('https://www.fileconvoy.com');
driver.findElement(By.id('upfile_0')).sendKeys('C:\\Users\\hello\\Documents\\<MEDIA_FOLDER>\\<IMAGE_NAME>.<IMAGE_EXT>');
driver.executeScript('document.getElementById("readTermsOfUse").click();');
driver.findElement(By.name('upload_button')).submit();
if (driver.findElement(By.id('TopMessage')).getText().includes('successfully uploaded')) {
driver.executeScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed","reason": "File upload successful"}}');
} else {
driver.executeScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed","reason": "File upload failed"}}');
}
} catch (exception) {
driver.executeScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed","reason": "Something wrong with script"}}');
} finally {
driver.quit();
}
```
Copy icon
Copy
```csharp
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using Newtonsoft.Json;
using System;
var capabilities = new DesiredCapabilities();
capabilities.SetCapability("bstack:options", JsonConvert.SerializeObject(new
{
os = "Windows",
projectName = "Sample Test",
buildName = "Sample_test",
uploadMedia = new[] { "media://<FILE_HASHED_ID>" },
userName = "YOUR_USERNAME",
accessKey = "YOUR_ACCESS_KEY",
}));
capabilities.SetCapability("browserName", "Chrome");
capabilities.SetCapability("browserVersion", latest);
IWebDriver driver = new RemoteWebDriver(new Uri("https://hub-cloud.browserstack.com/wd/hub"), capabilities);
try
{
driver.Navigate().GoToUrl("https://www.fileconvoy.com");
IWebElement uploadElement = driver.FindElement(By.Id("upfile_0"));
uploadElement.SendKeys("C:\\Users\\hello\\Documents\\<MEDIA_FOLDER>\\<IMAGE_NAME>.<IMAGE_EXT>");
IJavaScriptExecutor jse = (IJavaScriptExecutor)driver;
jse.ExecuteScript("document.getElementById('readTermsOfUse').click();");
driver.FindElement(By.Name("upload_button")).Submit();
IWebElement topMessage = driver.FindElement(By.Id("TopMessage"));
if (topMessage.Text.Contains("successfully uploaded"))
{
jse.ExecuteScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed","reason": "File upload successful"}}');
}
else
{
jse.ExecuteScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed","reason": "File upload failed"}}');
}
}
catch (Exception e)
{
IJavaScriptExecutor jse = (IJavaScriptExecutor)driver;
jse.ExecuteScript('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed","reason": "Something wrong with script"}}');
}
finally
{
driver.Quit();
}
```
Copy icon
Copy
```python
from selenium import webdriver
capabilities = {
'bstack:options': {
'os': 'Windows',
'projectName': 'Sample Test',
'buildName': 'Sample_test',
'uploadMedia': ['media://<FILE_HASHED_ID>']
'userName' : 'YOUR_USERNAME',
'accessKey' : 'YOUR_ACCESS_KEY',
},
'browserName': 'Chrome',
'browserVersion': latest,
}
driver = webdriver.Remote(
command_executor='https://hub-cloud.browserstack.com/wd/hub',
desired_capabilities=capabilities
)
try:
driver.get("https://www.fileconvoy.com")
driver.find_element_by_id("upfile_0").send_keys("C:\\Users\\hello\\Documents\\<MEDIA_FOLDER>\\<IMAGE_NAME>.<IMAGE_EXT>")
driver.execute_script("document.getElementById('readTermsOfUse').click();")
driver.find_element_by_name("upload_button").submit()
if "successfully uploaded" in driver.find_element_by_id("TopMessage").text:
driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed","reason": "File upload successful"}}')
else:
driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed","reason": "File upload failed"}}')
except:
driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed","reason": "Something wrong with script"}}')
finally:
driver.quit()
```
Copy icon
Copy
```ruby
require 'rubygems'
require 'selenium-webdriver'
# Input capabilities
capabilities = {
'bstack:options' => {
"os" => "Windows",
"projectName" => "Sample Test",
"buildName" => "Sample_test",
"uploadMedia" => ["media://<FILE_HASHED_ID>"]
"userName" => "YOUR_USERNAME",
"accessKey" => "YOUR_ACCESS_KEY",
},
"browserName" => "Chrome",
"browserVersion" => latest,
}
begin
driver = Selenium::WebDriver.for(
:remote,
:url => "https://hub-cloud.browserstack.com/wd/hub",
:capabilities => capabilities
)
driver.navigate.to "https://www.fileconvoy.com"
driver.find_element(:id, "upfile_0").send_keys("C:\\Users\\hello\\Documents\\<MEDIA_FOLDER>\\<IMAGE_NAME>.<IMAGE_EXT>") #File path in remote machine
# MEDIA_FOLDER will depend on the file type of the uploadMedia you pass, values can be "video", "images", "audio".
driver.execute_script('document.getElementById("readTermsOfUse").click();')
driver.find_element(:name, "upload_button").submit
if driver.find_element(:id, 'TopMessage').text.include? 'successfully uploaded'
driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"passed","reason": "File upload successful"}}')
else
driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed","reason": "File upload failed"}}')
end
rescue => exception
driver.execute_script('browserstack_executor: {"action": "setSessionStatus", "arguments": {"status":"failed","reason": "Something wrong with script"}}')
ensure
driver.quit
end
```
Copy icon
Copy
The table below lists different types of media and non-media files that you can preload to the BrowserStack server:
OS
Max size
Allowed type
Linux
15MB
.png
, .jpg
, .jpeg
, .gif
, .bmp
, .bmpf
Windows - Any supported browser
15MB
.png
, .jpg
, .jpeg
, .gif
, .svg
, .webp
, .avif
OS
Max size
Allowed type
Windows - Any supported browser
50MB
.mp4
, .webm
, .3gp
, .qt
, .qtff
, .ogg
Linux
50MB
.mp4
, .mkv
, .3gp
, .3gpp
OS
Max size
Allowed type
Windows
15MB
.aac
, .mp3
, .wav
Linux
15MB
.mp3
, .wav
OS
Max size
Allowed type
Linux
15MB
.zip
, .xlsx
, .xls
, .pdf
, .pnp
, .csv
, .html
, .txt
, .ppt
, .doc
, .docx
, .tar
, .rar
Windows
15MB
.zip
, .xlsx
, .xls
, .pdf
, .pnp
, .csv
, .html
, .txt
, .ppt
, .doc
, .docx
, .tar
, .rar