0
0
Selenium Javatesting~5 mins

File download handling in Selenium Java

Choose your learning style9 modes available
Introduction

We handle file downloads in tests to check if files are saved correctly when users download them from a website.

Testing if a report downloads correctly after clicking a button.
Verifying that an invoice PDF is saved when requested.
Checking if an image file downloads properly from a gallery.
Ensuring a CSV export feature saves the file as expected.
Syntax
Selenium Java
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.dir", "C:\\Downloads");
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");

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

Set preferences to avoid popup dialogs during download.

Specify the folder path where files will be saved.

Examples
This sets Firefox to download PDF files automatically to the specified folder without asking.
Selenium Java
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.dir", "C:\\Users\\User\\Downloads");
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");
WebDriver driver = new FirefoxDriver(new FirefoxOptions().setProfile(profile));
This configures Chrome to download files automatically to the given folder without popups.
Selenium Java
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<>();
prefs.put("download.default_directory", "C:\\Downloads");
prefs.put("download.prompt_for_download", false);
options.setExperimentalOption("prefs", prefs);
WebDriver driver = new ChromeDriver(options);
Sample Program

This test opens Firefox with settings to download PDF files automatically to C:\Downloads. It navigates to a PDF URL to trigger the download, waits 5 seconds, then prints a confirmation message.

Selenium Java
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;

public class FileDownloadTest {
    public static void main(String[] args) throws InterruptedException {
        FirefoxProfile profile = new FirefoxProfile();
        profile.setPreference("browser.download.folderList", 2);
        profile.setPreference("browser.download.dir", "C:\\Downloads");
        profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");

        FirefoxOptions options = new FirefoxOptions();
        options.setProfile(profile);

        WebDriver driver = new FirefoxDriver(options);

        driver.get("https://example.com/sample.pdf");

        // Wait for download to complete (simple wait for demo)
        Thread.sleep(5000);

        System.out.println("Download started and should be saved in C:\\Downloads");

        driver.quit();
    }
}
OutputSuccess
Important Notes

Always set the correct MIME type in preferences to avoid download prompts.

Use absolute paths for download directories to avoid confusion.

Waiting for download completion can be improved by checking the file system instead of fixed sleep.

Summary

File download handling lets tests check if files save correctly.

Set browser preferences to download files automatically without popups.

Use waits or file checks to confirm downloads finish before assertions.