I'm aware and have also used two ways of storing the WebElements/locators (a.k.a Object Repository)

  1. External File (example: .properties , .yaml, .xml, etc...or any other file type)
  2. Using page factory inside a Page Class (POM)

My question is are there any other way/approach used to store the Object Repository(Locators).

For example: Is it advisable to store all the locators(as instance of By Class) for a web application in a class(lets call this class as ObjectRepository) and directly inherit or call those locators in our Test Class.

Sample Code Below:

public class ObjectRepository {

    //locators for login page
    public By byUserNameTextboxID = By.id("username");
    public By byPasswordTextboxID = By.id("password");
    public By byLoginButtonName   = By.name("submit");

Edit 1: Current Framework is something like this.

(Class on the Left is inherited by Right)

SeleniumWrapper Class >> ObjectRepositoryLoginPage Class >> TestLoginPage

SeleniumWrapper Class - Holds all the selenium methods in custom methods Example:
public void click(By by) { driver.findElement(by).click(); }

ObjectRepository Class - Holds all the WebElements as a object of By Class Example:
By byUsernameTextboxID = By.id("username");

TestLoginPage Class - Contains methods related to testing the LoginPage. Example:

  • So if you have 1000 locators in total, why would you inherit 1000 class variables in each page class? Where each page class might require just 20-30 variables(locators). Mar 5 '18 at 12:35
  • @VishalAggarwal agree. Would you suggest a separate ObjectRepository for each class.For example: ObjectRepositoryForLoginPage and ObjectRepositoryForCustomerPage My intention of asking the (original) question is becoz I am facing a lot of problem in maintaining the pageFactory in my POM framework. Thanks.
    – Alok
    Mar 6 '18 at 10:17
  • There is no 'one shoe fits all' solution , every solution depend on given situation which I don't know in your case.I think it would be more beneficial if you share specific details about your implementation and ask about specific problems you are facing in maintenance of page factory. Mar 6 '18 at 10:29
  • @VishalAggarwal I have updated the question to add description of the current framework that I am using. Hope this helps. Thanks.
    – Alok
    Mar 9 '18 at 4:47
  • Alok, I would have one page class per r each page in the application which will encapsulate objects and methods related to that page. All the methods would be high level application level service methods like Login, Logout, Place order, CancelOrder instead of UI level like clickButton. Also I would not have separate Object Repository class. Mar 9 '18 at 10:40

If you are up for using Ruby say, you could:

store the name and locators as key-value pairs in a .yml file

Create a Page Object class with an initialize method that read the YAML file and dynamically adds the methods to the new instance of the class.

Make the initialize method go through each line of the file and dynamically add the method to the class.

Now your instance of PageObject will have these available as methods.

Code samples:

page_object.rb class PageObject def initialize load_page_object_file('spec/support/page_objects.yml') end private def load_page_object_file(file) page_object = YAML.load(File.read(file)) page_object.each do |k, v| define_singleton_method(k) do v end end end end

... primary_phone: 'input#PrimaryPhone' title: 'div.title-module' last_name: 'StudentLastName' ...


within a spec describe "stuff" do ... p = PageObject.new it "stuff" do fill_in p.last_name, with: 'testLast' fill_in p.title, with: 'MR' fill_in p.phone, with: '6175551212'

  • Hi Thanks...for the answer...I wanted to know if there is another way to store locators other than the external files(ex: .properties, .yml, etc.). sorry if I confused you. I have updated my question.
    – Alok
    Feb 19 '18 at 4:21
  • If you don't have external files you either have them at the bottom of the test file (but how to share?) or you have them inline (hard to maintain consistently and how to share across test files? Either way won't work well for most suites. Feb 19 '18 at 11:36

The another way of using object repositories in Selenium webdriver other than properties file is through XML file. Properties file is a text file where the data are stored in the form of key-value pairs whereas in XML file data can be stored in the form of DOM(Document object model).

  • Thanks shanila, as I mentioned in the question I am looking for any other approach other than POM and External files.
    – Alok
    Feb 22 '18 at 6:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.