Selenium WebDriver DDF : Creating Object Repository To Store Element Objects

In Selenium WebDriver software test data driven framework creation STEP 26, We have Implemented simple calc software test. Now In this step, We will create object repository using .properties file to store software web page element's locators like xpath Locator, cssSelector Locator, className Locator, id Locator, linkText Locator, name Locator,
partialLinkText Locator and tagName Locator. We can call It as object repository because we are storing objects In that .properties file.
STEP 27
Prerequisite : All the previous 26 Steps of selenium webdriver DDF creation should be Implemented. You can get links of all previous steps on THIS PAGE.

Why need to create object repository?
Supposing you have 100 software test cases and there Is one button or text box or any other element which Is used In 50 software test cases. Now for some reason, XPath or id or cssSelector locator of that element Is changed on software web page which you have used In 50 test cases. In this situation what you will do? You will go to change element's locator In all 50 test cases? It Is just waste of time and It will create many complexity and errors In code.

Best way to handle this situation Is creating object repository. As described In first paragraph, Object repository will store the element objects. So you need to store all the element's objects In single .properties file and then you can use It's key In your test cases wherever required. Now If above scenario happens then you have to modify your element's locator In .properties file only.

How to create object repository
For creating object repository, You have to Insert new file Objects.properties under com.stta.property package to store element's locators.

How to call object from Objects.properties file
First of all, We have to Initialize Objects.properties file before test case execution. So we will Initialize It Inside init() function of SuiteBase.java file. And then we can call any object Inside our test case using It's key.

Example :
Supposing, there Is one button +(plus) In calc application whose XPath Is "//input[@id='plus']". So you can store It In Objects.properties file as bellow.

btn_Plus=//input[@id='plus']

Here btn_Plus Is the key of that XPath. So now If you wants to click on that button In your test case then you can use bellow given syntax In your test case.

driver.findElement(By.xpath(Object.getProperty("btn_Plus"))).click();

This Is Just example, Same way you can call all other required objects In your test cases from Objects.properties file.

Download required files
You need modified Objects.properties, SuiteBase.java and SuiteOneCaseOne.java files. Download bellow given zip folder to get all these files.
Add/Replace files as bellow
  • Add Objects.properties Under com.stta.property Package
  • Paste SuiteBase.java File on com.stta.TestSuiteBase Package.
  • Paste SuiteOneCaseOne.java File on com.stta.SuiteOne Package.
Now your project structure will looks like bellow.

I have added comments In SuiteBase.java and SuiteOneCaseOne.java files to understand code better.

Run test suites and observe results
Run your test from Shortcut to Run Test.bat file. Now SuiteOneCaseOne.java will read element's objects from Objects.properties file.

What we have achieved In this step
We have created object repository for our selenium webdriver data driven framework. So from now onward, we will store all our objects In Objects.properties file. Benefits of creating object repository In webdriver DDF are as bellow.
  • Easy to maintain the software web page's objects elements.
  • Decrease the size of code.

4 comments:

  1. Hellp,
    I'm working on Keyword Driven Framework on Eclipse using Java. I'm having a problem with object repository has long xpath like this.

    btn_Logout=/html/body/div/table/tbody/tr/td[2]/table/tbody/tr[2]/td/table/tbody/tr/td[1]/a

    is this correct?

    ReplyDelete
    Replies
    1. If you are using .properties file, then it is correct way of declaring the locator.
      If you are using .java fie to store the locators then you have to mention as follows
      String = "";
      ex: public final String btn_LogOut = "/html/body/div/table/tbody/tr/td[2]/table/tbody/tr[2]/td/table/tbody/tr/td[1]/a";

      I hope your issue solved

      Delete
  2. Hi, Can you please explain how to use/call and load selenium webdriver methods in the script ? Please provide some sample code. Please answer my question.

    -Jia

    ReplyDelete
  3. Hi, Can you please explain how to use/call and load selenium webdriver methods in the script ? Please provide some sample code.

    -Jia

    ReplyDelete