0
0
Selenium Javatesting~10 mins

File download handling in Selenium Java - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test automates downloading a file from a web page and verifies that the file is successfully saved in the expected folder.

Test Code - Selenium
Selenium Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.io.File;
import java.time.Duration;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class FileDownloadTest {
    public static void main(String[] args) throws InterruptedException {
        String downloadFilepath = System.getProperty("user.dir") + "/downloads";
        File downloadDir = new File(downloadFilepath);
        if (!downloadDir.exists()) {
            downloadDir.mkdir();
        }

        ChromeOptions options = new ChromeOptions();
        options.addArguments("--headless=new");
        options.addArguments("--disable-gpu");
        options.addArguments("--window-size=1920,1080");
        options.addArguments("--no-sandbox");
        options.addArguments("--disable-dev-shm-usage");
        options.setExperimentalOption("prefs", java.util.Map.of(
            "download.default_directory", downloadFilepath,
            "download.prompt_for_download", false,
            "download.directory_upgrade", true,
            "safebrowsing.enabled", true
        ));

        WebDriver driver = new ChromeDriver(options);
        try {
            driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
            driver.get("https://www.selenium.dev/downloads/");

            // Find the download link for Selenium Server (example file)
            driver.findElement(By.linkText("4.12.0"))
                  .click();

            // Wait for the file to appear in the download folder
            File downloadedFile = new File(downloadFilepath + "/selenium-server-4.12.0.jar");
            int waitTime = 0;
            while (!downloadedFile.exists() && waitTime < 15000) { // wait max 15 seconds
                Thread.sleep(500);
                waitTime += 500;
            }

            assertTrue(downloadedFile.exists(), "Downloaded file should exist");
        } finally {
            driver.quit();
        }
    }
}
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Test starts and ChromeDriver is initialized with download preferences set to a custom folderBrowser is ready with headless mode and download folder configured-PASS
2Browser navigates to https://www.selenium.dev/downloads/Selenium downloads page is loaded-PASS
3Finds the link with text '4.12.0' and clicks it to start file downloadDownload of selenium-server-4.12.0.jar file starts-PASS
4Waits up to 15 seconds for the file selenium-server-4.12.0.jar to appear in the download folderFile appears in the download folderCheck if downloaded file exists in the folderPASS
5Test ends and browser closesBrowser closed, test complete-PASS
Failure Scenario
Failing Condition: The file does not download within 15 seconds or is saved to a different location
Execution Trace Quiz - 3 Questions
Test your understanding
What is the purpose of setting 'download.default_directory' in ChromeOptions?
ATo disable file downloads
BTo open downloads in a new tab
CTo specify where downloaded files are saved
DTo clear browser cache before download
Key Result
Always configure the browser's download directory explicitly in automated tests to reliably verify file downloads without manual intervention.