POM | More Business Pages | Part 4

Create Business Pages In POM - Part 4

We have already created a few classes for the web pages, however, there are more web pages in the demo application. These web pages contain business logic, application flow, and other functionalities.

For instance, in the landing page, the navigation bar contains Women, Dresses and T-Shirts buttons to navigate to these respective pages.

 

Navigation Bar POM

 

Also, this navigation bar is available on all web pages, i.e. it is common across the website. We can create a class for this navigation bar in a separate package which will return the WomenPage, DressesPage, and T_ShirtsPage respectively.

 

Creating The Business Page Classes

 

In the navigation menu, click on WomenA new web page is displayed with two subcategories – Tops and Dresses

 

Business Pages – Women

 

Clicking on Tops or Dresses link will further open a new web page. We can represent these web pages in classes as follows.

Inside the src/main/java create a new package com.appliedselenium.pages.Women and add 3 classes to it.

  • WomenPage.java – This class contains 2 methods which return the TopsPage and DressesPage object.
  • TopsPage.java – This class contains methods related to Tops web page functionality.
  • DressesPage.javaThis class contains methods related to Dresses web page functionality.

Code for the above classes is written below.

 

package com.appliedselenium.pages.Women;


public class WomenPage{

    public TopsPage getTops() {
        
        return new TopsPage();
    }
    
    public DressesPage getDresses() {
        
        return new DressesPage();
    }
    
}

                          WomenPage.java

 

 

package com.appliedselenium.pages.Women;

public class TopsPage {

    public void topPage() {
        System.out.println("In Top Page");
    }
    
}

                              TopsPage.java

 

 

package com.appliedselenium.pages.Women;

public class DressesPage {
    
    public void dressesPage() {
        
        System.out.println("Dresses Page");
    }

}

                         DressesPage.java

 

Above pages still lacks in following functionalities, which we will fix further in this tutorial:

  • the WebDriver reference
  • connection of WomenPage.java with the common navigation bar.

Also, you may create similar classes for Dresses and T-Shirts web pages.

 

Creating Common Page

 

During all these tutorials, we have been working with web pages but the navigation bar is different than a web page. It could be considered as the common web element across the demo application which could be accessed from any web page.

Inside src/main/java create a new package com.appliedselenium.pages.commons and add class NavBar.java to it. This class should contain methods to return the WomenPage, DressesPages and T-ShirtsPage objects. 

 

POM Base Pages

 

 

package com.appliedselenium.pages.commons;

public class NavBar {
    
    
    public WomenPage getWomen() {
        
        return new WomenPage();
    }
    
    public void getDresses() {
        
    }
    
    public void getT_Shirts() {
        
    }

}

 

The NavBar.java class also lacks in WebDriver reference. Unlike the previous classes in this tutorial, we can not extend the BasePage.javaas NavBar.java is not a web page. Also, we have to make this class available to all the other page classes.

The simple solution to this problem is:

  • Create a static reference of NavBar object in BasePage class and pass the WebDriver reference as parameter.
  • Create constructor of NavBar class which accepts WebDriver reference.
  • Extend the BasePage.java class to all the classes inside com.appliedselenium.pages.Women package.

Below is the updated code for BasePage.java, NavBar.java, WomenPage.java, TopsPage.java, DressesPage.java and LandingPageTest.java.

 

package com.appliedselenium.pages;

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 NavBar object variable
    public static NavBar navBar;

    // constructor of base class
    public BasePage() {

        if (driver == null) {
            // Set the path for chromedriver
            String filePath = System.getProperty("user.dir");

            System.setProperty("webdriver.chrome.driver",
                    filePath + "/src/test/resources/com/appliedselenium/drivers/chromedriver");

            driver = new ChromeDriver();

            // go to application
            driver.get("http://automationpractice.com/index.php");
            driver.manage().window().maximize();
            
            //Invoke the NavBar object with driver reference
            navBar = new NavBar(driver);
        }

    }

}

                            BasePage.java

 

 

package com.appliedselenium.pages.commons;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

import com.appliedselenium.pages.Women.WomenPage;

public class NavBar {
    
    WebDriver driver;
    
    //NavBar constructor to get the reference of WebDriver instance
    public NavBar(WebDriver driver) {
        this.driver = driver;
    }
    
    public WomenPage getWomen() {
        
        //click on Women link 
        driver.findElement(By.xpath("//a[@title='Women']")).click();
        return new WomenPage();
    }
    
    
    //Try implementing rest of the pages
    public void getDresses() {
        
    }
    
    public void getT_Shirts() {
        
    }

}

                                 NavBar.java

 

 

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();
        return new TopsPage();
    }
    
    //Implement this method yourself
    public DressesPage getDresses() {
        
        return new DressesPage();
    }
    
}

                                WomenPage.java

 

 

package com.appliedselenium.pages.Women;

import com.appliedselenium.pages.BasePage;

public class TopsPage extends BasePage {

    public void topPage()  {
        System.out.println("In Top Page");
    }
    
}

                             TopsPage.java

 

 

package com.appliedselenium.pages.Women;

import com.appliedselenium.pages.BasePage;

public class DressesPage extends BasePage{
    
    public void dressesPage() {
        
        System.out.println("Dresses Page");
    }

}

                              DressesPage.java

 

 

package com.appliedselenium.tests;

import com.appliedselenium.pages.BasePage;
import com.appliedselenium.pages.HomePage;
import com.appliedselenium.pages.LandingPage;
import com.appliedselenium.pages.ProductSearchPage;
import com.appliedselenium.pages.SignInPage;
import com.appliedselenium.pages.Women.WomenPage;
import com.appliedselenium.pages.commons.NavBar;

public class LandingPageTest {

    public static void main(String[] args) {

        // Declare an object of class ProductSearchPage class
        ProductSearchPage pr;
        SignInPage inPage;
        HomePage homePage;

        // Instantiate LandingPage class object
        LandingPage landingPage = new LandingPage();

        // Verify the titles
        if (landingPage.getTitle().equals("My Store")) {
            System.out.println("Title Matched - Test Passes");
        } else {
            System.out.println("Title doesn't match - Test Failed");
        }

        // Navigate to Product Search page
        landingPage.searchProduct();

        // Instantiate ProductSearchPage class object
        pr = new ProductSearchPage();

        // Verify the text
        if (pr.getProductSearchResult().contains("Printed Summer Dress")) {
            System.out.println("Product Found");
        } else {
            System.out.println("Product Search Failed");
        }

        // Click on Sign In button
        landingPage.clickOnSignIn();

        inPage = new SignInPage();
        inPage.userSignIn("abc11001@xyz.com", "abcxyz");

        homePage = new HomePage();

        if (homePage.verifyCorrectLogin().equals("abc xyz")) {
            System.out.println("Login Successful");
        } else {
            System.out.println("Login Failed");
        }
        
        BasePage.navBar.getWomen();
        WomenPage womenPage = new WomenPage();
        
        womenPage.getTops();

    }

}

                                LandingPageTest.java

 

Lines of code highlighted in bold in LandingPageTest.java click on the Women link and then clicks on Tops section of the Women page. 

Create the other page classes and use them in the test class.

In the next tutorial, we will learn how to further refactor the BasePage.java and implement some other components to the project.

 

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 email address will not be published. Required fields are marked *