Skip to main content
No Result Found

Test the file upload functionality

Learn how to test the file upload functionality using BrowserStack Automate TurboScale.

The file upload functionality enables you to upload files to a web app. For example, submit documents for processing in various applications, upload videos or images to social media sites, upload cover letters and resumes to career sites, or attach files to emails. The functionality involves looking for a file on your computer and then uploading it.

On Automate TurboScale, you can test the file upload functionality of your web app using:

  • Files downloaded in the same Automate TurboScale session
  • Files from your computer

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.

Copy icon Copy
Copy icon Copy

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
Copy icon Copy
Copy icon Copy
Copy icon Copy
Copy icon Copy
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
Copy icon Copy
Copy icon Copy
Copy icon Copy
Copy icon Copy
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:

Terminal
Copy icon Copy

BrowserStack returns a media_url (hash ID) of each file that you upload:

Terminal
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"]



Code snippets showing how to set uploadMedia (Selenium W3C)

Copy icon Copy
Copy icon Copy
Copy icon Copy
Copy icon Copy
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
Copy icon Copy
Copy icon Copy
Copy icon Copy
Copy icon Copy
Copy icon Copy

Supported file formats

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

BrowserStack allows you to test the Playwright’s file upload functionality using the setInputFiles method of the locator element that enables file transfers from your machine to the BrowserStack remote browser. The locator.setInputFiles(files[, options]) method should always point the first argument to a file type.

Upload files from your machine

The following code example shows how the setInputFiles method is used to upload a file:

'use strict';
const {chromium} = require('playwright');
const {promisify} = require('util');
const sleep = promisify(setTimeout);

(async () => {
  const caps = {
    build: 'Playwright testing',
    browser: "chrome",
    'browserstack.username': process.env.BROWSERSTACK_USERNAME || 'YOUR_USERNAME',
    'browserstack.accessKey': process.env.BROWSERSTACK_ACCESS_KEY || 'YOUR_ACCESS_KEY'
    'browserstack.networkLogs': true,
    'browserstack.console': 'errors',
  };

  const browser = await chromium.connect({
    wsEndpoint: `wss://hub-cloud.browserstack-ats.com/playwright?caps=${encodeURIComponent(JSON.stringify(caps))}`,
  });
  const context = await browser.newContext({ignoreHTTPSErrors: true});
  const page = await context.newPage();

  await page.goto('https://www.fileconvoy.com/');
  const fileChooserPromise = page.waitForEvent('filechooser');
  await page.locator('input[name=\"upfile_0\"]').click();
  const fileChooser = await fileChooserPromise;
  await fileChooser.setFiles('/provide/absolute/path/to/file.name'); // Provide absolute path 
  await page.locator('input[name=\"readTermsOfUse\"]').check();
  await page.locator('input[name=\"upload_button\"]').click();

  await sleep(1*60*1000);
  await browser.close();
})();

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

Is this page helping you?

Yes
No

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!

Talk to an Expert
Download Copy Check Circle