Selenium Webdriver is the most used software automation testing tool. It provides APIs that operate on the web objects using their element locators like ID, CSS, XPath, etc. It doesn’t have any built-in feature to implement an object repository.
Why Implement Object Repository for Selenium Tests
It is a tough task for any test engineer to maintain ever-changing locators in Selenium. If you hard code them in your test cases that makes it more unmanageable. Also, this approach leads to frequent code changes whenever the locator gets changed. Hence, test automation doesn’t get fixed until the UI gets finalized.
Consider that you are using an ID (which is one of the types of element locators) in multiple test cases, and gets modified. Then, you would need to update its value in all test cases. Unfortunately, if you miss updating any of them, then the test case eventually will fail.
Benefits of Using Properties File
You can overcome all of the challenges mentioned above with the help of an object repository.
1. Can implement the object repository using Java’s Properties class from the Java util package.
2. Can store the element locators as the key-value pair in a property file.
3. Can even use it to hold project configuration properties like usernames/passwords etc.
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 can we implement an object repository using the properties file in Selenium WebDriver.
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 where you can add/edit any key-value pair. Though, it is a simple text file that you can modify using basic file 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 that demonstrates 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.
Make sure that you add Selenium Java Webdriver jar files as referenced in the project build path. For details on how to create 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) { final String propFile = System.getProperty("user.dir") + "\\resources\\datafile.properties"; File file = new File(propFile); FileInputStream fileInput = null; try { fileInput = new FileInputStream(file); } catch (FileNotFoundException e) { e.printStackTrace(); } Properties prop = new Properties(); try { prop.load(fileInput); } catch (IOException e) { e.printStackTrace(); } WebDriver driver = new FirefoxDriver(); driver.get(prop.getProperty("url")); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS); driver.findElement(By.name("username")).sendKeys( prop.getProperty("username")); driver.findElement(By.name("password")).sendKeys( prop.getProperty("password")); driver.findElement(By.className("loginbtn")).click(); System.out.println("URL ::" + prop.getProperty("url")); System.out.println("User name::" + prop.getProperty("username")); System.out.println("Password::" + prop.getProperty("password")); } }
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=http://phptravels.net/login username=user@phptravels.com password=demouser
Before you leave, render us your support to remain free. Share this post on social media (Linkedin/Twitter) if you gained some knowledge from this tutorial.
Happy testing,
TechBeamers.