TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: How to Read data from Properties File Using TestNG
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Selenium Tutorial

How to Read data from Properties File Using TestNG

Last updated: Sep 12, 2023 2:25 am
By Meenakshi Agarwal
Share
6 Min Read
Read data from Properties File Using TestNG Framework
Read data from Properties File Using TestNG Framework
SHARE

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 <locator.properties>.
  2. Read project configurations like the URL, username, and password from the <datafile.properties>.
  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- <locator.properties>

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

2- <datafile.properties>

url=http://phptravels.net/login
username=user@phptravels.com
password=demouser

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.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
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();
			properties.load(Locator);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	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"))
			return By.id(locatorValue);
		else if (locatorType.toLowerCase().equals("name"))
			return By.name(locatorValue);
		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);
		else
			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;

	@Test
	public void login() throws Exception {

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

		// Get the username element
		WebElement username = driver.findElement(uimap
				.getLocator("Username_field"));
		username.sendKeys(datafile.getData("username"));

		// Get the password element
		WebElement password = driver.findElement(uimap
				.getLocator("Password_field"));
		password.sendKeys(datafile.getData("password"));

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

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

	@BeforeMethod
	public void setUp() throws Exception {

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

		// Create a new instance of the Firefox driver
		driver = new FirefoxDriver();
		driver.get("http://phptravels.net/login");
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
	}

	@AfterMethod
	public void afterMethod() throws Exception {
		driver.quit();
	}

}

 

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.

 

Thanks,

TechBeamers.

You Might Also Like

20 Demo Sites for Automation Testing

Page Object Model (POM) and Page Factory Guide in Selenium Java

Selenium 4 Relative Locators Guide

Selenium Version 4 Features – What’s New?

How to Inspect Element in Safari, Android, and iPhone

Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article Implement Object Repository in Selenium Webdriver How to Implement Object Repository (OR) in Selenium
Next Article Save Selenium WebDriver TestNG Result in Excel Code to Save Selenium TestNG Result to Excel

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x