0
0
Selenium Javatesting~10 mins

File download handling in Selenium Java - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set the download directory in ChromeOptions.

Selenium Java
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<>();
prefs.put("download.default_directory", [1]);
options.setExperimentalOption("prefs", prefs);
Drag options to blanks, or click blank then click option'
A"C:/Downloads"
B"/tmp"
C"C:/Program Files"
D"/usr/local/bin"
Attempts:
3 left
💡 Hint
Common Mistakes
Using an invalid path string or missing quotes.
Setting the preference key incorrectly.
2fill in blank
medium

Complete the code to wait until the file is downloaded by checking the file existence.

Selenium Java
File file = new File("C:/Downloads/report.pdf");
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30));
wait.until(driver -> [1]);
Drag options to blanks, or click blank then click option'
Afile.canRead()
Bfile.isDirectory()
Cfile.isHidden()
Dfile.exists()
Attempts:
3 left
💡 Hint
Common Mistakes
Using isDirectory() which checks if the path is a folder.
Using canRead() or isHidden() which do not confirm file presence.
3fill in blank
hard

Fix the error in the code that tries to delete the downloaded file after test completion.

Selenium Java
File downloadedFile = new File("C:/Downloads/report.pdf");
if ([1]) {
    downloadedFile.delete();
}
Drag options to blanks, or click blank then click option'
AdownloadedFile.isFile()
BdownloadedFile.exists()
CdownloadedFile.canWrite()
DdownloadedFile.isDirectory()
Attempts:
3 left
💡 Hint
Common Mistakes
Using isDirectory() which is false for files.
Using canWrite() which checks permissions but not existence.
4fill in blank
hard

Fill both blanks to configure Firefox profile to auto-download PDF files without prompt.

Selenium Java
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.download.folderList", [1]);
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", [2]);
Drag options to blanks, or click blank then click option'
A2
B"application/pdf"
C1
D"text/plain"
Attempts:
3 left
💡 Hint
Common Mistakes
Using folderList 1 which is default downloads folder.
Setting wrong MIME type that does not match PDF files.
5fill in blank
hard

Fill all three blanks to create a test that clicks a download link, waits for the file, and asserts its existence.

Selenium Java
driver.findElement(By.id([1])).click();
File file = new File([2]);
new WebDriverWait(driver, Duration.ofSeconds(20)).until(d -> file.[3]);
Drag options to blanks, or click blank then click option'
A"downloadLink"
B"C:/Downloads/testfile.zip"
Cexists()
DisFile()
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong element id or file path string.
Waiting on isFile() which may be false if file is not fully ready.