File Upload In Selenium 3.0

How to test file upload function in Selenium

File Upload functionality testing in Selenium 3.0

In this tutorial, we are going to learn How to test file upload function in Selenium. This functionality is part of many screens in a software application. We have to write scripts to automate the testing of File upload as well.

This tutorial is chapter 22 of complete Selenium 3.0 Tutorials for beginners, being developed by Techcanvass. These tutorials are categorized into Java, Selenium, TestNG, Automation Framework and Grid chapters.

Note: Techcanvass Selenium blog is listed as one of the top Selenium blogs by Feedspot. 

File Upload Functionality

  • In many of the test scenarios, we need to upload file to test whether file upload feature is working or not.
  • There are different file types with their respective file formats, such as image file types with .png,.jpg etc. extensions.
  • At the time of writing automated script on file upload we must interact with the web element as well as windows file upload dialog.

File Upload In Selenium 3.0

So, how do we handle the testing of File upload functionality.

Using SendKeys Method

We use sendKeys() method to test the file upload functionality. This method simulates the key strokes.

driver.findElement("FName")).sendkeys("Ramesh Chandra");

sendKeys() method works on every field. In the above example, we have a text field namely “FName”. Using the above method, we type “Ramesh Chandra” in that field.

However, we need to keep the following things in mind:

  • This method does not work till the element is not visible (or gets loaded)
  • We can’t use the “Browse…” button of the “File Upload” element, as clicking on the “Browse…” button opens a dialog box. There is no mechanism to select the file from that dialog box.

In this example, we are going to use the File upload Example HTML page. We will upload a file and will verify the name of the file.

The File upload element is marked in the diagram below:

File Upload

First of all, we will locate the element:

WebElement element= driver.findElement(“files”));

Next, we will select the file, which we would like to update from our file system:

///The file path which you would like to upload, use accordingly


Once the file is selected, the name of the file is printed in an element named “List” as shown below:

File Uploaded

We can verify the name of the uploaded file by checking the “List” element. The code will be as follows:

if (s.equalsIgnoreCase(“FileUploadtest.txt”))
System.out.println(“Test Case passed”);
System.out.println(“Test Case Failed”);

We can also check if the file is uploaded in the server if it has a file namely the uploaded file (code not in this article)

Below is the complete code:

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class FileUpload {

public static void main(String[] args) throws InterruptedException {

System.setProperty(“”, “E:\\Selenium 3\\lib\\chromedriver.exe”);

WebDriver driver;
driver = new ChromeDriver();

WebElement element= driver.findElement(“files”));

///The file path which you would like to upload, change it


driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

String s = driver.findElement(“list”)).getText();
if (s.equalsIgnoreCase(“FileUploadtest2.txt”))
System.out.println(“Test Case passed”);
System.out.println(“Test Case Failed”);

catch (Exception e)
System.out.println(“Some Error occurred, try after some time”);

//Quitting the web driver


Using Robot Class

The Robot class is an efficient and easy way of simulating input/click events. We use this class to write automation script for testing file upload functionality. This class is part of Java AWT package. So, we need to import this in our program.

We will write another article on testing file upload functionality using Robot class.

Do let us know in case you are facing any issues in running the code.

What’s Next?

In this tutorial, we explained how to test file upload function in Selenium. You can go and read more Selenium tutorials published on this blog.

Are you looking to become an Automation tester?

Techcanvass offers Automation Testing and Business Analysis certification courses. We are an IIBA endorsed education provider (EEP), iSQI Germany Authorized Training as well as Agile Testing alliance partner for CP-SAT certification training in Selenium.

One of the key features of our courses is the Job readiness package, as shown below:

Selenium Certification training
CP-SAT Certification Training