File Upload and Download using Selenium Webdriver

File Upload and Download using Selenium Webdriver

Table of Contents

In this article, we will learn how to handle file uploads and downloads.

Uploading Files

To Upload files, we will use http://the-internet.herokuapp.com/upload as our test application. Through this site any user can upload files without requiring them to sign up.
In Selenium WebDriver, we can upload files by simply using the sendKeys() method on the select file input field to enter the path to the file to upload. Mastering this technique is crucial for your Selenium Certification, as it demonstrates your ability to automate file upload functionality in web applications, a key skill for any Selenium expert.

In real-world automation projects, File Upload scenarios often appear in forms, profile pages, and document management systems. Understanding how Selenium handles File Upload without relying on OS pop-ups helps testers build stable and cross-platform test suites. This approach makes File Upload automation faster, more reliable, and easier to maintain in CI/CD pipelines.

Beyond basic examples, File Upload tests should validate success messages, uploaded file names, and error handling for unsupported formats. When combined with proper waits and assertions, File Upload automation ensures end-to-end coverage of critical user workflows. Practicing File Upload use cases strengthens your confidence in handling complex enterprise applications.

Let’s create a test case in which we will automate the following scenarios to handle File-Upload:

  • Invoke a Google chrome browser.
  • Open URL: http://the-internet.herokuapp.com/upload
  • Click on Choose file to upload a file.
  • Click on Upload button

Now, we will create a test case step by step in order to understand of how to handle file-upload in WebDriver.

Step 1: Launch Eclipse IDE.

Step 2: Go to File > New > Click on Java Project.

Enter Project Name: Demo_FileUpload

Step 3: Right click on the Project Name and click on the New > class.

Give your Class name as “Test_Fileupload” and Select the checkbox “public static void main(String[] args) and click on “Finish” button.

Step 4: Invoke the Google Chrome browser and set the system property to the path of your chromedriver.exe file.

[box type="info" align="" class="" width=""]Here is the sample code to set Chrome driver system property:

// System Property for Chrome Driver   

 System.setProperty("webdriver.chrome.driver", “ D:\\Drivers\\geckodriver.exe ");  [/box]

After that we have to initialize the Chrome driver using ChromeDriver Class. Below is the sample code to initialize Chrome driver using ChromeDriver class.

[box type="info" align="" class="" width=""]// Instantiate a ChromeDriver class.      

WebDriver driver=new ChromeDriver();  [/box]

We will get the below code to launch Google Chrome browser after combining both of the above codes.

[box type="info" align="" class="" width=""]System.setProperty("webdriver.chrome.driver", “ D:\\Drivers\\geckodriver.exe ");  

WebDriver driver=new ChromeDriver();[/box]

After that, we need to navigate to the desired URL.

Below is the sample code to navigate to the desired URL:

[box type="info" align="" class="" width=""]// Launch Website  

driver.navigate().to("http://the-internet.herokuapp.com/upload"); [/box]

Here is the complete code for above scenario:

import org.openqa.selenium.WebDriver;  
import org.openqa.selenium.chrome.ChromeDriver;  
  
public class Test_Fileupload {  
  
    public static void main(String[] args) {  
          
        // System Property for Chrome Driver   
 System.setProperty("webdriver.chrome.driver", “ D:\\Drivers\\geckodriver.exe ");  
  
    String baseUrl = ("http://the-internet.herokuapp.com/upload");  
    WebDriver driver=new ChromeDriver();  
  
     driver.get(baseUrl);
    }    
} 


Share this article

Enroll Free demo class
Enroll IT Courses

Enroll Free demo class

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Join Free Demo Class

Let's have a chat