🙂 Telegram Group
🙂 YouTube channel
🙂 LinkedIn
******
🚀 Selenium Tip: Save Downloads to a Specific Folder Using ChromeOptions in Java
In automation, downloading files to a project-specific directory is essential for managing test data efficiently. But usually downloaded file save in the default location and changing it at runtime is a tedious process.
đź§ Do You Know
By customizing ChromeOptions in Selenium, we can direct downloads to any folder within our project, like /resources, instead of the default system downloads folder
Here’s a quick example of how to set the download path using ChromeOptions in Selenium Java:
********
🔺
public class DownloadToSpecificFolder {
public static void main(String[] args) {
// Set up ChromeOptions
ChromeOptions options = new ChromeOptions();
// Define the download folder path
String downloadFilepath = System.getProperty("user.dir") + "/resources";
// Set download preferences
Map<String, Object> prefs = new HashMap<>();
prefs.put("download.default_directory", downloadFilepath);
prefs.put("download.prompt_for_download", false); // Disable download prompt
prefs.put("safebrowsing.enabled", true); // Enable safe browsing
options.setExperimentalOption("prefs", prefs);
// Set up the WebDriver
WebDriver driver = new ChromeDriver(options);
// Navigate to the website
driver.get("website link");
// Add your logic to trigger the download
// e.g., clicking a download link
driver.findElement(By.id("downloadButton")).click();
// Close the browser
driver.quit();
}
}
********
🔍 Key Points:
🔺Custom Download Path: download.default_directory is used to set a specific download folder (/resources in this case).
🔺Disable Prompt: We disable the download prompt with download.prompt_for_download so the file downloads automatically.
🔺Safe Browsing: Ensure safe browsing during download using safebrowsing.enabled.
This approach helps you maintain a clean automation environment and easily access the files you need for further validations.
***
📌Explore my hands-on session with real-time projects covering APIs, Postman, RestAssured, Selenium, Grid, Docker, Jenkins, Maven, and Git! Get personalized guidance, help with questions, mock interviews, and coding sessions: https://lnkd.in/gJ-fQQ-W
*****
👉 Interview Q&A Package to crack interviews for Automation Testing and SDET with similar examples: https://lnkd.in/gJrBGExe
#selenium #qa
Learn more Selenium Tip: Save Downloads to a Specific Folder Using ChromeOptions in Java