2016-03-31

Selenium Webdriver is the most used software automation testing tool. It provides APIs that operates on the web objects using their element locators like ID, CSS, XPath, etc. It doesn’t have any built-in feature to implement object repository. Lacking this facility increases the efforts of maintaining the objects locators. If you hard code these elements locators in the source code that make it more unmanageable. Also, this approach leads to frequent code changes whenever the locator gets changed. Hence, the test automation doesn’t get fixed until the UI gets finalized.

If you are using an Id (which is one of the types of element locators) in multiple test cases and it 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. You can overcome all of such issues with the help of an object repository. You can implement the object repository using Java’s Properties class which is a part of the Java Util package. You can store the element locators as the key-value pair in a property file. You can even use it to hold project configuration properties like username/passwords etc.

Let’s now check out how can we implement object repository using the properties file in Selenium WebDriver and learns the steps required to implement it. Firstly, we’ll see the one of the simplest methods to create a basic property file.

Just make sure you have Eclipse installed on your system. We’ve tested the example code using the Eclipse IDE.

How to Create a Properties File to Implement Object Repository?

It can be easily done using the Eclipse IDE. Open the Eclipse and navigate to File Menu >> New >> Click on the File option.



Implement Object Repository – Select a file.

The only thing you need to do is to set the file name and add the extension as <.properties>. (Example: dataFile.properties)



Implement Object Repository – Specify File Name

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 which you can modify using the basic file editors like the Notepad.

How to Read Properties File?

In Selenium projects, main purpose of the ‘.properties’ files is to store the GUI locators/elements, project configuration data, and database configuration, etc.

Each parameter in 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 the sample properties file from the previous section.

Below is an example program which demonstrates to read the data from .properties file using Java.

To execute the sample code in Java Eclipse, create a package named ‘seleniumObjectMap’ and add the class file ‘ReadFileData’ in 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 our -> post on this topic.



Implement Object Repository – Reading properties file.

Implement Object Repository – Reading datafile.properties

We’d passed the properties values to the Webdriver and printed the values at the end. Check the below Output after executing the above program.

The post Implement Object Repository Using Properties File In Selenium WebDriver appeared first on TechBeamers.

Show more