Wednesday 13 August 2014

To Create Object Repository in Selenium





To Create Object Repository in Selenium

The main hurdle of automating any GUI project is its maintainability. The objects/ids/xpath keeps changing with new enhancements or code changes. Marinating the code in such circumstances becomes extremely difficult.
Object Repository: Object Repository is a place where we can store objects information, it acts as interface between Test script and application in order to identify the objects during the execution

Uses of Object Repository

1.      Object repository is a centralized location of the objects and their properties, so that if any object information changed in Application, you need not to change in all the scripts, it is enough to change in the Object repository
2.      Easily maintain your tests or components when an object in your application changes

Step by Step method to create object repository.

1.   Create a new project. Create a package under it. Start writing test cases under them.
2. Create a folder under that project. Name it as config. 

 

 

       3. Create a text file in that folder and rename that to objectrepository.properties.

 

 


4  4. Now identify the elements on the GUI. Find out their xpath/ids or names.

5. Classify them into
a)Links
b)Textboxes
c)Buttons
d)Dropdowns
e)Checkboxes

6.    Enter an object name for that element in the objectrepositories.properties file , this name will be the variable name used in our code.
Eg: demoapp_registerlink=html/body/div[1]/table/tbody/tr/td[2]/table/tbody/tr[2]/td/table/tbody/tr/td[2]/a
demoapp_fname_textbox=html/body/div[1]/table/tbody/tr/td[2]/table/tbody/tr[4]/td/table/tbody/tr/td[2]/table/tbody/tr[5]/td/form/table/tbody/tr[2]/td[2]/input




   7.  Now to retrieve the value of the key, you will have to write the following code.
To retrieve objects from our newly created object map, we will define an object repository with a constructor taking a single argument, which is the path to the .properties file:

Properties Object=new Properties();
      FileInputStream file=new FileInputStream("D:\\Workspace\\TestProject\\TestConfig\\Object.properties");
      Object.load(file);

8. The above code will load the properties file.
9. Now you can use the object created for retrieving the value of xpath stored in ObjectRepositories.properties.
10. Write the below line of code for directly using the value of xpath from ObjectRepositories.properties.

driver1.findElement(By.xpath(Object.getProperty("Cqube_login_btn"))).click();

11. The standard practice for naming  the object in ObjectRepositories.properties. should be applicationname_action_element.

12.Sample code

package TestPackage;

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

import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;


public class testobjectpro {
   
   
    @Test
    public void testobjectpro() throws Exception
    {
   
    Properties Object=new Properties();
    FileInputStream file=new FileInputStream("D:\\Workspace\\TestProject\\TestConfig\\Object.properties");
    Object.load(file);
   
    WebDriver driver1 = new FirefoxDriver();
   
driver1.manage().window().maximize();
    driver1.get("http://cqube.nihilent.com/");
   

    driver1.findElement(By.xpath(Object.getProperty("Cqube_login_btn"))).click();
   
   
}
}

13. Here xpath for Cqube_login_btn is defined in the file ObjectRepositories.properties.
Object repository maintenance

With this straightforward mechanism we have been able to vastly reduce the amount of time needed for script maintenance in case object properties change. All it takes is an update of the appropriate entries in the object map and we’re good to go and run our tests again.

 

 

 

1 comment: