</>

Technology

Selenium

Difficulty

Advanced

Interview Question

How do you verify file download in Selenium automation?

Answer

Verifying File Downloads in Selenium

Selenium cannot interact with OS-level download dialogs directly, but you can control the download folder and verify file presence after triggering the download.

Step 1 — Set Custom Download Directory

Java
String downloadPath = System.getProperty("user.dir") + "/src/test/resources/downloads";

// Create directory if not exists
new File(downloadPath).mkdirs();

Map<String, Object> prefs = new HashMap<>();
prefs.put("download.default_directory", downloadPath);
prefs.put("download.prompt_for_download", false);   // no dialog
prefs.put("download.directory_upgrade", true);
prefs.put("safebrowsing.enabled", true);            // allow downloads

ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", prefs);
WebDriver driver = new ChromeDriver(options);

Step 2 — Trigger the Download

Java
driver.get("https://example.com/reports");
driver.findElement(By.cssSelector(".download-btn")).click();

Step 3 — Wait for File to Appear

Java
public boolean waitForDownload(String downloadDir, String fileExtension, int timeoutSeconds)
        throws InterruptedException {
    File dir = new File(downloadDir);
    long start = System.currentTimeMillis();
    long timeout = timeoutSeconds * 1000L;

    while (System.currentTimeMillis() - start < timeout) {
        File[] files = dir.listFiles((d, name) ->
            name.endsWith(fileExtension) && !name.endsWith(".crdownload") // exclude in-progress
        );
        if (files != null && files.length > 0) return true;
        Thread.sleep(500);
    }
    return false;
}

Step 4 — Verify File Content

Java
@Test
public void testReportDownload() throws Exception {
    String downloadDir = System.getProperty("user.dir") + "/downloads";
    new File(downloadDir).mkdirs();

    // Clean before test
    Arrays.stream(new File(downloadDir).listFiles()).forEach(File::delete);

    // Trigger download
    driver.findElement(By.id("downloadReport")).click();

    // Wait up to 20 seconds
    boolean downloaded = waitForDownload(downloadDir, ".pdf", 20);
    Assert.assertTrue(downloaded, "PDF file was not downloaded");

    // Verify file name
    File[] files = new File(downloadDir).listFiles((d, n) -> n.endsWith(".pdf"));
    Assert.assertTrue(files[0].getName().contains("report"), "Wrong file downloaded");

    // Verify file size > 0
    Assert.assertTrue(files[0].length() > 0, "Downloaded file is empty");
}

Firefox — Set Download Folder

Java
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.dir", downloadPath);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
    "application/pdf,application/octet-stream,text/csv");
profile.setPreference("pdfjs.disabled", true);

FirefoxOptions options = new FirefoxOptions();
options.setProfile(profile);
WebDriver driver = new FirefoxDriver(options);

Headless Chrome Download Fix

Headless Chrome blocks downloads by default. Enable via CDP:

Java
ChromeDriver chromeDriver = (ChromeDriver) driver;
DevTools devTools = chromeDriver.getDevTools();
devTools.createSession();
devTools.send(Browser.setDownloadBehavior(
    Browser.SetDownloadBehaviorBehavior.ALLOW,
    Optional.empty(),
    Optional.of(downloadPath),
    Optional.of(true)
));

Follow AutomateQA

Related Topics