Latest post from this blog

How to Manage Test Execution Across Different Browsers and Environments (QA, UAT, Staging)

In real-time automation projects, test execution is never limited to a single browser or a single environment . Applications must be validated across multiple browsers (Chrome, Firefox, Edge) and multiple environments such as QA, UAT, and Staging before going live. A well-designed Selenium + Java + Cucumber automation framework should allow testers to switch browsers and environments easily without changing test scripts . This blog explains how to manage test execution efficiently across different browsers and environments using best practices followed in real projects. Why Multi-Browser and Multi-Environment Testing Is Important Different users use different browsers QA, UAT, and Staging environments have different configurations Bugs may appear only in specific environments or browsers Same test cases must be validated everywhere before production release Common Challenges Testers Face Hardcoded browser names and URLs Maintaining separate test scripts for each environment Browse...

Download a File, Import/Upload It to the Application, and Handle Existing Files in Selenium

Introduction 
When using Selenium to automate web applications, downloading files is common. It's essential to manage existing files in the download folder to avoid issues. In this post, we’ll show you how to download a file with Selenium and Cucumber in Java, check for existing files, and delete them before downloading a new one.

Steps to Download a File and Upload It to the Application 
✓Set Up WebDriver
✓Check for Existing Files
✓Delete Old Files
✓Initiate File Download
✓Wait for the Download to Complete
✓Import/Upload the Downloaded File
✓Verify the Upload

Example 
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import java.io.File;

public class FileDownloadExample {

private static final String DOWNLOAD_DIR = "/path/to/download/folder"; // Specify your download path
private static final String FILE_NAME = "your_file_name.extension"; // Specify the expected file name

public static void main(String[] args) {
WebDriver driver = WebDriverSetup.setupDriver(); // Assuming you have a WebDriver setup method
driver.get("https://Google.com"); // Change to your application's URL

// Check for existing files and delete them
deleteExistingFiles();

// Initiate file download
WebElement downloadButton = driver.findElement(By.id("downloadButton")); // Change the locator as needed
downloadBtn.click();

// Wait for the file to download
waitForFileToDownload();

// Optionally: Import the downloaded file
importDownloadedFile(driver);

// Close the driver
driver.quit();
}

private static void deleteExistingFiles() {
File downloadFolder = new File(DOWNLOAD_DIR);
if (downloadFolder.exists() && downloadFolder.isDirectory()) {
File[] existingFiles = downloadFolder.listFiles();
if (existingFiles != null) {
for (File file : existingFiles) {
if (file.getName().equals(FILE_NAME)) {
boolean deleted = file.delete();
if (deleted) {
System.out.println("Deleted existing file: " + file.getName());
} else {
System.out.println("Failed to delete file: " + file.getName());
}
}
}
}
}
}

private static void waitForFileToDownload() {
File file = new File(DOWNLOAD_DIR + "/" + FILE_NAME);
while (!file.exists()) {
try {
Thread.sleep(1000); // Wait for 1 second before checking again
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

private static void importDownloadedFile(WebDriver driver) {
WebElement importButton = driver.findElement(By.id("importButton")); // Change the locator as needed
importButton.click();

WebElement fileInput = driver.findElement(By.id("fileInput")); // Change the locator as needed
fileInput.sendKeys(DOWNLOAD_DIR + "/" + FILE_NAME); // Full path to the downloaded file
}
}

Conclusion
In conclusion, managing file downloads and imports is key to smooth automation. By deleting existing files before downloading, you minimize errors and ensure your scripts run efficiently.

Comments

Popular posts from this blog

How to Manage Test Execution Across Different Browsers and Environments (QA, UAT, Staging)

Purpose of the StepData Class in Selenium + Java Cucumber Automation Framework

Ensuring Thread Safety in Parallel Test Execution with Selenium, Cucumber, and Java