Introduction to Selenium
Selenium WebDriver Basics
WebDriver Commands
Synchronization in Selenium
Working with Different Browsers
Setting up WebDriver for different browsers
Handling Advanced User Interactions
Page Object Model (POM)
Introduction to POM
TestNG Framework
Creating and Running TestNG Tests
Page Object Model (POM) is a design pattern in Selenium that enhances test maintenance and reduces code duplication. Page Factory is a class provided by Selenium WebDriver to support POM. It makes the creation of page objects simpler and easier. Let’s dive into the implementation of POM using Page Factory in Selenium Java with detailed examples.

1. Setting Up the Project

First, set up a Maven project with the necessary dependencies for Selenium.
				
					<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.3.0</version>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.4.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>

				
			

2. Creating the Page Object Model

Login Page

1. Page Class: LoginPage.java

				
					package pages;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class LoginPage {
    WebDriver driver;

    // Using @FindBy annotation to locate elements
    @FindBy(id = "username")
    WebElement usernameField;

    @FindBy(id = "password")
    WebElement passwordField;

    @FindBy(id = "loginButton")
    WebElement loginButton;

    // Constructor to initialize elements
    public LoginPage(WebDriver driver) {
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }

    // Method to log in
    public void login(String username, String password) {
        usernameField.sendKeys(username);
        passwordField.sendKeys(password);
        loginButton.click();
    }
}

				
			

Home Page

2. Page Class: HomePage.java

				
					package pages;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class HomePage {
    WebDriver driver;

    @FindBy(id = "welcomeMessage")
    WebElement welcomeMessage;

    public HomePage(WebDriver driver) {
        this.driver = driver;
        PageFactory.initElements(driver, this);
    }

    public String getWelcomeMessage() {
        return welcomeMessage.getText();
    }
}

				
			

3. Creating the Test Class

3. Test Class: LoginTest.java

				
					package tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import pages.HomePage;
import pages.LoginPage;

public class LoginTest {
    WebDriver driver;
    LoginPage loginPage;
    HomePage homePage;

    @BeforeClass
    public void setup() {
        // Set up the driver
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        driver = new ChromeDriver();
        driver.get("https://example.com/login");
        loginPage = new LoginPage(driver);
        homePage = new HomePage(driver);
    }

    @Test
    public void testLogin() {
        loginPage.login("testuser", "testpassword");
        String welcomeMessage = homePage.getWelcomeMessage();
        Assert.assertEquals(welcomeMessage, "Welcome, testuser!");
    }

    @AfterClass
    public void teardown() {
        driver.quit();
    }
}

				
			

Explanation

  1. Dependencies: Add Selenium and TestNG dependencies to the Maven project for handling browser interactions and running tests.

  2. Page Classes:

    • LoginPage: Contains elements and methods related to the login page. The ‘@FindBy‘ annotation is used to locate elements.
    • HomePage: Contains elements and methods related to the home page.
  3. Test Class:

    • setup(): Initializes the WebDriver, opens the login page, and creates instances of the page classes.
    • testLogin(): Performs the login operation using the ‘login‘ method from ‘LoginPage‘ and verifies the result using ‘getWelcomeMessage‘ from ‘HomePage‘.
    • teardown(): Closes the browser after the test completes.

Benefits of Using Page Factory in POM

  1. Readability: The @FindBy annotation makes it easy to read and maintain element locators.
  2. Initialization: The PageFactory.initElements method initializes all elements annotated with @FindBy.
  3. Separation of Concerns: Keeps the test code separate from the page-specific code, making it easier to maintain and understand.
Scroll to Top