Creating Object Repository In Selenium WebDriver Using Properties File As An External Source

Hello Folks,

We have seen Page Object Model in previous posts and you must be noticing that we are writing locators within code. It has its own disadvantages as below:

  1. For any modification in locator, you must need to touch the code.
  2. Difficult for manual tester or beginner in automation testing to find out correct place to change locator if needed.

So, we can also read locators from externals sources like:

a. Database
b. Properties file
c. XML file
d. Excel sheet

Database and Properties file are mostly used as external source of locator to create object repository. We will see how to create object repository using properties file.

What is properties file in Java?

A file with extension .properties in java, is called a property file which stores the data in form of key value pair (Like Map). We can store our locators in a properties file and we can read it from properties file and we can use to create object repository.

How to read data from properties file?

Step 1: Create a file with extension .properties as “locator.properties”.

Step 2: Load the properties file using load() method of Properties class.

Step 3: Use getProperty(String key) to read value using Key.

We will see step by step now.

Step 1: Let’s start by creating a properties file.

locator.properties:

We will store the value as:

WebElementName = LocatorType:LocatorValue

It will help in locating web element using desired locator.

Create a class to load properties file:

We will create a class which will have methods for loading properties file and locating web element .

Script class:

That’s it guys. Hope you would have clear concepts now.
If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.
#ThanksForReading
#HappySelenium

6 thoughts on “Creating Object Repository In Selenium WebDriver Using Properties File As An External Source

  1. Hi, Why when i try using this solution, i use like:

    WebElement txtEmail = driver.findElement(prop.getLocator(“EmailAddress”));
    txtEmail.sendKeys(“Amod”);

    i got like:

    Unhandled exception type Exception

    even i declare : PropertiesUtils prop = new PropertiesUtils(“./locator.properties”);

    What i am doing wrong?
    Thanks

Leave a Reply to Amod Mahajan Cancel reply

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