How to Upload a File Using Selenium?
Uploading a file is a common factor for most scenarios. There are many instances, like uploading a resume for your job portal, uploading your identity proof for your passport, etc. When multiple files need to be uploaded and tested at once, it becomes necessary to ensure that there are no errors to automate the test cases. Why? This avoids human intervention as it’s time-consuming.
This is where Automation testing comes into the picture. Selenium is the most popularly used framework to automate the test cases. Through this blog, let’s understand how to upload a file using Selenium.
Table Of Contents
Quick look at Automation Testing
Automation testing is a type of testing that uses programs and scripts to run test cases. It is a software testing technique in which testers leverage automated software tools for executing test cases.
Automating the test suite helps in saving time and effort required in performing repetitive and resource-heavy tasks that are difficult to perform manually.
Introduction to Selenium
Selenium is a set of libraries and tools that automates web browser actions. It automates the actions carried out on web applications. In simple words, Selenium offers the tools and functionalities to interact with the browser to automate the browser actions, like clicking, inputting, navigating, selecting, etc., with the help of scripts.
It is open source and freely available. To download and configure, you can refer to the blog on Selenium Tutorial. It offers a range of features that simplify the implementation of Automating Browser Testing. It helps testers perform tests across various browsers efficiently and seamlessly.
Features of Selenium:
- It is flexible and extensible.
- It widely supports multiple programming languages, operating systems, browsers, and browser versions.
- The current stable version of Selenium is Selenium 4. Know more about this through the help of this blog.
Now that you are aware of Automation testing and Selenium, let’s understand how to upload a file using Selenium.
How to Upload a File Using Selenium
There are various methods to upload a file using Selenium. You can use locators, page object models, etc., to upload a file. In this case, let’s use locators and sendkeys method to upload a file.
Let’s take a scenario where you would like to upload a file to your Google mail account.
Step 1: Login into your Gmail account.
Step 2: Click on Compose button to compose an email.
Step 3: Add a recipient
Step 4: Click the attach button to upload the file as shown in the below screenshot.
These are the manual steps carried out to upload a file.
Now let’s see how Selenium plays a vital role in testing the upload file functionality for a web application.
Selenium into Action
Let’s use locators in Selenium to locate the attachment button. In this case, I will be using XPath locator.
Step 1: Create a driver instance. In this case, I’d use ChromeDriver.
Note: Download the ChromeDriver compatible with your Google Chrome browser version. To configure ChromeDriver, refer to this blog on ChromeDriver for Selenium.
Below command is used to create a Google Chrome browser instance.
Step 2: Navigate to the Gmail account. Using the driver object, we will assign the path to
Step 3: Enter your credentials and login to your Gmail account. In this case, let’s use the id and name locators to locate the username and password. Here I am using Sendkeys method to send the values to the respective email and password field.
Step 4: Now let’s click on the Compose email field. In this case, let’s use XPath locator to locate the Compose button. Here we use the click action to click on the button.
Note: The value for the XPath locator can be fetched by inspecting the specific web element on the web page. In this case, we are using Relative XPath. The value is shown below for the Compose button.
Learn the fundamentals of XPath locator by referring to this cheatsheet.
Step 5: As a next step, we need to upload the file. Let’s write the Relative Xpath locator for attachment button. Here also click action is used to click on the attachment button to attach the file.
Step 6: Let’s use the add file method to add the file.
addFile.sendKeys(“<FILE_PATH>”);// FILE_PATH is the path to the file you want to upload
driver.findElement(By.linkText(“Upload a file”)).click(); // We are assuming that there is a link with text “Upload a file”, that will initiate the file upload.
Step 7: Next step is to verify if the file is uploaded. Refer to the below script to verify. The XPath locator value might differ. So it is better to inspect the uploaded file and then write the relative XPath for the same.
In the above program, we have used TestNG annotations for Before Test, @Test and After test. In the before test method, we are setting up the system properties of the ChromeDriver and launching the WebDriver instance to instantiate the browser. Next, we will navigate to the Gmail account.
As a next step, we will use locators to locate the web elements on the web page and upload a file.
The last step is to verify if the file has been uploaded. In this case, I am using assert function to display true and false. Finally, use driver.quit() command to quit the driver instance.
This is how we need to write the script for uploading a file using Selenium. As demonstrated above, uploading a file with Selenium is a relatively easy process.
But, as it is a user action, it needs to be tested frequently to make sure that the website offers a flawless user experience.
Let’s now understand how Testsigma eases the process of writing such a test case and how it is helpful to make the task of a test engineer easy.
Testsigma to the Rescue
There are already widely used tools and frameworks in the industry, such as Selenium. They also provide vivid features that aid in the performance of Automated testing. To make the best use of these frameworks, a user with a strong understanding of programming is required.
Finding and keeping good resources is a major challenge for most businesses today. Another cost in the recent trends is training resources needed to perform automated testing using these frameworks. This highlights the need for a tool that is highly simplified and does not require a high level of technical knowledge to use. This is where Testsigma comes into the picture.
Features of Testsigma
Testsigma is a cloud-based platform for unified test automation. It simplifies and accelerates automated testing for web applications, mobile applications, and APIs. Even someone with no or limited coding skills can easily learn how to use this platform.
This is basically a codeless platform for you to execute the test cases with ease. Automated test suites can be created quickly and with minimal technical overhead.
How does Testsigma assist you in accomplishing this?
- It employs simple English commands supported by an NLP (Natural Language Processing) engine, allowing the user to create test cases in minutes. Most of the teams with limited coding knowledge can achieve sprint test automation with minimal effort using NLP and integrated API testing capabilities.
- Maintaining multiple servers, operating systems, and mobile devices is no longer a problem with Testsigma. This is because Testsigma offers a cloud version too where all of this available on the cloud.
- End-to-end tests for web, mobile apps, and APIs can be built 5 times faster with English scripts that self-heal, allowing for maintenance-free testing.
- Parallel tests can be run on multiple operating systems, browsers, and browser versions. Testsigma provides access to over 200 Android and iOS operating systems for testing.
- With a few clicks, you can sign up for our Cloud-hosted platform. Testsigma cloud requires no setup and can easily integrate many features supported by the automated testing process. Refer to this documentation on Testsigma for more details.
- Testsigma is not a record and playback tool. It has all the flexibility of an enterprise-class test automation tool/framework, yet the simplest to write automated test scripts.
- Testsigma’s community-powered marketplace provides add-ons for automated actions and data generators that can be accessed instantly or customized to match your use case.
Know the similarities and differences between Selenium and Testsigma with the help of the blog on Selenium vs Testsigma. This blog also helps you understand how Testsigma is a better test automation tool with many advanced features employed.
With this, we come to the end of this article on How to Upload a file in Selenium.
In the modern era, customers have minimal tolerance for a bad user experience. Gone are those days when customers would entertain multiple iterations and changes to the software. Therefore, delivering top notch quality software which meets the users’ expectations and caters to their timelines is highly essential. This basically leads to short release cycles, and the testing approach needs to be effective and with wide coverage of test cases.
With raging competition and ever-changing requirements of the end users, it is necessary that organizations deliver high-quality software at the earliest. Using test automation at the right place at the right time can really help improve the quality while also keeping the testing cycle time to a minimum.
Testsigma is a fully-managed test automation platform that provides enhanced security, a large test lab, review management, and powerful reporting capabilities, among other things. You should consider using Testsigma to ensure a smooth and comprehensive testing process, particularly for automation of your web, mobile, desktop, and API testing.