How to Create an Object Repository in Selenium WebDriver
We get a lot of questions in which our readers wants to learn more about Object Repositories in Selenium WebDriver and how to create one. I think this is about time to have an article on this topic.
What is an Object repository?
An Object Repository is a map between UI element and its locator. Which can also be written as an Object Map between UI element and the way to find it. In Selenium WebDriver's context it means a Mapping between WebElement and the corresponding locator type and value. Lets understand it in more details by looking at a typical WebDriver code which finds an element.
WebDriver browser = new FirefoxDriver();
driver.get("https://toolsqa.com");
WebElement loginElement = driver.findElement(By.id("Element01"));
We can see that findElement command accepts the locator of the WebElement. In the example above locator is By Id and the value of Id is Element01. The name of the element is loginElement. There are three distinct values that we need to identify a web element:
- Name of the Element
- Locator type of element. Basically what By we are using
- Value of the locator
These three values when given can help us find an element uniquely on web page. This is what constitutes an Object Repository.
Storage of Object Repository
Now the question comes where do we store this map for various elements that are present in an application. The answer is not simple and it depends on what kind of frame work you are using. Some frameworks store an Object Repository in a properties file and some store it inside an XML or an Excel file. Today we will learn how to store these values in an Properties file. We will also learn how to read these values and use it in the test code.
Object repository in a Properties file
Test page that I have used in this tutorial is "http://toolsqa.com/automation-practice-form/" on this page we have three element First and Last name field and a link element with "Partial Link Text" as the Link text. Lets try to create a repository for these three elements. A property file stores information in a Key-Value pair. Key value pair is represented by two string values separated by the equal to sign. A typical property file looks like this:
In our case we have to store three values instead of two. Element Name, Locator type and the Locator value. We will simply separate two values with ':'and use it. Here is a sample object repository file that we will create and will name it ObjectRepo.properties.
Note : In a properties file a comment can be added by using the # keyword. As shown above the part that is displayed in green is basically a comment and will not be read by the properties file reader.
Reading Object Repository properties file
Java has support or the .properties files. One can easily read a properties file in three simple step
- Create a FileInputStream object on the .properties file
- Creating a Properties object over the File input stream created in step 1
- Simply read the Key-Values by using the getProperty("Property name"); method on Properties class.
For reading an Object Repository we will create a simple class and name it RepositoryParser.java. Here is the code for that
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import org.openqa.selenium.By;
public class RespositoryParser {
private FileInputStream stream;
private String RepositoryFile;
private Properties propertyFile = new Properties();
public RespositoryParser(String fileName) throws IOException
{
this.RepositoryFile = fileName;
stream = new FileInputStream(RepositoryFile);
propertyFile.load(stream);
}
public By getbjectLocator(String locatorName)
{
String locatorProperty = propertyFile.getProperty(locatorName);
System.out.println(locatorProperty.toString());
String locatorType = locatorProperty.split(":")[0];
String locatorValue = locatorProperty.split(":")[1];
By locator = null;
switch(locatorType)
{
case "Id":
locator = By.id(locatorValue);
break;
case "Name":
locator = By.name(locatorValue);
break;
case "CssSelector":
locator = By.cssSelector(locatorValue);
break;
case "LinkText":
locator = By.linkText(locatorValue);
break;
case "PartialLinkText":
locator = By.partialLinkText(locatorValue);
break;
case "TagName":
locator = By.tagName(locatorValue);
break;
case "Xpath":
locator = By.xpath(locatorValue);
break;
}
return locator;
}
}
Explanation of the code: RepositoryParser class accepts the .properties file path in the constructor. Once file path is received an FileInputStream object is created. Then this is fed to the Java.Util.Properties class to create a properties map between the key value pairs. Once we have the Key value map generated we have the ability to query the values from the Object repository. All the core logic happens inside the getbjectLocator(String elementName) method. Here elementName is nothing but the element you are looking for. This method at the end returns a By locator with correct value of Locator type and Locator value.
Usage of Object Repository Parser in Test
Lets create a simple TestNg test class to show the usage. All we would need is a RepositoryParser class. Here are the tests.
package Tests;
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import RepoUtils.RespositoryParser;
public class SampleTests {
private RespositoryParser parser;
private WebDriver driver;
@BeforeClass
public void setUp() throws IOException
{
driver = new FirefoxDriver();
driver.get("https://toolsqa.com/automation-practice-form/");
parser = new RespositoryParser("ObjectRepo.properties");
}
@Test
public void EnterValue()
{
//Lets see how we can find the first name field
WebElement FirstName = driver.findElement(parser.getbjectLocator("FirstName"));
WebElement LastName = driver.findElement(parser.getbjectLocator("LastName"));
FirstName.sendKeys("Virender");
LastName.sendKeys("Singh");
}
@Test
public void FindPartialLink()
{
WebElement link = driver.findElement(parser.getbjectLocator("PartialLink"));
link.click();
}
@AfterClass
public void tearDown()
{
driver.quit();
}
}
I hope this give you a starting point to understand and create Object Repositories. We will use Excel based Object Repository in coming tutorials.