How To Maintain The Object Repository In Selenium

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.

Types of Object Repository in Selenium

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.

1)Object Repository using a properties file.

2)Object Repository using YAML file.

3)Object Repository using XML file.

1)Selenium Object repository using the Properties file

Data is stored in properties files in the form of key-value pairs.

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

EmailTextBox = home-email
SignUpButton = home-submit

4)Read data from the properties file. Create a class, name it as ReadPropertyFile and add the below code.

public final class ReadingPropertiesFile {
	private static Properties properties = new Properties();
	public static String getValue(String key) throws Exception
	  String value="";
         InputStream input=new FileInputStream(" file path");
throw new Exception("property name" +key+"is not found.Please check");
		return value;

5) Start using the properties file in the Test class.

driver.findElement("EmailTextBox"))).sendKeys("[email protected]");								

Advantage: 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 file in case of failure due to locator changes. He/she need not look into the entire framework to update the locators.

Disadvantage: 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.

2)Selenium Object repository using the XML File

XML (Extensible Markup Language) is one of the commonly used approaches to storing and sending data.

  1. Click on New -> Other ->Select XML file ->Click next
  2. Enter the name as locator.xml and click save.
  3. Insert the XML declaration and add the root element.
<?xml version="1.0" encoding="UTF-8"?>

4. Under the root node provide the locators.


5. Create a class ReadObjectRepository and add the below code.

public class ReadObjectRepository {
public static String readXmlData(String data) throws ParserConfigurationException, SAXException, IOException {
File file = new File("/Users/varsha/Downloads/Hello-World-JAVA-master/DemoSampleApp_selenium/NewFile.xml");
	DocumentBuilderFactory Df = DocumentBuilderFactory.newInstance();
	DocumentBuilder Db = Df.newDocumentBuilder();
	Document document = Db.parse(file);
	NodeList list = document.getElementsByTagName("locator");
	Node node1 = list.item(0);
	Element ele = (Element) node1;
	return ele.getElementsByTagName(data).item(0).getTextContent();

6)Navigate to the test class and modify the code to read the locators from the XML file.

	ChromeOptions options = new ChromeOptions();
	WebDriver driver = new ChromeDriver(options);
	driver.findElement(By.xpath(readXmlData("search"))).sendKeys("Selenium", Keys.ENTER);

As per the requirement, the XML file can be enhanced to accommodate more tags like Type and value to improve readability.

<?xml version="1.0" encoding="UTF-8"?>

In the above code snippet we can see that we have modified the XML file in a more modular and readable format. I have added different tags like “NameOfElement”, and then further added element properties like “Type” and “Value”. This way our XML is more refined.

To read Nested XML nodes add the below code in your class file.

File inputFile = new File("/Users/varsha/Downloads/Hello-World-JAVA-master/DemoSampleApp_selenium/NewFile.xml");
		DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
		Document doc = dBuilder.parse(inputFile);
		System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
		NodeList nList = doc.getElementsByTagName("Element");
     	for (int temp = 0; temp < nList.getLength(); temp++) {
			Node nNode = nList.item(temp);
			System.out.println("\nCurrent Element :" + nNode.getNodeName());

			if (nNode.getNodeType() == Node.ELEMENT_NODE) {
				Element eElement = (Element) nNode;

				System.out.println("NameOfElement  : " 
						+ eElement
				System.out.println("Type : " 
						+ eElement
				System.out.println("Value: " 
						+ eElement


After execution, you will get all the Node information that can be utilized in the test scripts.

Object Repository In Selenium

Apart from these 2 approaches discussed above, there are several ways by which an object repository can be maintained. Just like the Properties file we can manage locators in the YAML file. Similar to the XML approach we can configure JSON files to read locators or can use excels, and CSV to externalize the objects.

Discover more from AutomationQaHub

Subscribe now to keep reading and get access to the full archive.

Continue reading