File handling is essential in test automation when dealing with web applications that allow users to upload or download files. While Selenium WebDriver doesn't provide direct support for file uploads or downloads, we can achieve this using different approaches.
File Upload in Selenium
There are multiple ways to upload files in Selenium, depending on the type of file upload element on the web page.
1. Using sendKeys() (Preferred Method)
If the file upload field is an <input type="file">
, we can use sendKeys()
to pass the file path directly.
WebElement uploadElement = driver.findElement(By.id("fileUpload"));
uploadElement.sendKeys("C:\\Users\\User\\Documents\\testfile.txt");
This method is simple and effective, but it only works for standard file input elements.
2. Using Robot Class (For Non-Standard Uploads)
When the upload button triggers a file dialog that doesnβt allow direct interaction, we use the Robot class.
Robot robot = new Robot();
StringSelection filePath = new StringSelection("C:\\Users\\User\\Documents\\testfile.txt");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(filePath, null);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
This method works well for pop-up dialogs but depends on system-specific interactions.
3. Using AutoIT (For Windows Applications)
For applications with complex upload mechanisms, AutoIT scripts can be used.
Runtime.getRuntime().exec("C:\\path\\to\\fileupload.exe");
This approach is Windows-dependent and requires a separate AutoIT script.
File Download in Selenium
By default, browsers prompt users for a download location, which can interrupt automation. We can configure browser settings to handle downloads automatically.
1. Automating Downloads in Chrome
We set browser preferences to download files without showing a popup.
HashMap<String, Object> chromePrefs = new HashMap<>();
chromePrefs.put("download.default_directory", "C:\\Downloads");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
WebDriver driver = new ChromeDriver(options);
This ensures files are saved in the specified directory without manual intervention.
2. Automating Downloads in Firefox
Similar to Chrome, we configure Firefox to save files automatically.
FirefoxOptions options = new FirefoxOptions();
options.addPreference("browser.download.dir", "C:\\Downloads");
options.addPreference("browser.download.folderList", 2);
options.addPreference("browser.helperApps.neverAsk.saveToDisk", "application/pdf");
This setup helps in downloading files without interruption.
3. Verifying File Download
Once the file is downloaded, we can verify its existence using Java.
File file = new File("C:\\Downloads\\sample.pdf");
if (file.exists()) {
System.out.println("File downloaded successfully!");
}
This ensures that the file download process is validated in automation tests.
Conclusion
- Use
sendKeys()
for standard file uploads. - Use
Robot Class
or AutoIT for handling OS file dialogs. - Set browser preferences for seamless file downloads.
- Verify downloaded files using Java.
This approach ensures effective handling of file uploads and downloads in Selenium automation.