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 <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.

 

 

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.

 

 

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.

Leave a Reply


*