Bird
Raised Fist0
Selenium Javatesting~20 mins

File download handling in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
File Download Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Selenium Java code snippet for file download?
Consider the following Selenium Java code that sets up a Firefox profile to automatically download PDF files without showing the download dialog. What will be the value of downloadDir after execution?
Selenium Java
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.dir", "/tmp/downloads");
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");

WebDriver driver = new FirefoxDriver(new FirefoxOptions().setProfile(profile));
String downloadDir = profile.getString("browser.download.dir");
A"/tmp/downloads"
B"C:\\Users\\Downloads"
C"/home/user/Downloads"
D""
Attempts:
2 left
💡 Hint
Look at the preference set for "browser.download.dir" in the Firefox profile.
❓ assertion
intermediate
1:30remaining
Which assertion correctly verifies the downloaded file exists?
You have downloaded a file named "report.pdf" to the directory "/tmp/downloads". Which assertion correctly checks that the file exists using Java's JUnit?
Selenium Java
File downloadedFile = new File("/tmp/downloads/report.pdf");
AassertNull(downloadedFile);
BassertEquals(true, downloadedFile.isDirectory());
CassertFalse(downloadedFile.exists());
DassertTrue(downloadedFile.exists());
Attempts:
2 left
💡 Hint
Check the method that verifies if a file exists on disk.
🔧 Debug
advanced
2:30remaining
Why does this Selenium Java code fail to download the file automatically?
Review the code below. The test expects to download a CSV file automatically, but the browser shows a download dialog instead. What is the main reason?
Selenium Java
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.folderList", 2);
profile.setPreference("browser.download.dir", "/tmp/downloads");
// Missing setting for MIME type

WebDriver driver = new FirefoxDriver(new FirefoxOptions().setProfile(profile));
driver.get("http://example.com/download.csv");
AThe driver.get() method does not trigger downloads.
BThe download directory path is invalid.
CThe MIME type for CSV files is not set in "browser.helperApps.neverAsk.saveToDisk" preference.
DFirefoxProfile is deprecated and cannot be used.
Attempts:
2 left
💡 Hint
Check if the browser knows which file types to download automatically.
❓ framework
advanced
2:00remaining
Which Selenium Java framework feature helps verify file download completion?
In automated tests, which approach best ensures that a file has finished downloading before assertions run?
AUse explicit waits to check the file exists and its size is stable over time.
BUse Thread.sleep() for a fixed time after triggering download.
CCheck only the file name in the download directory immediately after download starts.
DRely on browser logs to confirm download completion.
Attempts:
2 left
💡 Hint
Think about waiting for a condition rather than a fixed delay.
🧠 Conceptual
expert
3:00remaining
What is the best practice for handling file downloads in Selenium tests to avoid flaky tests?
Choose the best practice to handle file downloads in Selenium automated tests to ensure reliability and maintainability.
AManually click the download dialog buttons using Selenium WebDriver actions.
BConfigure browser profile to auto-download files to a dedicated temp folder and verify file presence with explicit waits.
CUse default browser settings and rely on manual verification of downloads.
DDisable downloads and mock file content instead.
Attempts:
2 left
💡 Hint
Think about automation stability and avoiding UI dialogs.

Practice

(1/5)
1. What is the main purpose of handling file downloads in Selenium tests?
easy
A. To verify that files are downloaded correctly without manual intervention
B. To upload files to the web application
C. To change the browser's homepage
D. To clear browser cookies automatically

Solution

  1. Step 1: Understand file download handling

    File download handling in Selenium is used to check if files are saved correctly during automated tests.
  2. Step 2: Identify the correct purpose

    Among the options, only verifying file downloads matches this purpose.
  3. Final Answer:

    To verify that files are downloaded correctly without manual intervention -> Option A
  4. Quick Check:

    File download handling = verify downloads [OK]
Hint: File download handling checks saved files, not uploads [OK]
Common Mistakes:
  • Confusing download handling with file upload
  • Thinking it changes browser settings unrelated to downloads
  • Assuming it clears cookies automatically
2. Which Java code snippet correctly sets Chrome options to automatically download files to a folder without popup?
easy
A. ChromeOptions options = new ChromeOptions(); options.addArguments("--disable-popup-blocking");
B. ChromeOptions options = new ChromeOptions(); options.setHeadless(true);
C. ChromeOptions options = new ChromeOptions(); Map prefs = new HashMap<>(); prefs.put("download.default_directory", "/path/to/download"); options.setExperimentalOption("prefs", prefs);
D. ChromeOptions options = new ChromeOptions(); options.addArguments("--start-maximized");

