How to read properties file in selenium (java)?

Working with properties file in java / Selenium – 
Hard coding is not a good practice anywhere. It’s hard to go back and change something you’ve hard-coded previously in your code. Suppose you are testing some application. Consider that, its URL keep changing environment to environment, like in UT, UIT, ST, SIT, UAT etc.
Now, your application will be same but URL only will be different.
In this case if you are hard coding URL in your coding, then you need to update the URL whenever it changes.
 Same case would be with Username and password.
So, to overcome situations like this, we usually give these changing parameters in properties file.
You can create the properties file anywhere in the project, just make sure that you give correct path while loading the file.
I
n properties file each entry is stored as a pair of Strings, let us say, Name and value.
Now, let us see, how can we create properties file first of all.
Click on File -> New -> File
 Give some meaningful file name. save the file with “.properties” extension
Once the file is created, Open the file, and have some data, can refer below snap.
Now, the file is created. Let us see how to read this file in our program.
There are 3 simple steps to read file
1. Define FileInputStream & Give your file path of properties file. 

Ex –

File configfile = new File("C:UsersPrakashworkspaceAayush-SeleniumsrccomaayushCorpexternalResourcesConfiguration.Properties");

FileInputStream fis = new FileInputStream(configfile);

2. Load properties file 

For this, create object of properties file & then load.

Properties prop = new Properties();
prop.load(fis);

       3. Retrieve data from properties file.
Use below line to retrieve data.

     PropertyFileValue = prop.getProperty(key);
Below is the complete code, which return the value from properties file based on the key you supply.

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.util.Properties;

public class PropertyOperations {

public String getPropertyValueByKey(String key) {

String PropertyFileValue = null;

FileInputStream fis = null;

try {

File configfile = new File(
fis = new FileInputStream(configfile);

} catch (Exception e) {
System.out.println("Configuration file is not found. terminating Process !!!");

System.exit(0);

}

Properties prop = new Properties();

try {

prop.load(fis);

} catch (IOException e) {

e.printStackTrace();

}


Boolean isFileEmpty = prop.isEmpty();


if (isFileEmpty == false) {

PropertyFileValue = prop.getProperty(key);

} else {

System.out.println("Configuration file is empty. Processing is terminated");

System.exit(0);


}

return PropertyFileValue;


}

}

You can create object of this class in any class where you wanted to retrieve the data from properties file. And then retrieve the data by giving key.

Ex – 

PropertyOperations prop = new PropertyOperations();

String username = prop.getPropertyValueByKey("username")

Above piece of code will return username from properties file in to String username.

Post you questions in comment if you have any

 

Leave Comment

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

Looking for learning Framework Development from Scratch? Lookout for Detailed Framework Development videos on YouTube here -

https://www.youtube.com/automationtalks

Get the Framework code at Github Repo: https://github.com/prakashnarkhede?tab=repositories