Read data from Properties File Using TestNG Framework

In this post, we are explaining how to read data from properties file using TestNG framework. In our previous post, we’ve illustrated the same concept i.e. creating/reading property file in a simple Selenium Webdriver project. Here, we’ll use both the TestNG framework and the Selenium Webdriver API. We did this post to demonstrate how the TestNG framework can extend the capability of a Selenium Webdriver project. You can learn the whole concept from the below example of the Selenium Webdriver project to read data from properties file using TestNG framework.

Let’s see a few details about the example project.

Firstly, it is essential to understand the test case scenario that we’ll cover using the example given in the post. There we’ll be performing the following operations.

  1. Read object locators (HTML form fields like username, password, and the login button) values from the <>.
  2. Read project configurations like the URL, username, and password from the <>.
  3. Open the website’s login page.
  4. Fill the text fields and perform the login.

We’ll use two properties file in the Selenium Webdriver project example; one file is to keep the element locators, and the other is for the project settings like the username/password.

1- <>

Username_field =name:username
Password_field =name:password
Login_button =classname:loginbtn
online_user =classname:RTL

2- <>


Example: Read data from Properties File Using TestNG Framework.

As always, you’ll need to start the Eclipse IDE and create a new Java project. You’ll be using following two Java source files in this example.

  • UIMap.Java, and
  • UIMapLoginTest.Java.

You need to add the above two files in your Eclipse project. You’ll also need to add the TestNG library and the Selenium Webdriver standalone jar to your project.

We suggest that you should refer this post for a step-by-step explanation of doing the above steps. Next, you need to create a <Resources> folder into your project and copy these two properties files under this folder. You can use the properties file content as is because the same data we’d utilized for the testing purpose.

We are now attaching the source code of the <UIMap.Java> file. It is a Java class file which provides the functions to load the properties and read the values from the properties file.


package com.techbeamers.testng;

import java.util.Properties;

import org.openqa.selenium.By;

public class UIMap {

	Properties properties;

	public UIMap(String FilePath) {

		try {
			FileInputStream Locator = new FileInputStream(FilePath);
			properties = new Properties();
		} catch (FileNotFoundException e) {
		} catch (IOException e) {


	public String getData(String ElementName) throws Exception {
		// Read value using the logical name as Key
		String data = properties.getProperty(ElementName);
		return data;

	public By getLocator(String ElementName) throws Exception {
		// Read value using the logical name as Key
		String locator = properties.getProperty(ElementName);
		// Split the value which contains locator type and locator value
		String locatorType = locator.split(":")[0];
		String locatorValue = locator.split(":")[1];
		// Return a instance of By class based on type of locator
		if (locatorType.toLowerCase().equals("id"))
		else if (locatorType.toLowerCase().equals("name"))
		else if ((locatorType.toLowerCase().equals("classname"))
				|| (locatorType.toLowerCase().equals("class")))
			return By.className(locatorValue);
		else if ((locatorType.toLowerCase().equals("tagname"))
				|| (locatorType.toLowerCase().equals("tag")))
			return By.className(locatorValue);
		else if ((locatorType.toLowerCase().equals("linktext"))
				|| (locatorType.toLowerCase().equals("link")))
			return By.linkText(locatorValue);
		else if (locatorType.toLowerCase().equals("partiallinktext"))
			return By.partialLinkText(locatorValue);
		else if ((locatorType.toLowerCase().equals("cssselector"))
				|| (locatorType.toLowerCase().equals("css")))
			return By.cssSelector(locatorValue);
		else if (locatorType.toLowerCase().equals("xpath"))
			return By.xpath(locatorValue);
			throw new Exception("Locator type '" + locatorType
					+ "' not defined!!");



Now is the turn of the second file which is <UIMapLoginTest.Java>. It is the main TestNG test case file which launches the browser and gets the login form filled for submission. It also reads the locators and the run-time configurations from the properties files.


package com.techbeamers.testng;

import static org.testng.AssertJUnit.assertEquals;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class UIMapLoginTest {
	public WebDriver driver;
	public UIMap uimap;
	public UIMap datafile;
	public String workingDir;

	public void login() throws Exception {

		// Get object map file
		uimap = new UIMap(workingDir + "\\Resources\\");

		// Get the username element
		WebElement username = driver.findElement(uimap

		// Get the password element
		WebElement password = driver.findElement(uimap

		// Click on the Login button
		WebElement login = driver.findElement(uimap.getLocator("Login_button"));;

		// Assert the user login by checking the Online user
		WebElement onlineuser = driver.findElement(uimap
		assertEquals("Hi, John Smith", onlineuser.getText());

	public void setUp() throws Exception {

		// Get current working directory and load data file
		workingDir = System.getProperty("user.dir");
		datafile = new UIMap(workingDir + "\\Resources\\");

		// Create a new instance of the Firefox driver
		driver = new FirefoxDriver();
		driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);

	public void afterMethod() throws Exception {



We wish the above example of reading the properties file using TestNG framework would be useful for you.

We’ve thoroughly tested the above code, and it should work without any error. But you can directly ask if you wish to report any issue or want to share any enhancement.