Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is an Object Repository in Selenium?
3.
Types of Object Repository in Selenium
3.1.
Local Object Repository
3.2.
External Object Repository
4.
How to Create an Object Repository in Selenium?
4.1.
Using a Properties File
4.2.
Java
4.3.
Using an XML File
4.4.
Java
5.
Advantages of Object Repository in Selenium
6.
Frequently Asked Questions
6.1.
What is the main benefit of using an object repository in Selenium?
6.2.
Can we use any programming language to create an object repository?
6.3.
What is the purpose of an object repository?
6.4.
What is the advantage of using XML files for an object repository?
7.
Conclusion
Last Updated: Apr 17, 2024
Medium

Object Repository in Selenium

Introduction

The Object Repository in Selenium is a centralized storage mechanism used to manage and store locators or identifiers for web elements. It allows testers to organize and maintain element locators separately from test scripts, enhancing reusability, readability, and maintainability of automated tests.

Nowadays, automation plays an important role in web applications and websites. To automate the web application, Selenium is one of the best ways. In Selenium, an object repository is used to enhance the efficiency and maintainability of our automation script.

object repository in selenium

In this article, we will discuss about an object repository in Selenium. Firstly, we will discuss about what Selenium and an object repository are. Then we will explain about ways to create an object repository. At the end of this article, we will figure out why we need to create an object repository in Selenium.

So, let's get started!!

What is an Object Repository in Selenium?

In Selenium, an object repository is used to write efficient and maintain the automation script. It is a centralized collection of UI(User Interface) elements that we are using in a web application. It acts as a map between the UI elements and their locators. 

An object repository is a storage place for locators such as IDs, names, and their corresponding UI elements. This thing helps in managing and updating elements across different tests and pages. For example, we are finding an element named ninjas on our Coding Ninjas website. So, we can write a line to find our UI element:

WebElement ninjaElement = driver.findElement(By.class("ninjas"));

 

In this example, we are finding ninjaElement. So, our locator is By class, and the class name is ninjas. This is what we call an object repository.

**Now you might be wondering what are the ways to create an object repository.

Types of Object Repository in Selenium

In Selenium automation testing, Object Repositories serve as a crucial component for storing and managing locators or identifiers of web elements.

Local Object Repository

A Local Object Repository in Selenium stores element locators directly within the test automation framework, typically within the test scripts or in separate classes/files within the project structure. This approach allows testers to define and manage element locators within the same codebase as the test scripts. While it provides simplicity and ease of access, changes to locators require modifications directly within the codebase, potentially leading to increased maintenance efforts, especially in larger test suites.

External Object Repository

An External Object Repository in Selenium stores element locators externally, outside of the test automation framework. These locators are typically stored in separate files (such as XML, JSON, or properties files) or databases, enabling centralized management and easy access across multiple test scripts. This approach promotes reusability, maintainability, and scalability of locators, as changes can be made in a single location without modifying test scripts. However, it may introduce dependencies on external files or databases and require additional configuration to integrate with the test automation framework.

How to Create an Object Repository in Selenium?

We can create an object repository in Selenium in two ways:

Using a Properties File

Using a Properties file, we can store locators and their corresponding values in a properties file. Using this way of creating an object repository will separate the locators from the code. It also makes maintenance simpler. Suppose we have a project called SeleniumRobot. In this project, we can create an application.properties file to store the locators and values separately. We need to follow the below-mentioned steps:

Step 1: Right-click on the created package, then go to New, and then go to Other.

creating a file

Step 2: Now, select the General category, go to the File option, and then click on the Next button.

go to file option

Step 3: Now, give a name to our properties file, so we are giving the name as application.properties, then click on the finish button.

creating application.properties

Then we will see our file is created successfully.

file created successfully

Suppose we are passing the data in application.properties as

# This is application.properties
name= Narayan
age= 22

browser= chrome

 

Now, we need to read this properties file, so we can create a class ReadingAppProp.

creating a class

In this class, we need to write the following script to read the properties file and to check whether the browser is Chrome or not:

  • Java

Java

package codingninjas;

import java.io.FileInputStream;

import java.util.Properties;

import java.io.IOException;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;


public class ReadingAppProp {

public static void main(String[] args) throws IOException {

// Importing Properties

Properties prop=new Properties();

// Passing the path of application.properties file as input

FileInputStream input= new FileInputStream("I:\\JavaCodes\\SeleniumRobotClass\\src\\codingninjas\\application.properties");

// Loading the properties file

prop.load(input);



// Trying to print the properties

System.out.println("Name is:-"+ prop.getProperty("name"));



System.out.println("Age is:-"+prop.getProperty("age"));



String browserName=prop.getProperty("browser");



if(browserName.equals("chrome"))

{

// Set the path of our Chrome browser

System.setProperty("webdriver.chrome.driver", "C:\\Users\\naray\\Downloads\\chromedriver_win32\\chromedriver.exe");



// Using the WebDriver

WebDriver driver=new ChromeDriver();

}

else

{

System.out.println("Check properties file browser is different!!");

}

}

}

After executing the above code, we will get:

output

And we will see the Chrome browser is started:

chrome browser started

