How to read data from properties file in automation frameworks

Ishan mittal
2 min readMay 24, 2024

--

Test configuration is one of the most important part of any test case weather its manual or automation.

When we work in automation , there are so many configurations (like browser name, browser version, baseUrl, remote, parallelThreadCount, global timeouts etc.) which are to be set as global data for running suites.

There are different ways to saving this data in the framework:

  1. Making it hardcoded in the steps (not recommended)
  2. Using Excel file
  3. Using Xml file
  4. Using .properties files (Most of the time , people use this method for storing configurations)

Here in this article i am going to show case the best practices to read data from .properties files.

Lets create on properties file with some global config parameters (global.properties)

global.properties

Approach 1:

Create one Singleton java file eg: ConfigReader.java . Create method to load all properties.

public class ConfigReader {

private ConfigReader(){
}
private static ConfigReader configReader;
private static Properties properties = new Properties();
private static final String propertyFilePath= "src/test/resources/configs/global.properties";


public static synchronized ConfigReader getConfigReader(){
if(Objects.isNull(configReader)){
configReader = new ConfigReader();
configReader.loadProperties();
}
return configReader;
}

private void loadProperties(){
final Properties props = new Properties();

BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(propertyFilePath));
try {
props.load(reader);
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException("Configuration.properties not found at " + propertyFilePath);
}
properties.putAll(props);
}

public String getProperty(String propertyName){
return properties.getProperty(propertyName);
}
}

Here the instance of class is created when its called.This approach is called Lazy initialization. Now you can use this getProperty() method through out the framework. You can use BufferReader or FileInputStream.

String browserName = ConfigReader.getConfigReader().getProperty("browser")

Approach 2: Using static Block

Create one Singleton java file eg: PropertiesReader.java . Create one static block to load all properties.

public class PropertiesReader {

private PropertiesReader() {
}

private static Properties properties = new Properties();
private static final Map<String, String> propertiesMap= new HashMap<>();
private static final String propertyFilePath = "src/test/resources/configs/global.properties";


static {
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(propertyFilePath));
properties.load(reader);
reader.close();
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
propertiesMap.put(String.valueOf(entry.getKey()), String.valueOf(entry.getValue()));
}
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Configuration.properties not found at " + propertyFilePath);
}
}

public static String getValue(String key) throws Exception {
if (Objects.isNull(key) || Objects.isNull(propertiesMap.get(key))) {
throw new Exception("Please provide valid key or check properties file");
}
return propertiesMap.get(key);
}
}

Here all the data will be loaded in static block and its called Eager initialization. Now you can use this getValue() method through out the framework. We are using hashmap here so this approach is little faster than 1st one.

String browserName = PropertiesReader.getValue("browser")

Hope this article will help you to solve your problems associated with data loading from properties files.

--

--