SUBSCRIBE HERE TO GET POST UPDATES VIA EMAIL :

Delivered by FeedBurner

Creating Object Repository Using Properties File In Selenium WebDriver

In selenium WebDriver software automation testing tool, There Is not any built In facility to create object repository. So maintenance of page objects(Page element locators) becomes very hard If you will write all objects of page on code level. Let me give you one simple example - You have one search button on page of software web application and you have used It(Using Id locator) In many of your test cases. Now supposing someone has changed Its Id In your application then what you will do? You will go for replacing Id of that element In all test cases wherever It Is used? It will create overhead for you. Simple solution to remove this overhead Is creating object repository using properties File support of java software development language. In properties file, First we can write element locator of software web element and then we can use It In our test cases.

Let us try to create object repository for simple calculator application given on THIS PAGE and then will try to create test case for that calc software application using object repository..

How to create new properties file
To create new properties file, Right click on your project package -> New -> Other .



It will open New wizard. Expand General folder In New wizard and select File and click on Next button.


Give file name objects.properties on next window and click on Finish button.

It will add object.properties file under your package. Now copy paste bellow given lines in objects.properties file. So now this objects.properties file Is object repository for your web application page calc.

objects.properties file
#Created unique key for Id of all web elements.
# Example 'one' Is key and '1' Is ID of web element button 1.
one=1
two=2
three=3
four=4
five=5
six=6
seven=7
eight=8
nine=9
zero=0
equalsto=equals
cler=AC
result=Resultbox
plus=plus
minus=minus
mul=multiply

In above file, Left side value Is key and right side value Is element locator(by Id) of all web elements of web calculator. You can use other element locator methods too like xpath, css ect.. VISIT THIS LINK to view different element locator methods of webdriver.

Now let us create webdriver test case to perform some operations on calculation. In bellow given example, All the element locators are coming from objects.properties file using obj.getProperty(key). Here key Is reference of element locator value in objects.properties file.

package ObjectRepo;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class CalcTest {
 WebDriver driver = new FirefoxDriver(); 
 
 @BeforeMethod
  public void openbrowser() throws IOException { 
   driver.manage().window().maximize();
   driver.get("http://only-testing-blog.blogspot.in/2014/04/calc.html");   
  }
 
 @AfterMethod
  public void closebrowser() {   
   driver.quit();
  }  
 
 @Test
  public void Calc_Operations() throws IOException{
  //Create Object of Properties Class.
  Properties obj = new Properties();   
  //Create Object of FileInputStream Class. Pass file path.
  FileInputStream objfile = new FileInputStream(System.getProperty("user.dir")+"\\src\\ObjectRepo\\objects.properties");
  //Pass object reference objfile to load method of Properties object.
  obj.load(objfile); 
  
  //Sum operation on calculator.
  //Accessing element locators of all web elements using obj.getProperty(key)
  driver.findElement(By.id(obj.getProperty("eight"))).click();
  driver.findElement(By.id(obj.getProperty("plus"))).click();
  driver.findElement(By.id(obj.getProperty("four"))).click();
  driver.findElement(By.id(obj.getProperty("equalsto"))).click();
  String i = driver.findElement(By.id(obj.getProperty("result"))).getAttribute("value");
  System.out.println(obj.getProperty("eight")+" + "+obj.getProperty("four")+" = "+i); 
  driver.findElement(By.id(obj.getProperty("result"))).clear();
  
  //Subtraction operation on calculator.
  //Accessing element locators of all web elements using obj.getProperty(key)
  driver.findElement(By.id(obj.getProperty("nine"))).click();
  driver.findElement(By.id(obj.getProperty("minus"))).click();
  driver.findElement(By.id(obj.getProperty("three"))).click();
  driver.findElement(By.id(obj.getProperty("equalsto"))).click();
  String j = driver.findElement(By.id(obj.getProperty("result"))).getAttribute("value");
  System.out.println(obj.getProperty("nine")+" - "+obj.getProperty("three")+" = "+j);
  }
}

Run above given example In your eclipse and observe execution.

Now supposing I have many such test cases and some one has changed Id of any button of calc application then what I have to do? I have to modify all test cases? Answer Is No. I just need to modify objects.properties file because I have not use element's Id directly In any test case but I have Used just Its key reference In all test cases.

This way you can create object repository of all your software web elements In one .properties file. You can VIEW MORE TUTORIALS about selenium webdriver.

10 comments:

  1. Thanks a lot for this great sharing :)

    ReplyDelete
  2. thank yo...waiting for Day 10 tutorial

    ReplyDelete
  3. I like this tutorial too. Great job. Thank you.

    ReplyDelete
  4. Properties obj = new Properties();
    FileInputStream objfile = new FileInputStream(System.getProperty("user.dir")+"\\src\\ObjectRepo\\objects.properties");
    obj.load(objfile);

    is there anyway to eliminate above 3 lines for every single test? If we have 100 tests, that's 300 lines on repeated code. Please let me know.

    Thanks

    ReplyDelete
  5. @Shiraz i dont know how much this answer will help but i think you can create one method in separate class which will read the properties file i.e. three lines and you can call that method whenever you need it, then it will be 100 lines for 100 test cases.
    If I answered your que?

    ReplyDelete
  6. The calculator page is buggy. Whenever I pressed "4" it displayed "5"
    Anyway, thanks for the tutorial

    ReplyDelete
  7. Hi,

    I have tried to run this, but the test fails at the first stage... Cannot locate first element (eight). If I try to locate the element by id driectly from my console, it works.. Just not via the object file.. Any suggestions?

    Thanks
    S

    ReplyDelete
  8. Hi,

    This does not work for me.. When I run the test it fails on first step. Cant locate first element (eight). However if I find the element directly from my test - it locates it. Only fails finding via object file. Any suggestions?

    Thanks
    S

    ReplyDelete
  9. Hi ,

    How to use properties file for multiple classes ,ii am using testng for creating scripts.Please suggest.

    ReplyDelete