Loading a properties file

In automation, there are times when the properties used for setting up the test environment change from test to test. To avoid hard coding and minimize the test setup time you can use a properties file.

The first step is to create a .properties file in your project and insert the desired values.

properties_file

 

 

 

 

You can load the properties file with the following method:

public Properties prop = new Properties();

public void loadProperties() {
 InputStream input = null;
 try {
 input = DriverConfig.class.getResourceAsStream("testConfig.properties"); //in this case, DriverConfig is the class that contains this method and is in the same package as the properties file
 prop.load(input);
 } catch (IOException e) {
 e.printStackTrace();
 }
 }

// afterwards, use prop.getProperty($propertyName) to access the values

Add a Comment

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