File Upload functionality testing in Selenium 3.0

File Upload functionality testing in Selenium 3.0

In this tutorial, we are going to learn about file upload functionality testing in Selenium 3.0. 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.

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(By.name("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(By.id(“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

element.sendKeys(“E:\\FileUploadtest.txt”);

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”);
}
else
{
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;
import org.openqa.selenium.chrome.ChromeDriver;

public class FileUpload {

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

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

WebDriver driver;
driver = new ChromeDriver();

try
{
driver.get(“http://techcanvass.com/Examples/fileupload-example.html”);
driver.manage().window().maximize();
WebElement element= driver.findElement(By.id(“files”));

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

element.sendKeys(“E:\\FileUploadtest.txt”);
//driver.findElement(By.id(“btn_submit”)).click();

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

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

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

//Quitting the web driver
driver.close();
driver.quit();
}

}

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.

 

About Techcanvass

Techcanvass offers IT certification courses for professionals. We are an IIBA endorsed education provider (EEP), iSQI ATP (for Certified Agile Business Analyst Training) as well as Agile Testing alliance partner for CP-SAT certification training in Selenium.

Our Selenium 3.0 Training programs along with DevOps courses are designed for Manual testing professionals with basics of Java and Python covered.

Selenium 3.0 Training with Java

Selenium Training Course

Selenium 3.0 Training with Python

python selenium training

Selenium Certification Training (CP-SAT)

Selenium Certification Course

DevOps Certification Training

DevOps Certification Training

Leave a Reply

Your email address will not be published. Required fields are marked *