While building Automation Framework, we need to deal with different data related to browsers, aut, grid, reports…etc.

You can make use of Java’s Properties file concept for the above data which doesn’t need to be compiled for every change you make in the framework.

Note that we need to keep the data in Key: Value format in the properties file. Each key and value should be on one line.

list of classes & methods we’re going to use from Java is mentioned below.

Properties Class

The Properties class represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream. Each key and its corresponding value in the property list is a string.

Read more about Properties class from Java Official Docs Here.

load( ) :

properties load method

getProperty( ) :

Properties getProperty method

FileInputStream Class

A FileInputStream obtains input bytes from a file in a file system.
Read more about FileInputStream class from Java Official Docs Here.

Now we will see how to read any property value and also different ways to display all keys and values from any Property file.

Iterating properties using Enumeration

package readpropertyfile;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;

public class PropertyFileReader 
{

	public static void main(String[] args) throws IOException 
	{
		String filePath = "D:\\backup19420\\Keren_Tutes\\Explore Selenium\\config\\framework.properties";
		// initialize Properties class
		Properties properties = new Properties();
		// initialize FileInputStream class
		FileInputStream stream = new FileInputStream(filePath);
		// call load() from properties file and pass FileInputStream class object reference
		properties.load(stream);
		
		// print browser name from properties file
		String browserName = properties.getProperty("browser");
		System.out.println("Browser Name is:" +browserName);
		
		// Iterating properties using Enumeration
		System.out.println("******Iterating all Keys and Values from Property File using Enumarations******");
		@SuppressWarnings("unchecked")
		Enumeration<String> enums = (Enumeration<String>) properties.propertyNames();
	    while (enums.hasMoreElements()) 
	    {
	      String key = enums.nextElement();
	      String value = properties.getProperty(key);
	      System.out.println(key + " : " + value);

	    }

	}

}

Iterating Properties using For-Each Loop AND Properties’s stringPropertyNames() method

package readpropertyfile;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;

public class PropertyReader2 
{
	public static void main(String[] args) throws IOException 
	{
		String filePath = "D:\\backup19420\\Keren_Tutes\\Explore Selenium\\config\\framework.properties";
		// initialize Properties class
		Properties properties = new Properties();
		// initialize FileInputStream class
		FileInputStream stream = new FileInputStream(filePath);
		// call load() from properties file and pass FileInputStream class object reference
		properties.load(stream);
		
		// print browser name from properties file
		String browserName = properties.getProperty("browser");
		System.out.println("Browser Name is:" +browserName);
		
		// Iterating Properties using For-Each Loop AND Properties's stringPropertyNames() method

	    Set<String> keys = properties.stringPropertyNames();
	    for (String key : keys) {
	      System.out.println(key + " : " + properties.getProperty(key));
	    }
	}
}

Iterating Properties using For-Each loop AND entry set

package readpropertyfile;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

public class PropertyReader3 
{
	public static void main(String[] args) throws IOException 
	{
		String filePath = "D:\\backup19420\\Keren_Tutes\\Explore Selenium\\config\\framework.properties";
		// initialize Properties class
		Properties properties = new Properties();
		// initialize FileInputStream class
		FileInputStream stream = new FileInputStream(filePath);
		// call load() from properties file and pass FileInputStream class object reference
		properties.load(stream);
		
		// print browser name from properties file
		String browserName = properties.getProperty("browser");
		System.out.println("Browser Name is:" +browserName);
		
		//Iterating Properties using For-Each loop AND entry set
		Set<Entry<Object, Object>> entries = properties.entrySet();
	    for (Entry<Object, Object> entry : entries) {
	      System.out.println(entry.getKey() + " : " + entry.getValue());
	    }
	}
}

Iterating properties using Java 8 forEach

package readpropertyfile;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class PropertyReader4 
{
	public static void main(String[] args) throws IOException 
	{
		String filePath = "D:\\backup19420\\Keren_Tutes\\Explore Selenium\\config\\framework.properties";
		// initialize Properties class
		Properties properties = new Properties();
		// initialize FileInputStream class
		FileInputStream stream = new FileInputStream(filePath);
		// call load() from properties file and pass FileInputStream class object reference
		properties.load(stream);
		
		// print browser name from properties file
		String browserName = properties.getProperty("browser");
		System.out.println("Browser Name is:" +browserName);
		
		//Iterating properties using Java 8 forEach
		properties.forEach((key, value) -> System.out.println(key + " : " + value));
	}
}

Important Points to Note

  • A properties file is used as configuration file in java application, which stores key and its corresponding value.
  • You can use CSV or XML files to store your framework configuration data.
  • Keeping your web elements inside your properties file is a bad idea.
  • You can do changes to your properties file without compiling your code.