Latest post from this blog

Test Scenarios vs. Test Cases: Understanding the Basics

What Are Test Scenarios Test scenarios represent high-level ideas or conditions that need to be validated to ensure the application works as expected. They provide a broader perspective and are typically used during the test planning phase. Purpose: To capture the "what to test" without going into granular details. Example of a Test Scenario: Verify that a user can successfully log in to the application using valid credentials. Verify the behavior of the login page when invalid credentials are entered. Verify the application behavior when the login button is clicked without entering any credentials. What Are Test Cases Test cases are detailed documents that define the specific steps to execute a test. They cover inputs, execution steps, expected results, and actual outcomes. Purpose: To guide the tester step-by-step on "how to test." Example of a Test Case (for the first scenario): Test Case ID TC_01_Login_Valid_Credentials Test Scenario Verify user login with val...

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

Common Exception in Selenium

Test Scenarios vs. Test Cases: Understanding the Basics

Handling Shadow DOM in Selenium WebDriver

Handling HTTPS Websites and SSL Certificate Errors in Selenium

Normalize-space Function In Xpath