Implementing Object Repository in Selenium

Selenium (http://seleniumhq.org/) has now emerged as one of the top contenders to take on QTP in the test automation tools space. Our teams at Zen Test Labs (www.zentestlabs.com) were one of the early adopters of this automation tool and have built up a decent level of expertise in automating test scripts using selenium. We recently also presented a tutorial on this topic at STARWEST, California in October 2011.

While both tools have their advantages and disadvantages, object repositories are one area that I have found to be important but not supported by Selenium. An object repository is a centralized place for storing properties of objects available in the application under test (AUT) to be used in scripts. QTP comes with its own object repository where one can record and store objects.

Over the course of a recent project I have tried to implement object repository in and it worked really well. I am listing below, how I went about doing this.

1. Create an interface of a name you want to give to your object repository as follows,
Interface ObjectRepository
{
// Now here you can store properties of the object in variable
Static String ddCategory = “id=ctl00_MainContent_ddlCategory”;
Static String btnSave = “”id=ctl00_MainContent_btnSave””;
}

2. Implement the interface into your class and you are good to use objects in your functions
Class TestLibrary implements ObjectRepository
{
public String testFuntion()
{
//here you can your object to perform actions or validations
selenium.select(ddCategory, “Testing”);
if (selenium.isElementPresent(btnSave)
selenium.click(btnSave);
}

}
In the above example, I have implemented it in java and have also tried it in C#. Thus, using OOPS concepts I feel that this can be implemented in any object oriented language supported by Selenium.
What is important to note is that one can also connect to excel or any database and store values. Thus in the event of changes in the application or changes in properties of any object; a simple change in the excel file or DB will reflect across all instances of the object.

Some of the advantages of doing this include:

1. Easily maintain your tests or components when an object in your application changes
2. Manage all the objects for a suite of tests or components in one central location
3. Code becomes more readable & easy to write when user defined objects name are used instead of complex and long property name & value.

These have been some of the approaches that have worked for me and my learning’s. Would be great to get to know other people’s experiences in implementing object repositories within Selenium

Hemant Jadhav | Senior Consultant | ZenTest Labs

Advertisements

8 thoughts on “Implementing Object Repository in Selenium

  1. Pingback: About Me | Hemant Jadhav

      • Yes please. I have a couple of questions. I’m new to the Selenium world, and I’m coming from QTP background.

        I’d like to know how does this compare to Page Object Model? Is this approach better than that? Also, can this be implemented in mobile app testing (web / native app)?

      • Hi Abhi,
        That’s a very good question. This is just some sample code to demonstrate how I implemented an Object Repository in Selenium. Yes, you can use Page Object Model in Selenium. It’s better and easy to maintain. I do not have any experience in mobile app. testing, but I believe this approach can be used for it.
        Warm Regards,
        Hemant

  2. Great stuff! Thanks!
    You can also go for a neat .properties file where to store all your locators and/or PageObjects.

    I like to have my “OR” in the page’s own PageObject. This way you avoid code repetition and you
    have all your page’s details (locators, web elements, user actions, sync, aso) in the same place.

    In the PageObject class:
    public class DashboardPage{
    // [some code… ]
    @FindBy(how = How.CSS, using = “.main-menu-item>a[title*=’Dashboard’]”)
    private WebElement dashboardTab;

    @FindBy(how = How.CSS, using = “#global-nav-courses”)
    private WebElement myCoursesTab;

    @FindBy(how = How.CSS, using = “.main-menu-item>a[title*=’Reporting’]”)
    private WebElement reportingTab;

    public void clickDashboardTab() throws IOException, InterruptedException {
    logger.Log(LOG_FILE, “Clicking on <> tab link …”);
    new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOf(dashboardTab));
    dashboardTab.click();
    }
    public void clickMyCoursesTab() throws IOException, InterruptedException {
    logger.Log(LOG_FILE, “Clicking on <> tab link …”);
    new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOf(myCoursesTab));
    myCoursesTab.click();
    }
    public void clickReportingTab() throws IOException, InterruptedException {
    logger.Log(LOG_FILE, “Clicking on <> tab link …”);
    new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOf(reportingTab));
    reportingTab.click();
    }
    // [some code… ]

    In the Test Class:

    @Test
    public void testDashboard(){
    // [some code… ]
    dashboardPage.clickDashboardTab();
    // [some code… ]
    dashboardPage.clickMyCoursesTab();
    // [some code… ]
    dashboardPage.clickReportingTab();
    // [some code… ]
    }

    More info on that here: http://blog.cloudtroopers.com/content/coding-selenium-2-webdriver-test-scripts-page-objects

  3. I’m curious why an interface is being used to store static information, as opposed to a .properties file or a regular class file containing constants. I worry that this approach is an abuse of the Interface mechanism and could lead people astray in good OO design.

    • Hi Darren,
      Thank you for your inputs. Yes, I agree with you. Using .properties files is always a better approach. We generally use .properties, XML or Excel files to store object properties, which also makes scripts maintainable. This blog post was written when I had started exploring Java and Selenium as a beginner.
      Warm Regards,
      Hemant

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s