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.

Read object locators (HTML form fields like username, password, and the login button) values from the <locator.properties>.

Read project configurations like the URL, username, and password from the <datafile.properties>.

Open the website’s login page.

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.

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.

UIMap.Java | Reading Properties File Using TestNG Framework.

Java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

packagecom.techbeamers.testng;

importjava.io.FileInputStream;

importjava.io.FileNotFoundException;

importjava.io.IOException;

importjava.util.Properties;

importorg.openqa.selenium.By;

publicclassUIMap{

Properties properties;

publicUIMap(StringFilePath){

try{

FileInputStream Locator=newFileInputStream(FilePath);

properties=newProperties();

properties.load(Locator);

}catch(FileNotFoundExceptione){

e.printStackTrace();

}catch(IOExceptione){

e.printStackTrace();

}

}

publicStringgetData(StringElementName)throwsException{

// Read value using the logical name as Key

Stringdata=properties.getProperty(ElementName);

returndata;

}

publicBy getLocator(StringElementName)throwsException{

// Read value using the logical name as Key

Stringlocator=properties.getProperty(ElementName);

// Split the value which contains locator type and locator value

StringlocatorType=locator.split(":")[0];

StringlocatorValue=locator.split(":")[1];

// Return a instance of By class based on type of locator

if(locatorType.toLowerCase().equals("id"))

returnBy.id(locatorValue);

elseif(locatorType.toLowerCase().equals("name"))

returnBy.name(locatorValue);

elseif((locatorType.toLowerCase().equals("classname"))

||(locatorType.toLowerCase().equals("class")))

returnBy.className(locatorValue);

elseif((locatorType.toLowerCase().equals("tagname"))

||(locatorType.toLowerCase().equals("tag")))

returnBy.className(locatorValue);

elseif((locatorType.toLowerCase().equals("linktext"))

||(locatorType.toLowerCase().equals("link")))

returnBy.linkText(locatorValue);

elseif(locatorType.toLowerCase().equals("partiallinktext"))

returnBy.partialLinkText(locatorValue);

elseif((locatorType.toLowerCase().equals("cssselector"))

||(locatorType.toLowerCase().equals("css")))

returnBy.cssSelector(locatorValue);

elseif(locatorType.toLowerCase().equals("xpath"))

returnBy.xpath(locatorValue);

else

thrownewException("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.