.properties File In POM | Part 5

.proprties file in pom

We will create 2 .properties files for this framework. Recall from the previous post, where we created folder propertiesRight click on this folder, select New > File and create the following 2 files:

  • config.properties file contains the test data information such as browser, application URL, etc.
  • or.properties file contains the web element locator values.

These classes will be loaded inside the BasePage.java class.

config .properties file

 

Inside the config.properties file, paste the following code:

 

browser=chrome
appurl=http://automationpractice.com/index.php

 

Update the BasePage.java class as below. 

 

package com.appliedselenium.pages;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqHelpera.selenium.chrome.ChromeDriver;

import com.appliedselenium.pages.commons.NavBar;

public class BasePage {

    // Declare the WebDriver
    public static WebDriver driver;
    
    //declare variable for config.properties 
    public static Properties config = new Properties();
    
    //File input stream to read from properties file
    public static FileInputStream fis;
        
    //Declare NavBar object variable
    public static NavBar navBar;

    // constructor of base class
    public BasePage() {

        if (driver == null) {
            
            
            try {
                
                //Take reference to the file
                fis = new FileInputStream(System.getProperty("user.dir")
                        + "/src/test/resources/com/appliedselenium/properties/config.properties");
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                
                //load the config.properties file
                config.load(fis);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

                    
            // Set the path for chromedriver
            String filePath = System.getProperty("user.dir");
            
            //verify if the browser is chrome or FF
            if (config.getProperty("browser").equals("chrome")) {
            System.setProperty("webdriver.chrome.driver",
                    filePath + "/src/test/resources/com/appliedselenium/drivers/chromedriver");

            driver = new ChromeDriver();
            } else if (config.getProperty("browser").equals("firefox")) {
                //launch firefox
            }

            // go to application
            driver.get(config.getProperty("appurl"));
            driver.manage().window().maximize();
            
            //Invoke the NavBar object with driver reference
            navBar = new NavBar(driver);
        }

    }
    
}

 

The above code will load the config.properties file and we can fetch the value inside this file using config.getProperty()In the above code, we have fetched values for browser and appurl.

 

or .properties file

 

This properties file contains the locator information. Go through all the classes we have created so far, and write all the locators in this properties file as follows:

 

#NavBar
womenlink_XP=//a[@title='Women']


#WomenPage
topslink_XP=//img[@class='replace-2x']

#HomePage
username_XP=//a[@class='account']/span

#LandingPage
searchkey_ID=search_query_top
signin_NAME=submit_search
signinBtn_XP=//a[@class='login']

#ProductSearchPage
searchtext_XP=//div[@class='right-block']//a[@class='product-name']

#SignInPage
email_ID=email
password_ID=passwd
submit_ID=SubmitLogin

 

The keys in this properties file are suffixed with XP, ID, and NAME to suggest the type of locator used while identifying these web elements. In the next tutorial, we will see how this approach is used to create functions in the BasePage.java class which are common to all the other classes.

The updated BasePage.java class is:

 

package com.appliedselenium.pages;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import com.appliedselenium.pages.commons.NavBar;

public class BasePage{

    // Declare the WebDriver
    public static WebDriver driver;
    
    //declare variable for config.properties 
    public static Properties config = new Properties();
    
    //declare variable for or.properties 
    public static Properties or = new Properties();
    
    //File input stream to read from properties file
    public static FileInputStream fis;
    
    
    //Declare NavBar object variable
    public static NavBar navBar;

    // constructor of base class
    public BasePage() {

        if (driver == null) {
            
            
            try {
                
                //Take reference to the config.properties file
                fis = new FileInputStream(System.getProperty("user.dir")
                        + "/src/test/resources/com/appliedselenium/properties/config.properties");
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                
                //load the config.properties file
                config.load(fis);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            
            //Take reference to the or.properties file
            try {
                fis = new FileInputStream(
                        System.getProperty("user.dir") + "/src/test/resources/com/appliedselenium/properties/or.properties");
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            //load the or.properties file
            try {
                or.load(fis);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            
            
            
            // Set the path for chromedriver
            String filePath = System.getProperty("user.dir");
            
            //verify if the browser is chrome or FF
            if (config.getProperty("browser").equals("chrome")) {
            System.setProperty("webdriver.chrome.driver",
                    filePath + "/src/test/resources/com/appliedselenium/drivers/chromedriver");

            driver = new ChromeDriver();
            }else if (config.getProperty("browser").equals("firefox")) {
                //launch firefox
            }
            // go to application
            driver.get(config.getProperty("appurl"));
            driver.manage().window().maximize();
            
            //Invoke the NavBar object with driver reference
            navBar = new NavBar(driver);
        }

    }

}

 

Execute the LandingPageTest.java class to make certain that everything is working fine. Your code should execute without any error for the upcoming tutorials.

 

Author: Dhawal Joshi

A post-graduate in MCA, ISTQB & ITIL certified QA with more than 8 years of experience in QA working with a CMMI Level 5 organization as System Analyst. I started my automation journey with HP UFT(formerly known as QTP) and for the past few years, I am using Selenium for automation. I also have experience in Android Application Development, Java, HTML, and VBScript. When I am not working, I like to spend time with my family, cooking and learning new developments in IT.

Leave a Reply

Your e-mail address will not be published. Required fields are marked *