Selenium is the most used software automation testing tool. It provides APIs that can interact with a web page using element locators like ID, CSS, XPath, etc. You can implement an object repository to store their values. However, there is no built-in feature to implement this repo. It is also a very useful way to pass data to your tests. So, we’ll cover how you can create it easily.
Why Use an Object Repository in Selenium?
Managing locators in Selenium can be difficult. If you hard-code them into your test cases, it becomes hard to manage. When a locator changes, you have to update it in every test case. This means tests might not run correctly until the UI is final.
For example, if you use an ID in several test cases and it changes, you must update it everywhere. If you miss one, your test case could fail.
Benefits of Using a Properties File
An object repository can solve these problems:
1. You can use Java’s Properties class to create an object repository.
2. Store locators as key-value pairs in a properties file.
3. Use the same file to keep other project details, like usernames and passwords.
This way, you make your test cases easier to manage and update.
Username_field =name:username Password_field =name:password Login_button =classname:loginbtn
url=http://phptravels.net/login username=user@phptravels.com password=demouser
Let’s now check out how to implement an object repository using the properties file in Selenium.
Firstly, we’ll see one of the simplest methods to create a property file. Just make sure you have Eclipse installed on your system. We’ve tested the example code using the Eclipse IDE.
Implement Object Repository from a Properties File
It can be quickly done using the Eclipse IDE. Open the Eclipse and navigate to File Menu >> New >> Click on the File option.
The only thing you need to do is to set the file name and add the extension as <.properties>. (Example: dataFile.properties).
Now you can open it in the Eclipse using the Property file editor and add/edit any key-value pair. However, it is a simple text file you can modify using basic editors like Notepad.
Read a Properties File
In Selenium projects, the main purpose of the “.properties” files is to store the GUI locators/elements, project configuration data, database configuration, etc.
Each parameter in the properties file appears as a pair of strings, in key-value format, where each key is on one line followed by its value separated by some delimiter. You can refer to the sample properties file from the previous section.
Below is an example program demonstrating how to read the data from the .properties file using Java.
To execute the sample code in Java Eclipse, create a package named ‘seleniumObjectMap’ and add the class file ‘ReadFileData’ to this package. Also, create a folder named <Resources> and add a file named <datafile.properties> in this folder. Please see the attached snapshot below which provides a glimpse of the folder structure used for the sample project.
You must not miss adding Selenium Java jar files as referenced in the project build path. For details on creating a Selenium test project refer to our -> post on “Six Steps to Make a Selenium Project“.
Sample Code to Implement Object Repository
package seleniumObjectMap;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class ReadFileData {
public static void main(String[] args) {
// Define the path to the properties file
final String propFile = System.getProperty("user.dir") + "\\resources\\datafile.properties";
File file = new File(propFile);
FileInputStream fileInput = null;
try {
// Try to open the file
fileInput = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace(); // Print error if file not found
}
Properties prop = new Properties();
try {
// Load the properties from the file
prop.load(fileInput);
} catch (IOException e) {
e.printStackTrace(); // Print error if there’s an issue loading the file
}
// Set up Firefox WebDriver
WebDriver driver = new FirefoxDriver();
driver.get(prop.getProperty("url")); // Open the URL from the properties file
driver.manage().window().maximize(); // Maximize the browser window
driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS); // Wait up to 50 seconds for elements
// Find elements by name and fill them with data from the properties file
driver.findElement(By.name("username")).sendKeys(prop.getProperty("username"));
driver.findElement(By.name("password")).sendKeys(prop.getProperty("password"));
driver.findElement(By.className("loginbtn")).click(); // Click the login button
// If you want, print URL, username, and password to the console
// using sys print out and prop.getProperty()
}
}
We passed the property values to the Webdriver and printed the values at the end. Check the below Output after executing the above program.
url=https://phptravels.net/login
username=user@phptravels.com
password=demouser
Simple Explanation
Here’s a very simple explanation of the code.
Import Statements
These lines of code include important libraries. selenium
helps you control web browsers, while java.io
helps you read files.
Class Definition
The class named ReadFileData
holds the main method. This is where the program starts running.
Main Method
Define File Path
The code sets the location of a file called datafile.properties
. This file has important data. It finds the folder where the program is running with System.getProperty("user.dir")
and adds the file’s name to that path.
Read Properties File
The code tries to open the datafile.properties
file and read what’s inside. If the file is missing or can’t be read, it shows an error message.
Set Up WebDriver
A tool called WebDriver for Firefox is created. This tool lets the program control the Firefox browser.
Open a Web Page
The WebDriver uses a URL from the datafile.properties
file to open a web page.
Perform Actions
The browser is set to full-screen mode and waits up to 50 seconds for the page elements to load. Then, it types the username and password from the file into the login fields and clicks the login button.
Print Info
The code prints the URL, username, and password to the screen. This helps you see what the program is using to log in.
Before You Leave
This tutorial shows how to use Selenium and Java to automate web testing. Selenium controls web browsers, while Java helps read data from files. You store important details like web element IDs in a file called a properties file.
Using this file makes it easier to update your tests if the website changes. The WebDriver opens a web page, waits for things to load, fills in login information, and clicks buttons. This way, your tests stay organized and are easier to fix if needed.
Finally, to keep our site free, we need your support. If you found this tutorial helpful, please share it on Linkedin or Twitter.
Happy testing,
TechBeamers.