Object Repository in Selenium

Learn via video courses
Topics Covered

Overview

An Object Repository in Selenium is a centralized location or file where web element locators and related information are stored separately from test scripts. It enhances test maintenance and reusability by allowing easy updates to locators without modifying the test scripts directly. By promoting a modular approach, the Object Repository improves the organization and management of locators, resulting in more robust and maintainable test scripts.

What is an Object Repository in Selenium?

An Object Repository in Selenium is a centralized storage or file that houses web element locators and associated information separately from the test scripts. It serves as a vital component for enhancing test maintenance and reusability by enabling easy updates to locators without directly modifying the test scripts. The Object Repository promotes a modular approach, facilitating efficient organization and management of locators for more robust and maintainable test scripts.

For example, let's consider a scenario where we have multiple test scripts that interact with a "Login" button on different pages. Instead of repeating the locator information in each script, we can store the "Login" button locator in the Object Repository and reference it in the respective test scripts. If the locator changes, we only need to update it once in the Object Repository, and all the test scripts utilizing that locator will automatically reflect the change. This saves time and effort in maintaining and updating the tests.

Types of Object Repository in Selenium

In Selenium, there are primarily two types of Object Repositories that can be used to store web element locators and related information.

Local Object Repository

A local Object Repository is specific to a particular test script or test suite. It is typically implemented as a data structure within the code itself. Locators such as XPath, CSS selectors, or IDs are directly embedded within the test scripts or stored in variables. This approach is simple and suitable for small-scale projects where the number of web elements is limited.

Let's look at a sample code where we have a local object repository embedded within the test script:

External Object Repository

An external Object Repository is a separate file or database that stores web element locators independently from the test scripts. Common formats for external repositories include XML, JSON, or properties files. The locators are stored hierarchically or in key-value pairs, associating them with descriptive names or logical identifiers. This approach allows for better organization, reusability, and central management of locators across multiple test scripts and projects.

Let's have a look at the external object repository in Selenium in the form of an XML file:

The same file, if written in JSON format for Selenium external object repository, would look like this:

Object Repository in Selenium Using Properties File

To implement an Object Repository in Selenium using a properties file, we can follow these steps:

Steps 1: Creating Properties File in Eclipse and Storing Data onto It

  • In Eclipse or any text editor, create a new file with a .properties extension (e.g., object_repository.properties).

  • Inside the properties file, define your web element locators using key-value pairs. For example:

  • Once the elements are defined, you can save the file for further use.

    creating properties file in eclipse and storing data onto it

Steps 2: Reading Data from Properties File

  • In your Selenium script, you can read the data from the properties file using the java.util.Properties class.

  • Load the properties file and retrieve the locator values using the corresponding keys.

Code Explanation

Let's break down the code into smaller units for better understanding:

Let's break down each important line of code in the given example:

  • import java.io.FileInputStream; and import java.util.Properties;: These lines import the necessary classes from java.io and java.util packages to work with file input streams and properties.
  • FileInputStream file = new FileInputStream("path/to/object_repository.properties");: This line creates a new FileInputStream object named file and specifies the path to the properties file. Make sure to provide the correct file path here.
  • Properties prop = new Properties();: This line creates a new Properties object named prop. The Properties class is used to load and store key-value pairs, which will represent the locator names and values from the properties file.
  • prop.load(file);: This line loads the properties file using the load() method of the Properties class. It reads the file data and populates the prop object with the key-value pairs defined in the properties file.
  • String usernameLocator = prop.getProperty("usernameInput");: This line retrieves the value associated with the key usernameInput from the prop object using the getProperty() method. It assigns the value to the usernameLocator variable. Similarly, the next two lines retrieve the values for the passwordInput and loginButton keys.

These lines collectively read the properties file, load the data into a Properties object, and retrieve the locator values using the respective keys. This allows you to dynamically access the stored locators and use them in your Selenium script for locating and interacting with web elements.

