But how we can achieve the same in selenium??..
It can be done by using Properties file feature of Java. Lets begin with some basic explanation.
What is Object Repository?
Object Repository is a centralized location where we can store objects information, it acts as interface between Test script and application in order to identify the objects during the execution.
We always recommend using external file for object repository rather than hard coding the objects and its properties directly into our code. Why this??? As it reduces the maintenance effort and provides positive ROI, for example say any of the object properties change within our application under test, we can easily change it in external object repository file, rather than searching and doing updates for that object individually in the code.
A basic object repository can be implemented as a collection of key-value pairs, with the key being a logical name identifying the object and the value containing unique objects properties used to identify the object on a screen. For this, we will use a .properties file in Java which is a basic collection of key-value pairs.
Creating Properties File in Eclipse:-
Right click on the Package in the solution Explorer of Eclipse-> New ->Other
|Adding New File in Eclipse|
Right click on the Package in the solution Explorer of Eclipse-> New ->File (In this case Step-2 below is not required)
|New ->File in Eclipse|
General ->File ->Click Next
|New Wizard in Eclipse|
Give name to the file with .properties extension (Say OR_Gmail_Login.properties) ->Click Finish
|Naming Properties File in Eclipse|
We generally create each properties file for every single page and capture all the UI elements present on the page and use it as per the needs.
Adding Key/value Pairs in Properties File:-
Before adding our objects into object repository, let’s outline a simple scenario that we will be automating in Selenium:-
1. Launch Gmail login page.
2. Fill the Username & Password fields and click on Submit button
After creating our empty properties file, now we need to add our elements, in the form of key and Value pairs.
Gmail.LoginPage.txtPassword -> Key
Note: – In Value property we have taken the Locator and its value, which will be used to identify our control. As password field, is getting uniquely identified by ID field so we have taken that value.
|Properties File in Java|