Create Object Repository using Properties file

In the previous post, we have seen how to work with WebTables in selenium. In this post, we will learn how to create Object Repository using Properties file.

This object repository will store all the information about the elements available in our application. The data will be stored in the form of key and value pair. This key can be read in the code to fetch the value associated with it. This key can be any kind of locator like XPATH, CSS, id , name etc and the value will its attribute value like XPATH Expression, CSS expression, id value of the element etc.

You can keep it in any package or desired location as per your need. Let’s see how to use it in selenium.

Create a properties file:

Step#1 We have created a package named as ‘com.resources’. Right click on this package and select a new File as shown in the figure below.

Create Properties

 

Step#2 You can give any name to the file and save it with .properties extension.After that click on Finish button.(refer the below figure for understanding). We have given the name ‘OR.properties‘.

Properties file Saving Selenium

 

Step#3 You can edit this file and provide object details with different locators like Xpath, id etc. We have saved the details of objects(elements) present on our demo site. We have stored the first name, last name, radio buttons, links etc. Refer the below figure to see how to add the objects.

Object repository in selenium

Step#4 Create a class file, where we will write the code to use the properties file. Use the below code to understand the usage of properties file in Selenium.

In this code, we are reading the XPATH for ‘First Name’ and ‘Last Name’  from the properties file. Then we are using, those keys to find elements and enter the data into them.

Output:

//*[@id=’firstname’] //*[@id=’lastname’]

Read properties file in selenium


Advantage of using Properties file as object repository:

If we use properties file, then all the objects will be at common place. Suppose, If there is any change in any of the object property like xpath, id etc. and if that xpath has been hard coded in multiple test cases. Then ,we have to update each and every test case class file.

But if xpath is placed in properties file, then we have to update the changed xpath value only in at one place i.e. properties file. This properties file will be associated as object repository with all multiple test cases.


Ask Question
Have any question or suggestion for us?Please feel free to post in Q&A Forum
Print Friendly, PDF & Email
Working with WebTables in Selenium
Introduction to Apache POI API
Shekhar Sharma

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. He is an automation engineer having more than 8 years of experience who loves troubleshooting in automation and finding innovative ways to solve the problems. His other leisurely activities includes playing harmonica, paintings, sketching portrait, watching movies and travelling etc.

You may also like...

1 Response

  1. Shravan says:

    If property file changes is it require Server Restart.
    Normally it requires Server Restart.

    Please confirm if i uses these one so i need to restart after passing values dynamically.

Leave a Reply

Your email address will not be published. Required fields are marked *