Solution

  1. Step 1: Identify how to set download folder in ChromeOptions

    Setting the "download.default_directory" preference in ChromeOptions disables popup and sets download path.
  2. Step 2: Match code snippet with correct setting

    ChromeOptions options = new ChromeOptions(); Map prefs = new HashMap<>(); prefs.put("download.default_directory", "/path/to/download"); options.setExperimentalOption("prefs", prefs); correctly creates prefs map and sets it as experimental option for ChromeOptions.
  3. Final Answer:

    ChromeOptions options = new ChromeOptions(); Map prefs = new HashMap<>(); prefs.put("download.default_directory", "/path/to/download"); options.setExperimentalOption("prefs", prefs); -> Option C
  4. Quick Check:

    Set prefs for download folder = ChromeOptions options = new ChromeOptions(); Map prefs = new HashMap<>(); prefs.put("download.default_directory", "/path/to/download"); options.setExperimentalOption("prefs", prefs); [OK]
Hint: Use prefs map with "download.default_directory" for downloads [OK]
Common Mistakes:
  • Using addArguments instead of prefs for download folder
  • Setting headless mode does not affect downloads
  • Forgetting to set experimental options
3. Given the following code snippet, what will be the output if the file is downloaded successfully?
File file = new File("/downloads/report.pdf");
boolean isDownloaded = file.exists();
System.out.println(isDownloaded);
medium
A. false
B. Compilation error
C. FileNotFoundException
D. true

Solution

  1. Step 1: Understand file.exists() method

    The exists() method returns true if the file exists at the given path, false otherwise.
  2. Step 2: Analyze the scenario

    If the file "report.pdf" is downloaded successfully to "/downloads", file.exists() returns true and prints it.
  3. Final Answer:

    true -> Option D
  4. Quick Check:

    file.exists() = true if file present [OK]
Hint: file.exists() returns true if file is present [OK]
Common Mistakes:
  • Assuming file.exists() throws exception if file missing
  • Confusing boolean output with exceptions
  • Thinking it returns file content
4. You wrote this code to check file download but the test always fails. What is the likely issue?
File file = new File("/downloads/report.pdf");
assertTrue(file.exists());
Options:
medium
A. The test does not wait for the download to complete before checking
B. The file path is incorrect syntax in Java
C. assertTrue is not a valid assertion method
D. File class cannot check file existence

Solution

  1. Step 1: Identify timing issue in file existence check

    The test may check file existence before download finishes, causing false failure.
  2. Step 2: Understand need for wait or polling

    Adding wait or retry logic ensures file is fully downloaded before assertion.
  3. Final Answer:

    The test does not wait for the download to complete before checking -> Option A
  4. Quick Check:

    Wait for download before assertTrue(file.exists()) [OK]
Hint: Always wait for download to finish before checking file [OK]
Common Mistakes:
  • Assuming file path syntax is wrong
  • Thinking assertTrue is invalid
  • Believing File class can't check existence
5. You want to verify a file download in Selenium Java tests for multiple file types (.pdf, .csv, .xlsx) saved in the same folder. Which approach best ensures correct file download handling?
hard
A. Check only the file size of the last downloaded file without verifying extension
B. Set browser to download automatically, then check folder for new file with expected extension after triggering download
C. Use Thread.sleep(5000) after download trigger and assume file is saved
D. Manually open the download popup and click save for each file type

Solution

  1. Step 1: Configure browser for automatic downloads

    Setting browser preferences to auto-download files avoids popup interruptions for all file types.
  2. Step 2: Verify download by checking folder for new file with expected extension

    After triggering download, checking the folder for a new file with the correct extension confirms success reliably.
  3. Final Answer:

    Set browser to download automatically, then check folder for new file with expected extension after triggering download -> Option B
  4. Quick Check:

    Auto-download + check file extension = reliable verification [OK]
Hint: Auto-download + check folder for new file extension [OK]
Common Mistakes:
  • Relying on manual popup handling in automated tests
  • Using fixed sleep without checking file presence
  • Ignoring file extension verification
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard