Page Object Model with Cucumber, Selenium, and TestNG

  

To implement Page Object Model (POM) in CucumberSelenium, and TestNG, you structure your automation framework to separate page-related codetest step definitions, and test runners for modularity and maintainability.


What is Page Object Model (POM)?

POM is a design pattern in Selenium that creates an object repository for web UI elements. It helps reduce code duplication and improves test maintenance.


Page Object Model Implementation


pom.xml:

<dependencies>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-java</artifactId>
        <version>7.14.0</version>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-testng</artifactId>
        <version>7.14.0</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.21.0</version>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>7.10.0</version>
        <scope>test</scope>
    </dependency>
</dependencies>




LoginPage.java (Page Object Class)

package pages;

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

public class LoginPage {
    WebDriver driver;

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

    // Locators
    By username = By.id("username");
    By password = By.id("password");
    By loginBtn = By.id("login");

    // Actions
    public void enterUsername(String user) {
        driver.findElement(username).sendKeys(user);
    }

    public void enterPassword(String pass) {
        driver.findElement(password).sendKeys(pass);
    }

    public void clickLogin() {
        driver.findElement(loginBtn).click();
    }
}





BaseTest.java – Setup WebDriver

package utils;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.cucumber.java.After;
import io.cucumber.java.Before;

public class BaseTest {
    public static WebDriver driver;

    @Before
    public void setUp() {
        driver = new ChromeDriver();
        driver.manage().window().maximize();
    }

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




LoginSteps.java – Step Definitions

package stepdefinitions;

import io.cucumber.java.en.*;
import pages.LoginPage;
import utils.BaseTest;

public class LoginSteps {
    LoginPage loginPage;

    @Given("user is on login page")
    public void user_is_on_login_page() {
        BaseTest.driver.get("https://example.com/login");
        loginPage = new LoginPage(BaseTest.driver);
    }

    @When("user enters username {string} and password {string}")
    public void user_enters_credentials(String username, String password) {
        loginPage.enterUsername(username);
        loginPage.enterPassword(password);
    }

    @And("clicks on login button")
    public void clicks_login_button() {
        loginPage.clickLogin();
    }

    @Then("user should be redirected to dashboard")
    public void user_redirected_to_dashboard() {
        String url = BaseTest.driver.getCurrentUrl();
        assert url.contains("dashboard");
    }
}




login.feature – Feature File

Feature: Login Feature

  Scenario: Successful login
    Given user is on login page
    When user enters username "admin" and password "admin123"
    And clicks on login button
    Then user should be redirected to dashboard




TestRunner.java – Cucumber Test Runner using TestNG

package testrunner;

import io.cucumber.testng.AbstractTestNGCucumberTests;
import io.cucumber.testng.CucumberOptions;

@CucumberOptions(
    features = "src/test/java/features",
    glue = {"stepdefinitions", "utils"},
    plugin = {"pretty", "html:target/cucumber-report.html"},
    monochrome = true
)
public class TestRunner extends AbstractTestNGCucumberTests {
}


How it works together

  • login.feature defines the behavior.

  • LoginSteps.java maps Gherkin steps to Java code.

  • LoginPage.java abstracts UI elements & actions.

  • TestRunner.java executes scenarios using TestNG.

  • BaseTest.java manages driver lifecycle.



Benefits of POM + Cucumber + Selenium + TestNG

FeatureBenefit
POMEasy maintainability & reuse of locators
CucumberReadable tests in Gherkin format
SeleniumBrowser automation
TestNGTest management, reporting, parallel execution