-
Notifications
You must be signed in to change notification settings - Fork 1
Page Generator
In Selenium Java with Cucumber, Page Generator is a design pattern that works in conjunction with the Page Object Model (POM). It is used to dynamically create and manage page objects at runtime, making the transition between different pages easier and cleaner.
- Efficient page transitions: Page Generator simplifies navigating from one page to another by instantiating page objects dynamically during the flow of the test.
- Centralized object creation: It centralizes the creation of page objects, helping avoid repetitive code. You don't need to create new instances of page objects manually in each test.
- Better maintainability: If a constructor of a page class changes, you only need to update the Page Generator rather than multiple test classes.
The Page Generator will have a method to initialize and return an instance of a page class (like LoginPage, HomePage, etc.). It uses the WebDriver instance to initialize the page and return it.
- Page Object Class (LoginPage.java)
package pages;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class LoginPage {
WebDriver driver;
// Constructor
public LoginPage(WebDriver driver) {
this.driver = driver;
}
// Page elements (locators)
By usernameField = By.id("username");
By passwordField = By.id("password");
By loginButton = By.id("loginButton");
// Actions
public void enterUsername(String username) {
WebElement usernameElement = driver.findElement(usernameField);
usernameElement.sendKeys(username);
}
public void enterPassword(String password) {
WebElement passwordElement = driver.findElement(passwordField);
passwordElement.sendKeys(password);
}
public void clickLoginButton() {
WebElement loginBtn = driver.findElement(loginButton);
loginBtn.click();
}
}
- Page Object Class (HomePage.java)
package pages;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class HomePage {
WebDriver driver;
// Constructor
public HomePage(WebDriver driver) {
this.driver = driver;
}
// Page elements (locators)
By welcomeMessage = By.id("welcome");
// Actions
public String getWelcomeMessage() {
WebElement messageElement = driver.findElement(welcomeMessage);
return messageElement.getText();
}
}
- Page Generator Class (PageGenerator.java)
package pages;
import org.openqa.selenium.WebDriver;
public class PageGenerator {
WebDriver driver;
// Constructor
public PageGenerator(WebDriver driver) {
this.driver = driver;
}
// Method to return LoginPage object
public LoginPage getLoginPage() {
return new LoginPage(driver);
}
// Method to return HomePage object
public HomePage getHomePage() {
return new HomePage(driver);
}
}
- Step Definition Class (LoginSteps.java)
package stepDefinitions;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.cucumber.java.en.*;
import pages.PageGenerator;
import pages.LoginPage;
import pages.HomePage;
public class LoginSteps {
WebDriver driver;
PageGenerator pageGenerator;
LoginPage loginPage;
HomePage homePage;
@Given("user is on login page")
public void user_is_on_login_page() {
// Initialize WebDriver
driver = new ChromeDriver();
driver.get("https://example.com/login");
// Initialize PageGenerator and get LoginPage
pageGenerator = new PageGenerator(driver);
loginPage = pageGenerator.getLoginPage();
}
@When("user enters {string} and {string}")
public void user_enters_username_and_password(String username, String password) {
loginPage.enterUsername(username);
loginPage.enterPassword(password);
}
@And("clicks on login button")
public void clicks_on_login_button() {
loginPage.clickLoginButton();
}
@Then("user is navigated to the home page")
public void user_is_navigated_to_the_home_page() {
// Initialize HomePage object using PageGenerator
homePage = pageGenerator.getHomePage();
// Verify welcome message on the home page
String welcomeMessage = homePage.getWelcomeMessage();
System.out.println("Welcome message: " + welcomeMessage);
driver.quit();
}
}
-
Page Object Classes (LoginPage.java, HomePage.java): These represent different pages of the application, containing element locators and actions specific to those pages.
-
Page Generator Class (PageGenerator.java): This class is responsible for initializing and returning the page objects like LoginPage or HomePage. The WebDriver is passed to the constructor, and the page object methods (like getLoginPage() or getHomePage()) instantiate and return the relevant page.
-
Step Definition Class (LoginSteps.java):
- Here, PageGenerator is used to obtain the page objects.
- In the login steps, the loginPage is retrieved through the pageGenerator, and the actions like entering credentials and clicking the login button are executed.
- After successful login, the homePage is also retrieved from pageGenerator to check the welcome message on the home page.
- Centralized control: The creation of all page objects is centralized in the PageGenerator, making the code more manageable and consistent.
- Cleaner code: Instead of initializing page objects in multiple places, the PageGenerator handles object creation, reducing redundancy.
- Easier maintenance: Any changes to the initialization process (such as updating constructors) can be handled in the PageGenerator, affecting all tests using it.
This approach simplifies navigation between pages, makes code more organized, and ensures better test maintenance in the long run.