How to create Object Repository in Selenium – User friendly Tech help

Unlike QTP/UFT, Selenium does not offer the default implementation for  object repository. In QTP things are really straightforward, just object spy the controls and add to object repository, and further with the blessings of intellisense feature in its IDE, utilize them easily in writing scripts.

n

But how we can achieve the same in selenium??..
nIt can be done by using Properties file feature of Java. Lets begin with some basic explanation.
n
nWhat is Object Repository?
n
Object Repository is a centralized location where we can store objects information, it acts as interface between Test script and application in order to identify the objects during the execution.
n
nWe always recommend using external file for object repository rather than hard coding the objects and its properties directly into our code. Why this??? As it reduces the maintenance  effort and provides positive ROI, for example say any of the object properties change within our application under test, we can easily change it in external object repository file, rather than searching and doing updates  for that object individually in the code.
n
nPrinciple:
nA basic object repository can be implemented as a collection of key-value pairs, with the key being a logical name identifying the object and the value containing unique objects properties used to identify the object on a screen. For this, we will use a .properties file in Java which is a basic collection of key-value pairs.

n

Creating Properties File in Eclipse:-
n
nStep1:-
nRight click on the Package in the solution Explorer of Eclipse-> New ->Other
n

n

n

n

n

n

n

n

n

Adding New File in Eclipse

n

Or 
nRight click on the Package in the solution Explorer of Eclipse-> New ->File (In this case Step-2 below is not required)
n

n

n

n

n

n

n

n

n

New ->File in Eclipse

n


Step2:
nGeneral ->File ->Click Next
n

n

n

n

n

n

n

n

n

New Wizard in Eclipse

n

Step3:-
nGive name to the file with .properties extension (Say OR_Gmail_Login.properties) ->Click Finish

n

n

n

n

n

n

n

n

Naming Properties File in Eclipse

n

n
nNote:- 
nWe generally create each properties file for every single page and capture all the UI elements present on the page and use it as per the needs.
n
n
nAdding Key/value Pairs in Properties File:-
n
nBefore adding our objects into object repository, let’s outline a simple scenario that we will be automating in Selenium:-
n
n1. Launch Gmail login page.
n2. Fill the Username & Password fields and click on Submit button
n
After creating our empty properties file, now we need to add our elements, in the form of key and Value pairs.
nFor Example:-
n
nGmail.LoginPage.txtPassword -> Key
nPasswd->Value
n
nNote: – In Value property we have taken the Locator and its value, which will be used to identify our control. As password field, is getting uniquely identified by ID field so we have taken that value.
n

n

n

n

n

n

n

n

n

Properties File in Java

n

nCode:-

n

package OR;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class PropertiesFile {
public static void main(String[] args) {
//Creating the File Object
File file = new File("D:\Automation\Selenium\MyCode\FirstSeleniumCode\src\OR\OR_Gmail_Login.properties");
//Creating properties object
Properties prop = new Properties();
//Creating InputStream object to read data
FileInputStream objInput = null;
try {
objInput = new FileInputStream(file);
//Reading properties key/values in file prop.load(objInput);
//Closing the InputStream objInput.close();
} catch (FileNotFoundException e) { System.out.println(e.getMessage());
} catch (IOException e) { System.out.println(e.getMessage()); }
//Creating the driver instance
WebDriver driver = new FirefoxDriver();
//Adding wait driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
//Launching the GMAIL page
driver.get(prop.getProperty("Gmail.URL"));
//Entering the UserName
WebElement element = driver.findElement(By.id(prop.getProperty("Gmail.LoginPage.txtUserName")));
element.sendKeys("[email protected]");
//Entering the Password
element = driver.findElement(By.id(prop.getProperty("Gmail.LoginPage.txtPassword")));
element.sendKeys("uftHelp");
//Clicking the SignIn button
element = driver.findElement(By.id(prop.getProperty("Gmail.LoginPage.btnSignIn"))); element.click();
System.out.println("Test Scenario Completed!!");
//Destroying the driver object
//driver.close(); }}
Was this article helpful?
YesNo

Similar Posts