đź§ŞAutomate File Download and Verification in Selenium with Java Cucumber Framework

Dinesh Y SDinesh Y S
2 min read

Testing file download isn’t just about clicking buttons — it’s about verifying real users outcomes. In this blog, ill walk you through how I implemented file download and verification logic using Selenium 4, integrated neatly into my Java + Cucumber framework.


💡What’s Covered

  • Set custom download path in Chrome

  • Clean up old files before each test

  • Verify downloaded file using file name + timeout

  • Write clean and reusable steps using Page Object Model

  • Log steps with Extent Reports + SLF4J


đź§°Prerequisites

  • Java 8+

  • Selenium 4.13.0+

  • Cucumber + TestNG

  • Maven(for managing dependencies)

  • Extent Reports

  • Page Object Model

  • SLF4J Logging


đź—‚Project Structure (Shortened View)

src/test/java/
├── base/
│   └── DriverFactory.java
├── pages/
│   └── UploadAndDownloadPage.java
├── stepDefs/
│   └── UploadAndDownload.java
├── utils/
│   └── DownloadUtils.java
├── runners/
│   └── ParallelTestRunner.java
└── features/
    └── upload_download.feature

đź§±Step Definitions Snippet

đź”˝ File Download Flow

@When("i click on the download button")
public void i_click_on_the_download_button() {
      uploadAndDownloadPage.scrollToDownloadButton();
      downloadUtils.clearDownloadFolder();
      test.info("Cleared the download folder before downloading the file.");
      uploadAndDownloadPage.clickDownloadButton();
      test.info("Click on the download button.");
}

âś…File Verification Step

@Then("I verify the file is downloaded successfully {string}")
public void verify_the_file_is_downloaded_succssfully(String fileName) {
     boolean is Downloaded = downloadUtils.waitForFileToDownload(fileName,15);
     Assert.assertTrue(isDownloaded, "File was not downloaded successfully!");
     test.info("File downloaded successfully and verified.");
}

🤓 I used System.getProperty(“user.home”) + File.separator + “Downloads” for OS-safe path setup!


⚙️ DownloadUtils.java

public class DownloadUtils {
     private final String downloadPath;

     public DownloadUtils(String downloadPath) {
     this.downloadPath = downloadPath;
     }

     public void clearDownloadFolder() {
          File folder = new File(downloadPath);
          for(File file : folder.listFiles()){
              file.delete();
          }
     }

     public boolean waitForFileToDownload(String filename, int timeoutSecs) {
     File file = new File(downloadPath + File.separator + fileName);
     int waited = 0;
     while (waited < timeoutSecs){
         if (file.exists()) {
             return true;
         }
         try{
             Thread.sleep(1000);
             waited++;
         } catch (InterruptedException e) {
              e.printStackTrace();
           }
     }
      return false;
     }
}

đź§Ş Feature File (UploadAndDownloadFiles.feature)

Feature: File upload and download feature

  Scenario: Download file and verify it's downloaded
     Given I want to navigate to download and upload page
     When I click on the download button
     Then I verify the file is downloaded successfully

đź’ľ GitHub Repo

🔗 👉 Click here for GitHub repo

Includes:

  • Framework setup

  • DownloadUtils class

  • POM structure

  • feature files

0
Subscribe to my newsletter

Read articles from Dinesh Y S directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Dinesh Y S
Dinesh Y S

Automation Engineer | Java + Selenium | Appium + Java | RestAssured | Sharing real-world automation tips