Main menu

Building and using an Object Repository in Selenium Webdriver

One of the main burdens of automated GUI test script maintainability is the amount of maintenance needed when object properties change within the application under test. A very common way of minimizing the time it takes to update your automated test scripts is the use of a central object repository (or object map as it’s also referred to sometimes). A basic object repository can be implemented as a collection of key-value pairs, with the key being a logical name identifying the object and the value containing unique objects properties used to identify the object on a screen.

Selenium Webdriver offers no object repository implementation by default. However, implementing and using a basic object repository is pretty straightforward. In this article, I will show you how to do it and how to lighten the burden of test script maintenance in this way.

Note that all code samples below are written in Java. However, the object repository concept as explained here can be used with your language of choice just as easily.

Creating the object repository
First, we are going to create a basic object repository and fill it with some objects that we will use in our test script. In this article, I am going to model a very basic scenario: go to the Bing search engine, search for a particular search query and determine the number of search results returned by Bing. To execute this scenario, our script needs to manipulate three screen objects:

The textbox where the search string is typed

The search button to be clicked in order to submit the search query

The text field that displays the number of search results

Our object map will a simple .properties text file that we add to our Selenium project:The key for each object, for example bing.homepage.textbox, is a logical name for the object that we will use in our script. The corresponding value consists of two parts: the attribute type used for uniquely identifying the object on screen and the corresponding attribute value. For example, the aforementioned text box is uniquely identified by its id attribute, which has the value sb_form_q.

Retrieving objects from the object repository
To retrieve objects from our newly created object map, we will define an ObjectMap with a constructor taking a single argument, which is the path to the .properties file:

You can see from this code sample that using an object from the object map in your test is as easy as referring to its logical name (i.e., the key in our object map).

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.

Thanks to Selenium Master for explaining this concept clearly for me to apply.

An example Eclipse project using the pattern described above can be downloaded here.

an Object Repository like this should be equally easy to use if you’re using the Page Object Model pattern. However, I think it’s a little less useful in that situation as you already optimize the number of times a reference to an object on a page is made. If you’re using POM and you’re still referencing the same button, link or whatever in many methods, then using an Object Repository might still be handy.

I’d have to look deeper into the combination of a PageFactory and an Object Repository, but as objects in the PageFactory are initialized differently I don’t think the two can be combined as easily..

I have a unique application where there are a lot of controls but they can easily be classified by type (single-select, freetext), so I create the selectors for it dynamically. I just use a keyword to match the element name, and list what of control it should be, and then I have a class that is able to interact with different controls… such as with single selects, it can select from a drop down. Our application has custom dropdowns, etc and the typical webdriver stuff doesn’t work. So I guess I’m saying that this works for certain scenarios, but not if your app would need like thousands of selectors. Then you gotta get creative. This would be a good option for smaller, less complex apps.

thanks for sharing your insights. I wholeheartedly agree with what you said. Using an object repository is just one approach to object identification and administration, and one that is more suitable for those applications that do not have a lot of objects to interact with. I can see it getting messy when you have a couple of hundred of objects..

I only recently learned about using the PageFactory approach myself and will probably write something about it in a future post. This is another approach to object identification, and one you might be familiar with already. It makes object identification manageable as well, even though I can see problems for dynamic pages that have a lot of objects in this approach too.

Your solution sounds pretty creative and well suited to your situation as far as I can judge from your explanation. Out of curiosity, what type of selectors do you use? XPath or CSS?

Hi Bas,
Let’s consider an object repository as a row of books in a library.so each book points to an element in the website we need to be tested.so in future if there happens to be a change in the website suppose an element is added or deleted or gets changed we can manipulate it in the object repository.Is this the idea?
otherwise please explain.

That is exactly the idea! Also, if you have 30 tests referencing the same element and the ID property (or any other property you’re using to identify the object) of that element changes, you only need to update it once, instead of 30 times.

I must say that in practice, the Page Object design pattern in combination with a PageFactory is probably used more often.

Hi Bas,
I downloaded your script which contains three java pages-SeleniumFramework.java,ObjectMap.java and
SeleniumProperties.java.Also two object Repository-objectmap.paroperties and selenium.properties.But how does this work?which file to run first?The file containing Main() function?

Hi Bas,
I found a video that contains the Page Object design pattern in combination with a PageFactory that you said here i guess.
Check this video-http://executeautomation.com/blog/page-object-model-in-selenium-part-1/

I think if i follow the pattern shown in the video that is separating objects from tests and write the code plus if i use ExtentReport to generate the report,maybe i can automate a full website right?Is the right approach to build framework?Does this avoid redundancy,duplication and all?

