Page Object Model (POM) is a design pattern used in Selenium automation testing to organize and maintain web page elements, their actions, and corresponding test cases. POM promotes code reusability, and maintainability, and reduces test script maintenance effort by separating the web page objects and operations from the test scripts. In this article, we will discuss the benefits of using POM and provide a code example to illustrate how to implement POM in Selenium automation testing.
Benefits of Using Page Object Model
- Code Reusability: POM promotes code reusability by separating web page elements and operations from the test scripts. This allows testers to reuse the same page object across multiple test cases, reducing the amount of code duplication and improving the code quality.
- Maintenance: POM makes it easier to maintain test scripts as changes to web pages can be isolated in a single page object class rather than having to update multiple test cases. This reduces the amount of time and effort required for script maintenance, making the testing process more efficient.
- Readability: POM improves the readability of test scripts as the test cases focus only on the actions performed on the web page elements rather than the implementation details. This makes the test cases easier to read, understand and maintain.
- Scalability: POM can be easily scaled for larger test suites as it allows for a modular approach to testing, where each page object represents a specific functionality of the web page. This makes it easier to add new test cases and update existing ones without impacting the entire test suite.
Selenium skills are a must-have for any software testing professional looking to stay competitive in the rapidly evolving technology landscape.
MasterClass Selenium WebDriver with Java: Advance Test Automation
Code Example of Page Object Model:
Consider a scenario where we need to test the login functionality of a web application. The login page has two input fields for email and password and a submit button. To automate the login functionality using POM, we need to create a separate class for the login page, which will contain all the web page elements and their corresponding methods.
Here’s a code example for the login page class-
public class LoginPage {
private WebDriver driver;
@FindBy(id = "email")
private WebElement email;
@FindBy(id = "password")
private WebElement password;
@FindBy(id = "submit")
private WebElement submit;
public LoginPage(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
public void setEmail(String email) {
this.email.sendKeys(email);
}
public void setPassword(String password) {
this.password.sendKeys(password);
}
public void clickSubmit() {
this.submit.click();
}
}
In the above code example, we have created a class named LoginPage that contains three web page elements — email, password, and submit button — and their corresponding methods. The @FindBy
annotation is used to identify the web elements based on their locator strategy, such as ID, Name, Class, etc. In this example, we have used the id
locator strategy to identify the web elements.
The constructor of the LoginPage class takes an WebDriver
object, which is used to interact with the web page elements. The PageFactory.initElements(driver, this)
method initializes the web elements, and their corresponding methods are created using the @FindBy
annotation.
The setEmail
tmethod is used to enter the email in the email input field, the setPassword
method is used to enter the password in the password input field, and the clickSubmit
method is used to click the submit button.
Now, let’s look at the test script for the login functionality that uses the LoginPage class:
public class LoginTest {
private WebDriver driver;
private LoginPage loginPage;
@BeforeClass
public void setup() {
driver = new ChromeDriver();
driver.get("http://www.example.com/login");
loginPage = new LoginPage(driver);
}
@Test
public void testLogin() {
loginPage.setEmail("test@example.com");
loginPage.setPassword("password123");
loginPage.clickSubmit();
// Add assertions to validate the login functionality
}
@AfterClass
public void teardown() {
driver.quit();
}
}
In the above code example, we have created a separate test class named LoginTest
for testing the login functionality. In the setup
method, we have initialized the WebDriver object and opened the login page. The LoginPage
object is created using the WebDriver object, and all the web page elements are initialized.
In the testLogin
method, we have used the methods of the LoginPage class to enter the email and password and click the submit button. We can add assertions to validate the login functionality.
In the teardown
method, we closed the browser window after the test execution.