Read/Write Operations for Properties file in Automation Framework

This is another tutorial in the marathon series of tutorials on Parameterization in Selenium WebDriver. We are going to discuss read/write operations of properties file in Selenium. This is very much helpful while designing the Automation Framework. This file has the similar advantage of maintaining data in HashMap.

Why should I maintain Properties file in Automation framework?

There are many significances of maintaining a configuration file (as properties file) in Automation framework. Some of the significances are listed below:

  • It works like the HashMap by holding key-value pairs
  • Ease of portability
  • Easy to read and write data
  • Good options to maintain object repository
  • Coding is clean and simple
  • It does not take much space, hence, smaller file size mainly in KB
  • Less complex
  • Very rear chances of the file getting corrupted

Automation Framework Design Hack (Parameterization Hack)

In the last tutorial, we discussed storing excel data to the HashMap. Right?

If you see complexities in adding test data from Excel sheet to the HashMap then you can use this technique as the alternative option. Simply, instead of storing test data in HashMap, you can store them in the properties file. It’s quite simple. It stores data in the key-value pairs itself.

I have some suggestions for you, please go through the below tutorials if you want to learn more on parameterization:

Class instantiation

We need to first import class.

Once we imported then we need to create an instance of the Properties class.

How to write data to the Properties file?

After creating the instance of the class, you can either set String for both key and value pairs or you can set both arguments as Object. It depends on your coding strategy. Here is the sample code to write data to the properties file.

As per above code, we are first creating the file then we created the instance of Properties class. After that, we set the property by calling the method setProperty(String arg, String arg). Further, we passed the file argument to the FIleOutputStream then we called the store() method to store the property in the file.

Properties file

It is good to store data at runtime in a property file. We can even use this technique to store data from the excel sheet to later retrieve in key-value pairs when needed.

How to create configuration file in Eclipse?

We basically maintain a configuration data like URL, Path, Constants and some other attributes which we basically use them at runtime. This is good and efficient to maintain suck type of data in the lightweight file.

Here are the steps to create configuration file.

Step 1: Right click on the folder or go to File New->File

Properties file from New file


Step 2: Give then click Finish

Create properties file

How to read data from the configuration file?

Following code read data from the configuration file.

In this code, we first defined the path of the file then we created the instance of the Properties class. Further, after loading the file we used getProperty() method to retrieve the value.

Here is the console output.

Properties file console

Join our Facebook group for more discussion on Automation testing.

Join Inviul fb group

Leave a Reply