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...

Using Properties Files in Selenium for Test Automation

Introduction
In software testing, managing configuration settings is vital for reliable and maintainable automation scripts. Properties files offer a simple way to store key-value pairs of configuration data separately from the code, enhancing flexibility and ease of management in test automation.

Using properties files in test automation offers several benefits:
Decoupling Configuration from Code: Properties files allow easy updates to configuration data without changing the test code, minimizing errors.

Ease of Maintenance: Testers can quickly modify the properties file for updates, saving time compared to changing multiple scripts.

Enhanced Reusability: A single properties file can be reused across different test scripts, promoting cleaner code and easier testing.

In this tutorial, we will cover the following topics:
  • How to create a properties file and add key-value pairs for configuration settings.
  • How to read data from the properties file using Java’s built-in Properties class.
  • Integrating the properties file into Selenium scripts for dynamic configuration management.

1. How to create a Properties file and add data

 1: Set Up Project Structure
        Use Eclipse/IntelliJ.
        Locate src/main/resources or src/test/resources.

Step 2: Create the Properties File
        Right-click on the folder(Locate src/main/resources or src/test/resources), select New > File, and name it global.properties.

Step 3: Add Key-Value Pairs
You can store the data as key-value pairs by double-clicking the .properties file and entering the key-value data. For example, if you want to store the Blogger URL, include it in the properties file.





2. How to read data from the Properties file:
To read data from the Properties file, we need to use the built-in Properties class that is available in Java.util.package.

So need to create the object of the properties class
Properties obj = new Properties();

we also need to create an object of the FileInputStream class with its path pointing to the .properties file
FileInputStream objfile = new FileInputStream(System.getProperty("user.dir")+"\\<name of the properties file>");

Example Code:
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class DataReader {
public static void main(String[] args) {
Properties properties = new Properties();

try {
//path of the your .properties file name
FileInputStream input = new FileInputStream("global.properties");
properties.load(input);

String url = properties.getProperty("url");
String browserType = properties.getProperty("browser");
String userName = properties.getProperty("username");
String Password = properties.getProperty("password");

System.out.println("URL: " + url);
System.out.println("browser: " + browserType);
System.out.println("username: " + userName);
System.out.println("password: " + Password);

} catch (IOException e) {
e.printStackTrace();
}
}
  • FileInputStream class reads the content of the file as a stream of bytes.

  • The properties class is used to get property value based on the property key.

  • pro. load(fis) will read data from the properties file.

3. Using the Properties file in Selenium Script:

Step 1: Import Selenium WebDriver Classes
        Example imports for Chrome and Firefox drivers.

Step 2: Set Up WebDriver Based on Properties
        Conditional logic to set up the browser.

Step 3: Launch Browser and Perform Actions
        Navigate to the URL.
        Locate elements using WebDriver and perform actions.
        Close the driver.

Example Code
package utils;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;

public class Test {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = null;
Properties properties = new Properties();

try {
//path_to_your_properties_file/golbal.properties
FileInputStream file = new FileInputStream("golbal.properties");
//Load the properties file
properties.load(file);

//Read the values from the properties file
String url = properties.getProperty("url");
String browser = properties.getProperty("browser");
boolean headless = Boolean.parseBoolean(properties.getProperty("headless"));
String username = properties.getProperty("username");
String password = properties.getProperty("password");

//Set up the browser based on the properties
if (browser.equalsIgnoreCase("chrome")) {
System.setProperty("webdriver.chrome.driver", "path_to_chromedriver");

ChromeOptions options = new ChromeOptions();
if (headless) {
options.addArguments("--headless");
}
driver = new ChromeDriver(options);

} else if (browser.equalsIgnoreCase("firefox")) {
System.setProperty("webdriver.gecko.driver", "path_to_geckodriver");

FirefoxOptions options = new FirefoxOptions();
if (headless) {
options.addArguments("--headless");
}
driver = new FirefoxDriver(options);
}
//Use the URL from the properties file
driver.get(url);
System.out.println("Opened URL: " + driver.getCurrentUrl());
WebElement emailField = driver.findElement(By.id("identifierId"));
emailField.sendKeys(username);
WebElement nextButton = driver.findElement(By.id("identifierNext"));
nextButton.click();
Thread.sleep(2000);
WebElement passwordField = driver.findElement(By.name("password"));
passwordField.sendKeys(password);
WebElement passwordNextButton = driver.findElement(By.id("passwordNext"));
passwordNextButton.click();

} catch (IOException e) {
e.printStackTrace();
} finally {
if (driver != null) {
driver.quit();
}
}
}
}

Conclusion
Recap the benefits of using properties files for configuration management in Selenium.
Encourage readers to implement properties files in their automation projects for better maintainability and flexibility.

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