{"id":2135,"date":"2023-04-03T10:43:50","date_gmt":"2023-04-03T10:43:50","guid":{"rendered":"https:\/\/automationqahub.com\/?p=2135"},"modified":"2024-04-08T11:49:15","modified_gmt":"2024-04-08T11:49:15","slug":"how-to-maintain-the-object-repository-in-selenium","status":"publish","type":"post","link":"https:\/\/automationqahub.com\/how-to-maintain-the-object-repository-in-selenium\/","title":{"rendered":"How To Maintain The Object Repository In Selenium"},"content":{"rendered":"\n

An Object Repository is a central location that is used to keep the locators in the form of objects. The Major advantages of maintaining an object repository are readability, maintainability and reusability. In this article, we will discuss several approaches to maintaining the object repository in Selenium.<\/p>\n\n\n\n

Types of Object Repository in Selenium<\/h2>\n\n\n\n

Page object Model or POM is a trendy approach in the Selenium framework where an Object repository is maintained in the form of page classes but in this article, we will see different approaches.<\/p>\n\n\n\n

1)Object Repository using a properties file.<\/p>\n\n\n\n

2)Object Repository using YAML file.<\/p>\n\n\n\n

3)Object Repository using XML file.<\/p>\n\n\n\n

1)Selenium Object repository using the Properties file<\/h3>\n\n\n\n

Data is stored in properties files in the form of key-value pairs.<\/p>\n\n\n\n

1)To start with create a properties file by right click -> new -> file
2)Give it a name for example locators.properties<\/strong>
3)Open properties files and store locators in the form of keys and values.<\/p>\n\n\n\n

<\/path><\/path><\/svg><\/span>
Home<\/span>=<\/span>"<\/span>\/\/span[normalize-space()='Home']<\/span>"<\/span><\/span>\nEmailTextBox <\/span>=<\/span> home<\/span>-<\/span>email<\/span><\/span>\nSignUpButton <\/span>=<\/span> home<\/span>-<\/span>submit<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n

4)Read data from the properties file. Create a class, name it as ReadPropertyFile and add the below code.<\/p>\n\n\n\n

<\/path><\/path><\/svg><\/span>
public<\/span> <\/span>final<\/span> <\/span>class<\/span> <\/span>ReadingPropertiesFile<\/span> {<\/span><\/span>\n\t<\/span><\/span>\n\t<\/span>private<\/span> <\/span>static<\/span> <\/span>Properties<\/span> properties <\/span>=<\/span> <\/span>new<\/span> <\/span>Properties<\/span>();<\/span><\/span>\n\t<\/span>public<\/span> <\/span>static<\/span> <\/span>String<\/span> <\/span>getValue<\/span>(<\/span>String<\/span> <\/span>key<\/span>) <\/span>throws<\/span> <\/span>Exception<\/span><\/span>\n\t{<\/span><\/span>\n\t  <\/span>String<\/span> value<\/span>=<\/span>""<\/span>;<\/span><\/span>\n         <\/span>InputStream<\/span> input<\/span>=<\/span>new<\/span> <\/span>FileInputStream<\/span>(<\/span>"<\/span>locator.properties file path<\/span>"<\/span>);<\/span><\/span>\n\t\tproperties.<\/span>load<\/span>(input);<\/span><\/span>\n\t\tvalue<\/span>=<\/span>properties.<\/span>getProperty<\/span>(key);<\/span><\/span>\n\t\t<\/span>if<\/span>(Objects.<\/span>isNull<\/span>(value))<\/span><\/span>\n\t\t{<\/span><\/span>\nthrow<\/span> <\/span>new<\/span> <\/span>Exception<\/span>(<\/span>"<\/span>property name<\/span>"<\/span> <\/span>+<\/span>key<\/span>+<\/span>"<\/span>is not found.Please check config.properties<\/span>"<\/span>);<\/span><\/span>\n\t\t}<\/span><\/span>\n\t\t<\/span>return<\/span> value;<\/span><\/span>\n\t}}<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n

5) Start using the properties file in the Test class.<\/p>\n\n\n\n

<\/path><\/path><\/svg><\/span>
driver.<\/span>findElement<\/span>(By.<\/span>xpath<\/span>(ReadingPropertiesFile.<\/span>getValue<\/span>(<\/span>"<\/span>Home<\/span>"<\/span>))).<\/span>click<\/span>();<\/span><\/span>\ndriver.<\/span>findElement<\/span>(By.<\/span>id<\/span>(ReadingPropertiesFile.<\/span>getValue<\/span>(<\/span>"<\/span>EmailTextBox<\/span>"<\/span>))).<\/span>sendKeys<\/span>(<\/span>"<\/span>aqh@gmail.com<\/span>"<\/span>);\t\t\t\t\t\t\t\t<\/span><\/span>\ndriver.<\/span>findElement<\/span>(By.<\/span>id<\/span>(ReadingPropertiesFile.<\/span>getValue<\/span>(<\/span>"<\/span>SignUpButton<\/span>"<\/span>))).<\/span>click<\/span>();<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n

Advantage: <\/strong> This approach is useful for small projects where all the locators can be stored in properties files and reusable across the framework. Another benefit is that locators are externalized from the framework, so a less experienced person can access and make changes in the locator.properties file in case of failure due to locator changes. He\/she need not look into the entire framework to update the locators.<\/p>\n\n\n\n

Disadvantage<\/strong>: This approach is not suitable for large projects because it’s not feasible to write thousands of locators in the properties file and access it from there for readability purposes.<\/p>\n\n\n\n