
Uploading files is a crucial part of many web applications, and automating this process is essential for thorough test coverage. Selenium WebDriver provides several methods to handle file uploads, ranging from directly sending file paths to input elements to using external tools for native dialogs.
This article explores these techniques with practical examples, including real-world scenarios like uploading a GitHub profile picture and verifying uploads on plagiarism checkers, while highlighting the importance of testing on real devices for comprehensive validation.
Selenium WebDriver is a powerful API that enables automation of web browsers by simulating user interactions like clicking, typing, and navigation. It works through browser-specific drivers that act as intermediaries between the test scripts and the browsers, translating commands into browser actions.
With WebDriver, testers can interact with web elements identified using various locator strategies, execute JavaScript, manage cookies, and control browser sessions. Selenium WebDriver supports multiple programming languages and browsers, making it versatile for cross-platform test automation needs.
There are several common methods to upload a file using Selenium WebDriver:
These methods can be combined or chosen based on the complexity of the web application’s file upload functionality and the testing environment requirements.
Here’s how to upload a file using Selenium WebDriver:
Here is an example of how to automate uploading a profile picture on GitHub using Selenium WebDriver:
Sample C# Code Snippet
driver.Navigate().GoToUrl("https://github.com/login");
driver.FindElement(By.Id("login_field")).SendKeys("your_username");
driver.FindElement(By.Id("password")).SendKeys("your_password");
driver.FindElement(By.Name("commit")).Click();
driver.Navigate().GoToUrl("https://github.com/settings/profile");
// Locate hidden file input and upload image
IWebElement uploadInput = driver.FindElement(By.Id("avatar-upload-input"));
uploadInput.SendKeys(@"C:\path\to\profile_picture.jpg");
// Click save button
driver.FindElement(By.CssSelector("button.js-update-avatar-button")).Click();
// Add waits/assertions here to verify successful upload
This example demonstrates the key steps to automate profile picture upload on GitHub, showcasing the file input interaction and form submission using Selenium with C#.
A practical real-world use case of file upload automation with Selenium is in plagiarism detection platforms. For instance, when users upload documents to check for copied content, Selenium can automate this by locating the file input element on the plagiarism checker’s website and sending the file path through the sendKeys() method.
This enables testers to validate the upload functionality, verify the processing of the document, and ensure that the plagiarism report is generated correctly. Automating such workflows improves reliability and efficiency in testing key features of educational and content verification web applications.
Step-by-Step Automation Flow
Sample C# Selenium Code
// Visit the plagiarism checker upload page
driver.Navigate().GoToUrl("https://plagiarismchecker.example.com");
// Locate the file input element
IWebElement fileInput = driver.FindElement(By.Id("document-upload-input"));
// Provide the absolute path to the document
fileInput.SendKeys(@"C:\Users\User\Documents\sample-document.docx");
// Click the button to start the check (if necessary)
driver.FindElement(By.Id("submit-upload")).Click();
// Use an explicit wait to wait for results page or confirmation
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));
IWebElement resultMessage = wait.Until(
d => d.FindElement(By.Id("scan-results"))
);
// Assert the result or display status
Assert.IsTrue(resultMessage.Displayed);
Key Considerations
Running Selenium tests on real devices offers several critical advantages for ensuring application quality, performance, and reliability:
Automating file uploads with Selenium streamlines web testing for real-world scenarios such as plagiarism detection and profile management. By leveraging techniques like sendKeys() on file input elements, testers can efficiently validate key application workflows.
Running these tests on real devices further enhances reliability, uncovering issues related to hardware, browser, and OS variations that emulators often miss. Real-device testing ensures complete coverage, catching device-specific bugs, validating mobile file pickers, and verifying user experience across platforms.
To maximize confidence in your automation efforts, integrating Selenium scripts with cloud-based platforms like BrowserStack Automate is highly recommended.
BrowserStack Automate provides scalable access to thousands of real browsers and devices, allowing you to execute tests in genuine user environments without the hassle of managing physical devices.
This seamless integration improves test accuracy, reduces maintenance overhead, and accelerates feedback for every deployment.
Run Selenium Tests on Cloud
Get visual proof, steps to reproduce and technical logs with one click
Continue reading
Try Bird on your next bug - you’ll love it
“Game changer”
Julie, Head of QA
Try Bird later, from your desktop