File Upload & Download
Practice file upload, file selection, download links and Selenium file handling techniques.
What You Will Practice
- Select a file using a file input.
- Upload a file using Selenium.
- Validate the selected file name.
- Validate file size.
- Practice download links.
- Understand download verification.
- Handle file paths in Selenium.
- Understand upload and download limitations.
File Upload
Select a file from your computer and validate the selected file.
WebElement upload = driver.findElement(By.id("fileInput"));
upload.sendKeys("C:\\Users\\Student\\Documents\\sample.pdf");
File Validation
Validate the selected file name, extension and size.
String fileName = upload.getAttribute("value");
File Download
Use the download link to practice browser download automation.
driver.findElement(By.id("downloadLink")).click();
Download Verification
Understand how automation engineers verify that a downloaded file exists.
nextgen-selenium-practice.txt
File file = new File(downloadPath);
Assert.assertTrue(file.exists());
File Path Practice
Understand absolute and relative file paths used during automation.
C:\Users\Student\Downloads\file.pdf
test-data/sample.pdf
String filePath = System.getProperty("user.dir") + "\\test-data\\sample.pdf";
Selenium File Handling
driver.findElement(
By.id("fileInput")
).sendKeys(
filePath
);
driver.findElement(
By.id("downloadLink")
).click();
File file =
new File(downloadPath);
Assert.assertTrue(
file.exists()
);
String path =
System.getProperty(
"user.dir"
);