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