Creating Common Methods | Part 6

COMMON METHODS IN POM

Looking closely to the framework we have created so far, you would see that there are methods which operate on web element using the driver instance. Following is the example of such methods from LandingPage.java class:

 

public ProductSearchPage searchProduct() {
        
        //Enter text dress in the search text box
       driver.findElement(By.id("search_query_top")).sendKeys("dress");
        
        //Click on Search button
       driver.findElement(By.name("submit_search")).click();
       
    //Product search page is displayed
        return new ProductSearchPage();
}



 public SignInPage clickOnSignIn() {
        
        driver.findElement(By.xpath("//a[@class='login']")).click();
   
        //clicking on Sign In button will display Sign In page
        return new SignInPage();
    }
                       LandingPage.java

 

The code highlighted in bold above either enter a text in a text box or clicks on a web element. Other classes also contain similar code where a driver instance is used to interact with the web element and perform certain actions.

In this section, we will:

  • Remove the driver dependency from all the page classes (except BasePage.java and NavBar.java)
  • Create common methods inside BasePage.java class to perform operations on web elements.

 

Common method for click

 

Recall from the previous post where we created or.properties file containing the locators of all possible web elements. We will make use of this properties file in this tutorial.

In LandingPage.java class, the click operation happens for 2 web elements. We will create a method inside BasePage.java class which accept the String variable, which is the web element’s locator key from or.properties file.

Update the BasePage.java class with the following code (highlighted in bold):

 

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);
        }

    }

    public void click(String element) {
        
        if(element.contains("XP")) {
            driver.findElement(By.xpath(or.getProperty(element))).click();
        }
        
        if(element.contains("ID")) {
            driver.findElement(By.id(or.getProperty(element))).click();
        }
        
        if(element.contains("NAME")) {
            driver.findElement(By.name(or.getProperty(element))).click();
        }
        
    }
}

 

  • click() method accepts a String argument, which is the key (locator name) from or.properties file.
  • Since all the keys are suffixed with the respective locator indicators(XP, ID, NAME), if block will identify and process(click) the desired locator.

 

Common method for sendkeys

 

Similar to the click() method, we will create another enter() method which will accept 2 arguments of type String. One argument will be the locator key and another will be the value which user want to enter. This value is hardcoded in the LandingPage.java class. 

Update the BasePage.java class with the following code (highlighted in bold):

 

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);
        }

    }

    public void click(String element) {
        
        if(element.contains("XP")) {
            driver.findElement(By.xpath(or.getProperty(element))).click();
        }
        
        if(element.contains("ID")) {
            driver.findElement(By.id(or.getProperty(element))).click();
        }
        
        if(element.contains("NAME")) {
            driver.findElement(By.name(or.getProperty(element))).click();
        }
        
    }
    
    public void enter(String element, String value) {
        
        if(element.contains("XP")) {
            driver.findElement(By.xpath(or.getProperty(element))).sendKeys(value);
        }
        
        if(element.contains("ID")) {
            driver.findElement(By.id(or.getProperty(element))).sendKeys(value);
        }
        
        if(element.contains("NAME")) {
            driver.findElement(By.name(or.getProperty(element))).sendKeys(value);
        }
        
    }
}

 

 

Updating the page classes

 

LandingPage.java

Now, we will update the LandingPage.java class and use the above methods instead of using the driver instance. Below is the updated code for LandingPage.java:

 

package com.appliedselenium.pages;

import org.openqa.selenium.By;

public class LandingPage extends BasePage{
    
    

    
    //First Test. This title should be verified in LandingPageTest class
    public String getTitle() {
        return driver.getTitle();
    }
    
    
    //Searching for a product at landing page returns (displays) product search page. Hence 
    //the return type is ProductSearchPage and return value is new ProductSearchPage() 
    public ProductSearchPage searchProduct() {
        
        //Enter text dress in the search text box
       // driver.findElement(By.id("search_query_top")).sendKeys("dress");
      
        enter("searchkey_ID", "dress");
        
        //Click on Search button
       // driver.findElement(By.name("submit_search")).click();
      
        click("signin_NAME");
        //Product search page is displayed
        return new ProductSearchPage();
        
    }
    
    //Click on Sign In button
    public SignInPage clickOnSignIn() {
        
        //driver.findElement(By.xpath("//a[@class='login']")).click();
      
        click("signinBtn_XP");
        //clicking on Sign In button will display Sign In page
        return new SignInPage();
    }
    

}

 

SignInPage.java

This class also contains the sendkeys and click operations on driver instance. Update this class as follows:

 

package com.appliedselenium.pages;

import org.openqa.selenium.By;

public class SignInPage extends BasePage {


    
    public HomePage userSignIn(String email, String password) {
        
        enter("email_ID", email);
        enter("password_ID", password);
        click("submit_ID");
        
        /*driver.findElement(By.id("email")).sendKeys(email);
        driver.findElement(By.id("passwd")).sendKeys(password);
        driver.findElement(By.id("SubmitLogin")).click();*/
        
        return new HomePage();
        
    }

}

 

email and password values are provided by the user (hardcoded) in the test class. We can parameterize these values also using the excel and data provider.

 

WomenPage.java

Below is the updated code for WomenPage.java class:

 

package com.appliedselenium.pages.Women;

import org.openqa.selenium.By;

import com.appliedselenium.pages.BasePage;

//Extend the BasePage to get the reference of WebDriver
public class WomenPage extends BasePage{

    public TopsPage getTops() {
        
        //click on Tops link
        //driver.findElement(By.xpath("//img[@class='replace-2x']")).click();
        
        click("topslink_XP");
        return new TopsPage();
    }
    
    //Implement this method yourself
    public DressesPage getDresses() {
        
        return new DressesPage();
    }
    
}

 

NavBar.java

This class is an exception for common methods analogy. This class contains driver instance but we cannot use our common method(s) inside this class as it does not extend BasePage.java.

Run your code using the test class and it should work fine. Comment below in case you are stuck.

Exercise:

Create a method getElementText(String locator) which returns the locator text. Replace the getText() method from HomePage.java and ProductSearchPage.java with this new method.

 

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.

2 thoughts on “Creating Common Methods | Part 6

Leave a Reply

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