Steps 3: Using Properties File in Selenium Script

With the locator values retrieved from the properties file, you can utilize them in your Selenium script.

For example:

Object Repository in Selenium Using XML File

To implement an Object Repository in Selenium using an XML file, the following steps need to be done:

Step 1: Creating XML File in Eclipse and Storing Data onto It

  • In Eclipse or any text editor, create a new XML file (e.g., object_repository.xml).

  • Inside the XML file, define your web element locators using appropriate XML tags and structure. For example:

Step 2: Reading Data from XML File

  • In your Selenium script, you can read the data from the XML file using an XML parser.

  • Parse the XML file and extract the required locator values. Let's look at an example using the javax.xml.parsers.DocumentBuilderFactory:

Code Explanation

Let's breakdown the entire code to understand it better

  • DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();: This line creates a new instance of DocumentBuilderFactory, which is used to obtain a DocumentBuilder object.
  • DocumentBuilder builder = factory.newDocumentBuilder();: This line creates a new DocumentBuilder object using the newDocumentBuilder() method of the DocumentBuilderFactory class.
  • Document doc = builder.parse(new file ("path/to/object_repository.xml"));: This line parses the XML file specified by the file path and creates a Document object representing the XML document.
  • NodeList nodeList = doc.getElementsByTagName("object");: This line retrieves a list of all elements with the tag name "object" from the XML document.
  • for (int i = 0; i < nodeList.getLength(); i++) {: This line initiates a loop to iterate over the nodes in the NodeList.
    • Then, the current node from the NodeList at the specified index is retrieved.
    • if (node.getNodeType() == Node.ELEMENT_NODE) {: This line checks if the current node is an element node.
      • Then the required values are retrieved
  • The retrieved values can be used as required for your specific requirements within this block of code.

Step 3: Using XML File in Selenium Script

  • With the locator values retrieved from the XML file, you can utilize them in your Selenium script. For example:

Advantages of Object Repository in Selenium

The advantages of using an Object Repository in Selenium include:

  • Reusability: By storing web element locators in a centralized repository, it promotes reusability across multiple tests and test suites. Changes to locators can be easily managed in one place, avoiding the need for modifying them across various test scripts.
  • Maintainability: An Object Repository enhances the maintainability of automation scripts. It allows testers to separate the element locators from the test logic, making scripts more readable and easier to maintain. Updates or modifications to locators can be done in the repository without affecting the test scripts.
  • Scalability: As the number of test cases increases, managing locators individually becomes challenging. With an Object Repository, it becomes easier to handle a large number of locators and organize them effectively, improving the scalability of automation projects.
  • Collaboration: Object Repositories facilitate collaboration among team members. Testers can share and collaborate on a common repository, enabling better coordination and consistency in element identification across the project.
  • Cross-Browser and Cross-Platform Compatibility: By utilizing an Object Repository, it becomes easier to manage locators for different browsers and platforms. Separate repositories can be created for specific browser or platform configurations, ensuring better cross-browser and cross-platform compatibility.
  • Centralized Control: Object Repositories provide a centralized control point for managing locators. This allows for better control and governance over the identification and maintenance of elements, ensuring standardization and reducing duplication.

Conclusion

  • An Object Repository in Selenium is a centralized storage or file that separates web element locators from test scripts, promoting maintainability and reusability.
  • It allows easy updates to locators without modifying the test scripts directly, enhancing test maintenance efficiency.
  • There are two types of Object Repositories: local and external. Local repositories embed locators within the test scripts, while external repositories store locators in separate files or databases.
  • External Object Repositories offer better organization, reusability, and central management of locators across multiple scripts and projects.
  • Object Repositories improve collaboration, standardization, and scalability in Selenium automation projects.
  • Advantages of object repository in Selenium include the following:
    • Reusability across multiple tests and test suites.
    • Improved maintainability and easier updates to locators.
    • Scalability for handling a large number of locators.
    • Enhanced collaboration and consistency among team members.