End-to-end Testing
How-tos & Guides
5 min read

How to test file uploads in Selenium

Learn how to build automated tests in Selenium for end-to-end scenarios that include file uploads.

Mayela Gonzalez
Published August 8, 2022
Table of contents
AI Assistant for Playwright
Code AI-powered test steps with the free ZeroStep JavaScript library
Learn more

Introduction

Testing workflows that include a file upload step can be quite difficult to automate. Popular testing tools like Selenium lack the ability to test file uploads out of the box, but luckily there are third-party tools that make automatically testing file uploads possible.

In this article, we’ll cover how a third-party tool called AutoIT can be used in combination with Selenium to build end-to-end tests that include file upload functionality.

Testing file uploads

Even for something as seemingly straightforward as uploading a file, there are actually quite a few things you could validate as part of an automated test, such as:

Verifying that the file was uploaded correctly. This “happy path” scenario is what most people are looking to test and what oftentimes will provide the most value when it comes to choosing what to automate.

Test file upload privileges based on user permissions. In some cases, file upload behavior is restricted to certain roles within the application. Testing who has access to upload a file can be part of a large strategy for testing role-based access controls or RBAC for short.

Test that only certain file types can be uploaded. Ideally, in these tests, you’re validating that the file’s extension is checked by your file upload logic and that your system parses the file and rejects files that may have been renamed with an incorrect extension (e.g., .js to .jpg).

Test to be specific to the maximum size file. This type of boundary testing can defend against bugs that threaten the availability of the system since huge files can slow down or even crash a web application.

What is Selenium?

Selenium is a popular tool for automating interactions with a browser and is most commonly used for automated testing scenarios.

Despite its popularity and maturity as a software testing tool, Selenium doesn’t have built-in support for file uploads. Since file uploads involve functionality and data that lives outside of the browser, you could argue that file uploads are technically outside of the scope of a tool like Selenium. However, given how common it is to find file upload behavior in web applications, having some way to support testing this behavior can provide a lot of benefits.

By combining Selenium with a tool called AutoIT, we can create automated tests that test end-to-end workflows that include file uploads.

Using AutoIT

AutoIT is “a freeware BASIC-like scripting language” that fills some of the gaps needed to build a full end-to-end test for a file upload. Like Selenium, AutoIT provides tools for triggering mouse clicks and keyboard events. But unlike Selenium, it is built to drive an underlying Windows OS, rather than the web browser process.

This tutorial will use Python, but you can use Java or any other language supported by Selenium.

In AutoIT, we’ll write a simple script that waits for a file upload dialog, inputs a known file, and submits it. Here’s what that looks like:

1
2
3
4
5
WinWaitActive("File Upload")

Send("C:\Users\a\Desktop\test.txt")

Send("{Enter}")

In order to make this script triggerable by Selenium, we’ll need to compile it and produce an executable. Using the Aut2Exe command, we’ll generate an output file called uploadtest.exe.

For the Selenium test, we’ll want to script the actions leading up to the file upload dialog getting triggered, invoke the AutoIT script we just wrote to complete the file upload process, and optionally add any additional Selenium actions or assertions once the file upload has completed.

Here’s an example Selenium script written in Python that implements these steps:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import os
import subprocess
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

service = Service(executable_path=r'C:/Users/a/Desktop/geckodriver.exe')
driver = webdriver.Firefox(service=service)

driver.get("http://www.csm-testcenter.org/test?do=show&subdo=common&test=file_upload")

driver.implicitly_wait(4.0)
elem = driver.find_element(By.NAME, "file_upload")
elem.send_keys(Keys.RETURN)
current_dir = r"C:\Users\a\Desktop"
subprocess.Popen(os.path.join(current_dir,"uploadtest.exe"))

driver.implicitly_wait(4.0)
elem = driver.find_element(By.NAME, "http_submit")
elem.send_keys(Keys.RETURN)

Note that in some cases, you can use Selenium’s send_keys() method to trigger the file upload. This lets you avoid having to use AutoIT, but you’ll need to test this for yourself on your own application to determine if this approach works:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.common.by import By
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

service = Service(executable_path=r'C:/Users/a/Desktop/geckodriver.exe')
driver = webdriver.Firefox(service=service)

driver.get("http://www.csm-testcenter.org/test?do=show&subdo=common&test=file_upload")
driver.implicitly_wait(4.0)

elem = driver.find_element(By.NAME, "file_upload")
elem.send_keys("C:\Users\a\Desktop\testfile.txt")
elem.send_keys(Keys.RETURN)
elem = driver.find_element(By.NAME, "http_submit")
elem.send_keys(Keys.RETURN)

Although AutoIT augments Selenium to provide robust support for testing file uploads, there are still some major limitations:

Conclusion

Testing file uploads in Selenium is possible through the use of third-party tools like AutoIT. Just be aware of the potential pitfalls of using these technologies to test file uploads, especially when it comes to test flakiness and creating tests that work on local machines and CI servers. Despite its challenges, having an automated approach for testing file uploads can provide test coverage for many critical workflows and thus be very important in your overall regression testing strategy.

Reflect: A testing tool with built-in support for testing file uploads

Reflect is a no-code testing tool that can test virtually any action you can take on a browser, including file uploads. Creating a file upload test in Reflect is easy: the tool records your actions as you interact with the application and automatically turns those actions into a repeatable test. For file upload steps, files are saved securely in the cloud so they can be re-used each time the test runs.

Get started with Reflect today

Create your first test in 2 minutes, no installation or setup required. Accelerate your testing efforts with fast and maintainable test suites without writing a line of code.

Copyright © Reflect Software Inc. All Rights Reserved.