Table of Contents

An open source Java library which can be used to serialize and deserialize Java objects to (and from) JSON.

JSON is Java Script Object Notation, an open standard format that uses human-readable text to transmit data objects consisting of attribute-value pairs.

Features:-

  1. Compact readable output
  2. Null object fields handled by default- by default, they are not present in the output
  3. Page locators hierarchy creation is very easy
  4. Page locators inheritance can be easily defined in file
  5. Traversing JSON OR locator file for any locator address is less cumbersome
  6. No need to invest time in explaining OR locator JSON file to anyone, it is so much easily readable that anyone can predict the page locator configuration or locators sequence on page

In this chapter, we will learn How to create Object Repository for Selenium using JSON. Let’s create one JSON file as an example for the sample small section, snapshot below:-

Json_1

Step 1 : Creating OR for above section of the Page

  • Naming convention : "ObjectRepository.json"

Json_2

  • Highlights

    • {} --> Start and End for the file
    • "HOME" --> Key/Element Name on page which holds elements Locator address
    • : --> Separator between Key and Locator Value
    • "XPATH;home" -- > Locator value with locator identifier and “;” delimiter between identifier and locator value
  • "TRAININGS" section with sub menu

    • "Menu_TRAININGS" --> Key which signifies Training is also have menu
    • “SubMenu” --> Key which signifies start of the “TRININGS” sub menu Key’s
    • “SELENIUM” --> Key which is a SubMenu of Menu “TRAININGS”
    • “APPIUM” --> Same as of “SELENIUM”
  • Advantages

    • Creating Parent Child relationship between page elements
    • It is easy to traverse huge OR
    • Tester can create inheritance according to the Page Rendering or Page View
    • Easy to divide Page Sub sections with some significant names
    • Can traverse easily for any locator if any error found from Bottom to Top or Top to Bottom in ease

Step 2 : How to fetch Key/Element locators address values in JAVA code

  • Import “json-path-0.9.0
  • Import “json-path-0.9.0-dependencies
  • Snapshot below :-

Json_3

  • Declare File variable for JSON file

Json_4

  • Initialize “jsonfile” variable with the created JSON file for OR

Json_5

  • Just, start fetching the data from OR

Json_6

  • read(jsonfile,"$."+"HOME")
  • Pass file variable initialized “jsonfile” with “$” in combination with Key for which the locator value to be fetched like for example “HOME

"Menu_TRAININGS.SubMenu[0].SELENIUM", “SubMenu” is an array with index “0*” and this holds Keys of sub menu like “SELENIUM” & “APPIUM

Step 3 : Output of the above code is

Json_7

  • CSS;tutorial --> Reflects that for “TUTORIALS” key, locator we are using is “CSS” and the value of the CSS is “tutorial”, and this “tutorial” will be replaced by actual CSS of Key on page.

At last we can say that, you just need write small code for traversing this output string through which you can divide the locator identifier and locator value using delimiter “;” or any of your choice which is to be passed to “driver.findelement(BY.locator identifier(locator value));”.

HAPPY LEARNING AHEAD……………..! Ankur

Object Repository in Selenium
Object Repository in Selenium
Previous Article
Page Object Model using Page Factory in Selenium WebDriver
Page Object Model using Page Factory in Selenium WebDriver
Next Article
Lakshay Sharma
I’M LAKSHAY SHARMA AND I’M A FULL-STACK TEST AUTOMATION ENGINEER. Have passed 16 years playing with automation in mammoth projects like O2 (UK), Sprint (US), TD Bank (CA), Canadian Tire (CA), NHS (UK) & ASOS(UK). Currently, I am working with RABO Bank as a Chapter Lead QA. I am passionate about designing Automation Frameworks that follow OOPS concepts and Design patterns.
Reviewers
Virender Singh's Photo
Virender Singh

Similar Articles

Feedback