Using an XML File

Using an XML file, we can hold the locators and elements. By using this way of creating an object repository in Selenium, we will get a structured and easily readable format for storage. Suppose we have a project called SeleniumRobot. In this project, we can create an XML file to store the locators and elements. We need to follow the below-mentioned steps:

Step 1: Right-click on the created package, then go to New, and then go to Other.

creating a new xml file

Step 2: Now, go to the file option and save the file with the .xml extension.

creating properties.xml file

As we can see in the package, we have successfully created the XML file.

file is created successfully

Suppose we are passing the data in properties.xml as

<?xml version="1.0" encoding="UTF-8"?>
<person>
    <name>Narayan Mishra</name>
    <age>22</age>
    <occupation>Software Engineer</occupation>
    <browser>Firefox</browser>
</person>

 

Now, we need to read this XML file, so we can create a class ReadingAppXML.

creating a class

In this class, we need to write the following script to read the XML file and to check whether the browser is Chrome or not:

  • Java

Java

package codingninjas;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;



public class ReadingAppXML {

   public static void main(String[] args) {

       try {

           // Passing the path of the created XML file

           File input = new File("I:\\JavaCodes\\SeleniumRobotClass\\src\\codingninjas\\properties.xml");

         

           // Create a DocumentBuilderFactory to parse the XML

           DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

           DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();

           Document doc = dBuilder.parse(input);


           // Normalizing the XML document

           doc.getDocumentElement().normalize();

           Element passedElement = doc.getDocumentElement();



           // Extracting values from XML tags

           String name = getElementValue(passedElement, "name");

           String age = getElementValue(passedElement, "age");

           String occupation = getElementValue(passedElement, "occupation");

           String browser = getElementValue(passedElement, "browser");


           // Printing the extracted values from properties.xml

           System.out.println("Name is: " + name);

           System.out.println("Age is: " + age);

           System.out.println("Occupation is: " + occupation);

          

           // Checking browser value from XML and initializing WebDriver accordingly

           if(browser.equals("Chrome")) {

               // Set the path of the Chrome browser executable

               System.setProperty("webdriver.chrome.driver", "C:\\Users\\naray\\Downloads\\chromedriver_win32\\chromedriver.exe");

              

               // Initialize the WebDriver with ChromeDriver

               WebDriver driver = new ChromeDriver();

           } else {

               System.out.println("Check properties file browser is different!!");

           }



       } catch (Exception e) {

           e.printStackTrace();

       }

   }



   // Method to extract text content from an XML element

   private static String getElementValue(Element element, String tagName) {

       NodeList nodeList = element.getElementsByTagName(tagName).item(0).getChildNodes();

       Node node = nodeList.item(0);

       return node.getNodeValue();

   }

}

After executing the above code, we will get the following output:

output

Advantages of Object Repository in Selenium

The advantages of using an Object Repository in Selenium are numerous:

  • Enhanced Reusability: Object Repositories allow testers to define and store element locators in a centralized location, facilitating their reuse across multiple test scripts. This minimizes redundancy and promotes consistency in test automation projects.
  • Improved Maintainability: By separating element locators from test scripts, Object Repositories simplify maintenance tasks. Testers can easily update or modify locators in the repository without having to edit multiple test scripts, thereby reducing the risk of errors and ensuring faster maintenance cycles.
  • Centralized Management: Object Repositories provide a centralized location for managing element locators, making it easier to organize, categorize, and search for locators. This centralized approach enhances the organization and management of test assets, leading to improved efficiency and productivity.
  • Enhanced Readability: Test scripts become more readable and understandable when element locators are stored in a separate repository. Testers can focus on test logic and scenarios without cluttering the code with locator details, resulting in cleaner and more maintainable code.
  • Facilitates Collaboration: Object Repositories promote collaboration among team members by providing a shared repository for storing and accessing element locators. This fosters teamwork, encourages knowledge sharing, and ensures consistency in test automation efforts across the team.

Frequently Asked Questions

What is the main benefit of using an object repository in Selenium?

By using an object repository in Selenium, we can enhance the maintainability and reusability of automation code. It is used to centralize the storage of web elements and their locators. This helps in making updates and changes more efficient across different tests and pages.

Can we use any programming language to create an object repository?

Yes, we can create an object repository using various programming languages. Selenium supports multiple programming languages like Java, Python, C#, etc.

What is the purpose of an object repository?

The purpose of an object repository is to store and manage element locators or identifiers for web elements used in test automation scripts, enhancing reusability and maintainability.

What is the advantage of using XML files for an object repository?

Using XML files provides a structured and easily readable format for storing locators and elements. It separates the element information from the code and can be beneficial for collaboration and maintenance.

Conclusion

In this article, we have discussed an object repository in Selenium. We have also explained ways to create an object repository in Selenium. If you want to learn more about Selenium, then you can check out our blogs:

We hope this blog helps you to get knowledge about an object repository in Selenium. You can refer to our guided paths on the Coding Ninjas Studio platform. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

To practice and improve yourself in the interview, you can also check out Interview ExperienceCoding interview questions, and the Ultimate Guide path for interviews.

Happy Coding!!

Live masterclass