All IT Courses 50% Off
Selenium Tutorials

Creating Object Repository in Selenium WebDriver

A repository means a central location where data is stored and managed. An object repository is the common storage location where we can store a collection of all required application objects and their properties.

In other words, an object repository is a central location where data is stored and managed. In Selenium WebDriver, the objects repository is used to store element locator values in a central location to avoid hard coding within scripts.

The main advantage of using an object repository in selenium automation is separating objects from test cases. If the locator value of any one web element changes on the site, only the object repository needs to be updated rather than making the changes in all test cases in which the locator has been used increasing the modularity of framework implementation.

Types of Object Repositories in the Selenium Web Driver:

Selenium WebDriver does not provide an in-built object repository by default. The object repository in Selenium can be created using the key-value pair technique wherein the key refers to the name of an object and value refers to the property of that object used to identify an object within the web page uniquely.

Two types of object repositories can be created in Selenium WebDriver.

1. Object Repository using the Properties file

2. Object Repository using the XML file

Creating Object Repository using the Properties file:

The properties file is a text file with .properties as an extension where the tester can store environment configuration details like browser name, driver path, URL, userid, password that are common for all test scripts of an application. We can also use the properties file as Object Repository and store the path of different Web elements.

Properties file store the data in a key-value approach where key refer to object and value refer to that object’s property.

Let us assume that the tester created different test classes for an application, and the tester is using a chrome browser. So tester has to pass the path of chrome driver in each test class. After some time, if he changes the chrome driver’s path, he has to update the path in all the test classes of the application, which is not a good approach. So what he can do is he will store the path of chrome driver in the properties file and read the data from that file. If he wants some modification, he can do it in the properties file, and data will be updated in all test classes.

To use the properties file as Object Repository and to read data from the properties file, follow the below steps.

You can find the code at the end; here is an explanation of that code.

1. Create a file with .properties as an extension

2. Store value in key-value format

3. Write code to fetch the data from the properties file.

To read the data from the properties file, write the below code.

-> Create Properties class object   

           Properties pro = new Properties();

-> Pass the properties file’s path and load the properties file’s data to the Properties class using the load() method. 

          FileInputStream file = new FileInputStream(“path of properties file”); 

          pro.load(file);

-> Use getProperty() method to get data from properties file.

         pro.getProperty(“gmail”);  (“gmail” is the object name that is  created in properties file.)

To create a property file in the project.

Go to package >New >File.

https://mundrisoft.com/tech-bytes/wp-content/uploads/2019/07/new.png

Create a file with .properties as an extension and click on the finish button.

https://mundrisoft.com/tech-bytes/wp-content/uploads/2019/07/new2.png

Properties file will look like the below image in the project structure after created.

https://mundrisoft.com/tech-bytes/wp-content/uploads/2019/07/package-620x187.png

Store the data in properties file like the below image.

https://mundrisoft.com/tech-bytes/wp-content/uploads/2019/07/read123.png

Code to read data from properties file:

package selenium;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class PropertyDataRead {
 public static void main(String arg[]) throws IOException, InterruptedException { 
 System.setProperty("webdriver.chrome.driver", "path of chrome driver");
 WebDriver driver=new ChromeDriver();
 driver.manage().window().maximize();
 driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
 driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
 driver.get("https://www.google.com");
 Properties pro =new Properties();
 FileInputStream file=new FileInputStream("D:\\selenium\\New\\readProperty.properties");
 pro.load(file);
 driver.findElement(By.xpath(pro.getProperty("gmail"))).click();
 driver.navigate().back();
 driver.findElement(By.xpath(pro.getProperty("image"))).click();
 driver.navigate().back();
 Thread.sleep(5000);
 WebElement searchButton=driver.findElement(By.xpath(pro.getProperty("search")));
 searchButton.sendKeys("selenium" ,Keys.ENTER);
 }
}

Selenium WebDriver Object Repository Using XML File

XML stands for Extensible Markup Language. An XML File uses the Document Object Model (DOM) as the basic structure. This File format will replicate the HTML format upon which the webpage is constructed.

So to use the XML file as an object repository, we will follow the below steps.

1. Create an XML file in the project

2. Store the locators’ path in the XML file

3. Write the script to fetch the data from the XML file

To create an XML file

Go to Package->New->File

https://mundrisoft.com/tech-bytes/wp-content/uploads/2019/07/new-1.png

Create a file with .xml as an extension and click on the finish button.

https://mundrisoft.com/tech-bytes/wp-content/uploads/2019/07/dataxml-1.png

Store different path of web elements in the xml file like below format.

<?xml version="1.0" encoding="UTF-8"?>
<locator>
<gmail>//a[text()='Gmail']</gmail>
<image>//a[text()='Images']</image>
<search>//input[@name='q']</search>
</locator>
Code to fetch the data from an XML file:
package selenium; 
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
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;
import org.xml.sax.SAXException; 
public class ReadDataFromXml {
 public static String readXmlData(String data) throws ParserConfigurationException, SAXException, IOException {
 File file = new File("D:\\selenium\\New\\ObjectDataRead.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();
 } 
 public static void main(String arg[]) throws IOException, ParserConfigurationException, SAXException {
 System.setProperty("webdriver.chrome.driver", "path of chrome driver");
 WebDriver driver = new ChromeDriver();
 driver.manage().window().maximize();
 driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
 driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
 driver.get("https://www.google.com");
 driver.findElement(By.xpath(readXmlData("gmail"))).click();
 driver.navigate().back();
 driver.findElement(By.xpath(readXmlData("image"))).click();
 driver.navigate().back();
 driver.findElement(By.xpath(readXmlData("search"))).sendKeys("Selenium", Keys.ENTER);
 }
}

Facebook Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Back to top button
Close
Close

Get Selenium Training Online
Course Now at 50%OFF!

Offer valid for 1st 20 seats only, Hurry up!!

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

H2kinfosys will use this information to be in touch with you for providing updates.