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

The Page Object Model (POM) is a design pattern in Selenium that creates an object repository for web UI elements. It reduces code duplication and improves test maintenance.

Key Concepts of POM:

  1. Page Class: Each web page has a corresponding Page Class. This class contains web elements and methods to interact with them.
  2. Test Class: The Test Class contains the test methods that use the Page Class methods.
  3. Page Factory: Selenium provides the ‘PageFactory‘ class to initialize the web elements defined in the Page Class.

Step-by-Step Implementation

1. Project Setup

Ensure you have the necessary dependencies in your ‘pom.xml‘ (for Maven projects):

				
					<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.4.0</version>
    </dependency>
</dependencies>

				
			

2. Create Page Class

Let’s consider a simple login page with a username field, password field, and login button.

				
					package com.example.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;

    // Web elements
    @FindBy(id = "username")
    WebElement usernameField;

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

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

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

    // Page methods
    public void setUsername(String username) {
        usernameField.sendKeys(username);
    }

    public void setPassword(String password) {
        passwordField.sendKeys(password);
    }

    public void clickLoginButton() {
        loginButton.click();
    }

    public void login(String username, String password) {
        setUsername(username);
        setPassword(password);
        clickLoginButton();
    }
}

				
			

3. Create Test Class

Next, create a Test Class to write the test script using TestNG.

				
					package com.example.tests;

import com.example.pages.LoginPage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class LoginTest {
    WebDriver driver;
    LoginPage loginPage;

    @BeforeMethod
    public void setUp() {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        driver = new ChromeDriver();
        driver.get("https://example.com/login");
        loginPage = new LoginPage(driver);
    }

    @Test
    public void testLogin() {
        loginPage.login("testuser", "testpassword");
        // Add assertions to verify login success
    }

    @AfterMethod
    public void tearDown() {
        driver.quit();
    }
}

				
			

Explanation

1. Page Class (LoginPage.java):

  • Web Elements: Defined using ‘@FindBy‘ annotations.
  • Constructor: Initializes web elements using ‘PageFactory.initElements‘.
  • Methods: Interact with web elements. The ‘login‘ method is a high-level method that uses other methods to perform a login.

2. Test Class (LoginTest.java):

  • setUp Method: Initializes the WebDriver and opens the login page.
  • testLogin Method: Calls the login method from the LoginPage class to perform the login action.
  • tearDown Method: Closes the browser.

Enhancements

  • Data-Driven Testing: Use a data provider to pass different sets of data to the test methods.
  • Utility Classes: Create utility classes for common functionalities like browser setup.
  • Assertions: Add assertions to validate test outcomes.
Scroll to Top