Thanks for providing this, but I believe I’ve run into an issue. I use XPaths to locate my elements, and within some xpaths, 2 consecutive colons are used (i.e. xpath://div[@class=’contactus’]/parent::div).

In these cases, I think the following piece of your code is shortening my xpath:

You’re absolutely right, using a colon as a separator character won’t work when using xpaths that contain semicolons. A simple workaround would be to use another separator character, one that does not occur in xpath or any other locator. Maybe a semicolon would be a good candidate? Don’t forget to change the split() call as well.

Hi, thanks for the quick reply. I did try using unique characters for the split() calls, but they didn’t work (I used ” | ” and ” ^ ” characters, and separated my xpaths with those characters accordingly). However I found a workaround, using 3 colons does work.

You could always try and right-click the button in Chrome and choose ‘Inspect’ to see its properties. You can also directly copy an XPath locator from there, although it’s probably an ugly one. Without access to the application I can’t help you further I’m afraid..

Hi Bas,
1. Can you guide me on how to to validate expected and actual value in selenium. Is there a generic method that I can use.
2. Is there a way where I can use your Object Repository file (.properties) to store the expected values for various fields (ex:dropdown, multiselect checkbox) so that it is easy to maintain.

I am trying to validate various fields and its values on a salesforce webpage.

Hi, Just a suggestion.
We can use Switch case for “public By getLocator(String strElement) ” method. since if else logic will traverse through all the code where as switch case will directly go to the desired Locator type.
Cheers.

Hey Bas,
Thanks for such an excellent article.In one of your earlier comments,you mentioned that you moved on from storing objects in properties file.Is there a better way to maintain page objects.Does storing objects in a JSON file makes it easier to understand ?Please advise.

After reading your article and comments above i understood maintaining object repository from properties file with object map is not a good idea implementing using pom & page factory options are the best.

Can you please help me on this scenario like testing the application with multiple countries and different languages.

Since i already implemented my framework with POM and page factory for one country and want to convert this framework to multiple countries and languages.
With out object repository how can we achieve this using pom and page factory.

POM is a lot of overhead to maintain as the suite gets larger and once pages start sharing elements and functionalities, which is basically every web application out there. Many times automation testers find themselves maintaining POM instead of actually writing tests. Let’s also admit that most automation testers are not developers, making POM notorious for bad implementations. Partly the limitation of the tester but also alludes to the fact that it has a steep learning curve and not tester friendly – this coming from a Java developer like myself. I’ve worked with it long enough to understand it’s a maintenance chore nobody wants to admit because everyone else is using it. POM with Cucumber is the WORST as step definitions/methods are already independent units on their own. POM is also AUT-specific, which makes it unusable in other projects. Any change of the elements requires a recompile of the .java file.

POM is an inflexible and rigid design pattern that builds technical debt over time and bloats the codebase. I have seen suites with 50+ page objects and it is an absolute nightmare. You will need developers to continue it in the long run, not testers.

UI map/object repo is a good compromise of maintainability, learning curve, and delivery. And this article outlines a good approach to it. At the end of the day, it’s really the completed tests that matter to the stakeholders.

Hey Kat, thank you for your insights. I tend to use and teach Page Objects regularly, but as you say, they do have their downsides as well. It really depends on the situation whether or not it is a useful pattern.

Also sorry for taking so long to approve your comment and reply to it, I blame holiday!

No problem! The UI map definitely still has its place as does this fine article you wrote. The UI map can scale from medium-large projects – which is usually enough and there’s a faster ROI to it. Companies like to think they’re “big data” but most of the time, they’re really not haha.

A framework is only as good as its maintainability and if the framework cannot live beyond its creator, it’s as good as dead – as does any software. Hence why there is no one-size-fits-all design pattern. 🙂

By the way, the StringUtils from Apache Commons Lang has some really splendid methods for splicing/manipulating Strings like subStringAfter() and subStringBefore().

About me

Hi there! I'm Bas, a test automation trainer and consultant always looking for more intelligent ways to use tools to support testing. I'm sharing my experiences and thoughts here, so you can benefit from them too!

Agenda

I will be facilitating a full day workshop titled ‘Investigating the context – How to design an effective automation strategy’. More information about the event can be found here.

Test Automation Days
June 19-20, 2019 (Utrecht, NL)

I will be facilitating a half day workshop titled ‘Patterns and principles over tools and tricks:
applying the pillars of object oriented programming to your test automation code’. More information about the event can be found here.

I’ll deliver this course in public in collaboration with Black Koi Consulting. See here for more information and registration.

Agile & Automation Days
October 28-29, 2019 (Gdańsk, PL)

I have the honour of delivering a keynote titled ‘Test automation: Put on the brakes so you can go fast’ at this conference. Next to that, I will also be delivering a full day tutorial on API test automation and service virtualization.