Use of .properties file in selenium project with TestNG

.properties
files are mainly used in Java programs to maintain project configuration data, database config or project settings, etc. You can easily read properties from some file using an object of type Properties. This is a utility provided by Java itself.
In simple words, a Properties file is a file with .properties
an extension. Each parameter in the properties file is stored as a pair of strings, in key-value pair format(key = value), where each key is on one line. They can also be used for storing strings for Internationalization and localization.
Since the .properties
file is a java library from .util
the package we do not need any dependencies. but because I’m using TestNG, Selenium and doing browser action let's start with a maven project.
1.Adding Dependencies to the pom.xml file
First, create a maven project and name it as PropertiesFile. Then add the following dependencies to the pom.xml file.
First, copy the selenium dependency and set it in the pom.xml file.
https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java/3.141.59
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
Copy the TestNG dependency and set it in the pom.xml file.
https://mvnrepository.com/artifact/org.testng/testng
<! — https://mvnrepository.com/artifact/org.testng/testng →
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.4.0</version>
<scope>test</scope>
</dependency>
As well as set the WebDriver maven dependency for making our test simple.
https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager/4.4.3
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>4.4.3</version>
</dependency>
That's all we have to do as prerequisites and you are good to go…
2.Create a file with a .properties extension
Under PropertiesFile project create a package called config.
Right-click on it and create a File as config.properties.
This is the very first step we are going to do by initializing our key-value pair as below.
browser=chrome
3.Create a Java class
Create a new class called “PropertiesFile”. And include the main()
method.
To understand the behaviour of the .properties
file lets write a simple code.
I have written this code with the Code Modular and to handle the exceptions it's written in the try-catch block
This code will read the value from .properties
file and print the value in the console. Then It will write a new value to the same key from .properties
file and again call the previous function. Let's understand what I have done here inside the code.
First I have created two methods inside the class named getProperties()
and setProperties()
getProperties()
setProperties()
getProperties()
will read the values from the .properties
file. setProperties()
will write values to the .properties
file.
Then I have created an object called prop
from the class level.
static Properties prop = new Properties();
In getProperties()
the method I have set the path to the .properties
file and created the object input
InputStream input = new FileInputStream(projectPath + "/src/test/java/test/config.properties");
And In here its reading the value from the .properties
file and store in the variable browser
String browser = prop.getProperty("browser");
In setProperties()
the method I have again set the path to the .properties
file and created the object output
OutputStream output = new FileOutputStream(projectPath + "/src/test/java/test/config.properties");
Here its overwriting the value in the .properties
file to the firefox and finally store it.
prop.setProperty("browser", "firefox");
prop.store(output, "setting firefox");
And in the .properties
the file you will see it's replaced with the value firefox and the date is also has been set.
#setting firefox
#Mon May 24 01:08:49 IST 2021
browser=firefox
So this is how we write and read from the .properties
file. So lets try to put another step by using TestNG and doing some browser actions with selenium.
4.Create a TestNG Class
Use the same package and create another class called “PropertiesFileTestNG”. And do not include a main()
method since it is a TestNG class.
For this example, I will use a code which I had in my previous article “How to use Extent Reports with TestNG in Selenium Java”
Just check in the link below. I have explained the code there.
https://manulwick.medium.com/how-to-use-extent-reports-with-testng-in-selenium-java-c892cf256737
This will go to the site www.ebay.com and then it will validate the site by comparing the Title. After will enter “mobile” in the search box and hit enter.
I have used @Test
annotation to execute the test case which is test1 and used extent Reports as the reporting library.
I have created a variable named browserName
in this class so I can reference it with the PropertiesFile
class.
public static String browserName = null;
I have put this reference in the PropertiesFile
so the browser can be defined from .properties
file.
PropertiesFileTestNG.browserName = browser;
Here the only thing we have to do is change the key value in the .properties
file as firefox or chrome so the rest will continue with the classes PropertiesFile
and PropertiesFileTestNG
Since we are using Extent Reports here as the reporting library Calling the flush
method writes/updates the test information of your reporter to the destination type.
So let's have a look at it.


As you can see its clearly mentioning the status and all the tests are passing. And if you noticed that I have called the PropertiesFile
again to write the status of our test to the .properties
file.
PropertiesFile.setProperties();
Before that make sure you replace setPropertiesFile
it with the following code line. This will write a new key result
setting the value as test passed
prop.setProperty("result", "test passed");

So it's clear that if the .properties
file is updating with this value our test has passed.
Ok, that’s it…
In this article, i have shown you guys What is .properties
file, How to create one with our selenium project as well as with a TestNG. Also, we analyzed test results with the Reporting library Extent Reports and we also updated the .properties
file after successful automation.
In my next article let’s talk about how to run this same selenium test framework using Jar files from the command line without using any IDE.
For your reference find my GitHub project here
THINK QUALITY!