To implement Page Object Model (POM) in Cucumber, Selenium, and TestNG, you structure your automation framework to separate page-related code, test 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 WebDriverpackage 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 Definitionspackage 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 FileFeature: 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 TestNGpackage 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.featuredefines the behavior.LoginSteps.javamaps Gherkin steps to Java code.LoginPage.javaabstracts UI elements & actions.TestRunner.javaexecutes scenarios using TestNG.BaseTest.javamanages driver lifecycle.
Benefits of POM + Cucumber + Selenium + TestNG
| Feature | Benefit |
|---|---|
| POM | Easy maintainability & reuse of locators |
| Cucumber | Readable tests in Gherkin format |
| Selenium | Browser automation |
| TestNG | Test management, reporting, parallel execution |
No comments:
Post a Comment