Creating Object Repository In Selenium WebDriver Using Properties File As An External Source
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:
- For any modification in locator, you must need to touch the code.
- 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:
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.
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 .
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.
6 thoughts on “Creating Object Repository In Selenium WebDriver Using Properties File As An External Source”
Hi, Why when i try using this solution, i use like:
WebElement txtEmail = driver.findElement(prop.getLocator(“EmailAddress”));
i got like:
Unhandled exception type Exception
even i declare : PropertiesUtils prop = new PropertiesUtils(“./locator.properties”);
What i am doing wrong?
You need to put statements in try catch block or add throws statement.
Can you please help me with the same example in C#?
Thanks in advance
Actually I have no much idea of C# programming knowledge. I will try to find out.
I want to learn Object repository as XML file , do u have any blog or reference for same?
Thanks in Advance
Object repository using xml is very old practice and not recommended.