Hi all, in this article I will explain to you how to read configurations from a property file which holds global configurations and variables. You can add a utility class to handle this operation. You can name is PropertyReader, ConfigReader, PropertyManager, PropertyReader, etc. Let’s go on to learn how to do this.

First, I want to show you my project structure, it is a project which is written based on Page Object Model and in utilities package, I added a PropertyManager class to read configurations.properties file, get the global variables and configurations. I hold the configurations and variables in configurations.properties file.

configuration.properties File:

Property Manager Class:

Here, I used singleton design pattern to create the PropertyManager class only one time. In this way, we read and load the configuration data only one time. After the first call, we get the PropertyManager instance/object via getInstance() method.

loadData() method reads the configuration.properties file and assign the variables to the private variables. Lastly, we have getter methods to reach the variables such as getURL(), getWrongPassword(), getWrongUsername().

Now, we can use PropertyManager class in our test or page classes. I will show how its usage below:

In HomePage Class, we got the URL value from configurations.properties file by using below line:

String baseURL = PropertyManager.getInstance().getURL();

Also, you can use PropertyManager in test classes as shown below:

String wrongUsername = PropertyManager.getInstance().getWrongUsername();

String wrongPassword = PropertyManager.getInstance().getWrongPassword();

Here is the project code: https://github.com/swtestacademy/configreaderexample


There’s another library that you can use for read and write from a property file. In case you ever tried writing or updating a value in property file, all other properties are deleted in case you use java.util.Properties. There’s no way of updating a value without deleting the other entries.

In order to be able to achieve that challenge, you need to use a 3rd party library provided by Apache foundation. There might be other ones, but this is the one that we use in our projects right now.

After adding this dependency, logic is very simple. you need to create a FileBasedConfigurationBuilder object that’s bind to a properties file. Then you can read value by using getString(), getInt(),..  functions. There are many functions for different type of objects. But setting a value is much more straight forward. You just need to use setProperty as there are no different object types in properties file.

At the end of the operation, you just need to save the file by builder object.

Here’s a sample code.


Onur Baskirt