Creating an object repository for Selenium WebDriver

Problem

It is very useful to extract the locators from the test code. This makes your test code much more maintainable and readable. Beside of that there is no need to recompile the project after changing the identifiers. There are many ways to do it, but we will see just one way in this example. It can be done by the Java properties file. Just load the file in front of running the tests and then you can use all the properties, that is basically everything what you have to do. The properties file is just flat text. That is why there is no need to recompile the whole source code once locators have been changed. We can also make use of the same locators in multiple test cases. These are big advantages of using a object repository.

Prerequisites

We can install Eclipse as preferred IDE to setup a Java test project.

Solution

  1. We have to create a properties file, given this name locators.properties. The part before the equals sign is the key, the part after the equals sign is the value. We can give the following content to the file:
login.username.textfield=css=input#username
login.password.textfield=css=input#password
  1. We have to load the properties file, before we can call the locators. We have to initiate the properties object and load the contents of the file in it. We can do this with the following code:
Properties props = new Properties();
try {
    InputStream in = getClass().getResourceAsStream("locators.properties");
    props.load(in);
} catch (IOException e) {
}
  1. We can use the object repository, once the key/values are actually loaded in the properties object. We can use the value in our tests, by calling the key parameter, like this:
props.getProperty("login.username.textfield")

What has been done

We are loading a key/value pairs into a properties object. Make sure you will do this in front of running any tests. Finally we are able to get the value by it’s key.

Source Code

The source code is available on GitHub at the following location: https://github.com/roydekleijn/HowToUsePropertiesFile

Creating an object repository for Selenium WebDriver
0 votes, 0.00 avg. rating (0% score)

6 thoughts on “Creating an object repository for Selenium WebDriver

  1. Hi Team,

    could you please let me know, where this locator.properties file need to be placed

    i placed in project directory path /projname/OR/locator.properties and also tried to keep in same package where source code present

    your quick response highly appreciated.

    • Hello Arun,

      Thanks for your question.
      Did you do a clone from GIT ? I just double checked and it is working for me.

      Please elaborate the steps you have taken.

  2. Does this properties file method support comments?

    Say for example that you have multiple versions of login.username.textfield and you are testing which is better. Rather than point to different property files or copy and paste new value each time to test, you just comment out one line and uncomment the other.

    If yes, what is the comment marker/indicator? A hash, double forward slashes, or semicolon?

    • Hello David,

      Thanks for your question!

      You can use comments in Java properties files. Please use the following syntax to comment rows:
      # a comment
      ! a comment

      Slashes are used to escape characters, such as quotes: \” \’

      I hope this helps,
      Roy de Kleijn

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>