Last Updated on March 30, 2020 by Scientech Easy
What is Object with respect to Selenium?
An object is a graphics user element in a web application. For example, a button, edit box, image, link, etc. An object basically consists of Data and Functions.
Data within an object are known as properties of the object. Functions within an object are known as methods that can be performed on an object. This concept is nothing but object-oriented programming OOPs concept.
Let’s understand it with a realtime example. Consider a fan as an example of an object. The data (properties) of a fan can be color, current speed, weight, etc. The functions of fan can be switching on-off of fan, changing speed of fan, etc.
Similarly, consider a button on a web page. A web button is an object in AUT. The properties or data of button can be name, width, height, visible, etc whereas, the functions or methods of a button can be clicking on the web button.
Hope that you understand the exact meaning of object w.r.t. Selenium. Let’s move to the main topic.
What is Object Repository?
A repository means a central location in which data is stored and managed. An object repository is a common storage location where we store a collection of all required application objects and their properties.
In other words, object repository is a central location where data is stored and managed. In context of Selenium WebDriver, objects repository is mainly used to store element locator values in a central location to avoid hard coding within scripts.
What are advantages of using Object Repository in Selenium Automation?
The main advantage of using object repository in selenium automation is the separation of objects from test cases. If the locator value of one web element changes, only the object repository needs to be changed rather than making changes in all test cases in which the locator has been used. This will increases the modularity of framework implementation.
Let’s understand the importance of Object repository with a realtime scenario. The scenario is that suppose you have 100 test cases and in all test cases, have a login scenario.
If your application changes in the future, and the locator value of one web element is also changed from email to login-email, you have to modify all the test cases and you have to change locator value email to login-email in all 100 test cases.
This task is very tedious and boring, is also not possible. So, to overcome this, we will have to move all locators in a separate file and link all test cases to this file. In the future, any changes happen in our locators, we will simply change in this file, not all our test cases.
Types of Creating Object Repository in Selenium WebDriver
In QTP, when a user records a test, application objects, and its properties are stored by default in an object repository. QTP uses this object repository to play back scripts. So, there is no need to create object repository in QTP. It provides an in-built object repository by default.
But in the case of Selenium WebDriver, it does not provide an in-built object repository by default. Object repository in Selenium can be created using the key-value pair technique wherein key refers to a name of an object and value refers to the property of an object that is used to uniquely identify an object within the web page.
There are two types of object repositories that can be created in Selenium WebDriver.
1. Object Repository using Properties file
2. Object Repository using XML file
Creating Object Repository using Properties file
Properties file in Selenium is a text file wherein data is stored in the form of key-value pairs. We will create properties file to store all element locators. This properties file acts as an object repository in Selenium WebDriver.
There are following steps to create object repository using properties file in eclipse. They are:
Step 1: Creating Properties file in Eclipse
1. Create a java project named “ObjectRepositoryDemo” and then create a package named “com.objectRepository.demo” as shown in the below screenshot.
2. Now, right-click on the main project folder and select New -> Other.
3. In the next dialog window, select General -> File and then click on ‘Next’ button.
4. Name a valid file with the extension ‘.properties’ on the new file resource window and click on ‘Finish’ button.
5. A file named ‘application.properties’ will be seen on your Project Structure.
Step 2: Storing data into properties file
Step 3: Reading data from Object repository properties file
2. Second, we need to create an object of FileInputStream class with passing reference variable “file” of File class object as a parameter to its constructor.
3. Create an object of Properties class and pass reference variable “fis” of FileInputStream class object as a parameter to its constructor.
Now, call load() method provided by Properties class in java to read data from properties file. The following complete code is as follows:
How to use Object Repository (Properties file) in Test scripts?
Object repository can be used in test scripts by reading data from a properties file and passing the data using the getProperty() method of Properties class as a parameter to the findElement method.
The following code will be implemented to read data from properties file in test scripts.
In the preceding code, getProperty is a method that has been used to search for the property with the specified key in the property list. If the key is not found in the property list, it will return a null value.
The syntax of getProperty() method is as follows:
public String getProperty(String key)
This method accepts the keys of String type and returns the specified key value in the property list.
Let’s take a test scenario where we will open the login page of Linkedin and send username and password to the text box by using properties file. We will use properties file as shown in the above screenshot.
The complete code for the above test scenario is as follows:
Program source code:
In the above code, getProperty() method accepts keys specified in the object repository and returns the value specified with keys to the findElement() method to locate web elements.
Hope that this tutorial has covered almost all important points related to creating object repository in Selenium WebDriver with suitable example scenario. I hope that you will have understood the properties file in selenium.
Thanks for reading!!!