How to use POM in Selenium
As an IT professional, efficiency is essential in web development and testing. Do you really want to go through many steps whenever you want to ensure a button on your website is working? That’s where the Page Object Model (POM) design pattern comes to the rescue, simplifying the testing process and saving you time and frustration. In this article, we’ll explore POM, why it’s such a game-changer, and how you can easily use it to test your application.
Table of contents
- What is the Page Object Model (POM)?
- Page Object Model advantages
- Setting up Page Object Model with Selenium WebDriver
- Difference between Page Object Model and Page Factory
- Components of the Page Object Model
- Commonly asked questions on Page Object Model
- Conclusion
What is the Page Object Model (POM)?
The Page Object Model (POM) is a design pattern used in web testing to organize and streamline the testing process. It creates a blueprint for web pages, breaking them into reusable components called page objects. Each page object represents a specific page or section of a webpage and contains methods to interact with its elements. This separation of concerns makes test code more organized, easier to read, and less prone to errors when changes occur in the UI.
Fun fact:
The Page Object Model was created by Martin Fowler, and it was originally called Window Driver!
In practice, POM simplifies testing by outlining the details of the webpage structure from test logic. Test scripts interact with these page objects instead of directly with web page elements, making tests more flexible to changes in the UI.
Advantages of Page Object Model
Here are several advantages of the Page Object Model broken down into distinct points:
Enhanced code maintainability
POM is a testing design pattern that boosts code maintainability by structuring test logic into reusable components called page objects. This simplifies updates and modifications, reduces the risk of errors, and facilitates collaboration among team members.
Improved test readability
The Page Object Model simplifies the application’s user interface (UI) by improving test readability. With POM, test scripts become easier to understand and maintain as they focus on high-level interactions with page objects instead of low-level DOM manipulations.
This abstraction reduces testers’ cognitive load, making it more straightforward for them to identify and understand the purpose of each test. As a result, troubleshooting and debugging become faster and more efficient.
Code reusability
The Page Object Model offers a significant advantage in terms of code reusability across various tests and projects. Page objects encapsulate page-specific functionality, allowing them to be shared and reused across multiple tests.
This reduces code duplication and effort, leading to more efficient test development and maintenance. Additionally, POM’s modular framework promotes the creation of generic page objects that can be applied to similar pages within the application, further increasing code reusability and reducing redundancy.
Isolation of UI changes
POM isolates changes in the UI, making tests more resilient to UI modifications. When the web UI elements or functionality change, only the corresponding page objects need to be updated, minimizing the impact on test scripts. This isolation reduces the test script maintenance overhead associated with new UI changes, allowing testers to focus on validating application functionality rather than constantly adjusting test scripts.
Improved collaboration
POM facilitates collaboration among team members by providing a common language for discussing and understanding application pages. It promotes consistent naming conventions and coding standards, streamlining communication and enhancing productivity. This shared understanding leads to more efficient test planning, execution, and maintenance.
Setting up Page Object Model with Selenium WebDriver
This section will discuss how to set up the Page Object Model with Selenium WebDriver, the industry-standard tool for web automated testing.
Create page objects
The first step in setting up POM with Selenium WebDriver is to create page objects for each webpage or section of your application. Since we will be using Java, page objects are created by defining a Java class that contains methods for interacting with a given application page.
Let’s set up a page object for the Facebook login page:
package myproject.tests; // Add your respective package here
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class LoginPage {
private final WebDriver driver;
private final By usernameTextbox = By.id("email"); // Locator for the username text box
private final By passwordTextbox = By.id("pass"); // Locator for the password text box
private final By loginButton = By.name("login"); // Locator for the login button
// Constructor to initialize the WebDriver
public LoginPage(WebDriver driver) {
this.driver = driver;
}
// Method to enter the username
public LoginPage enterUsername(String username) {
// Wait until the username text box is visible, then enter the username
WebElement usernameElement = new WebDriverWait(driver, Duration.ofSeconds(10))
.until(ExpectedConditions.visibilityOfElementLocated(usernameTextbox));
usernameElement.sendKeys(username);
return this;
}
// Method to enter the password
public LoginPage enterPassword(String password) {
// Wait until the password text box is visible, then enter the password
WebElement passwordElement = new WebDriverWait(driver, Duration.ofSeconds(10))
.until(ExpectedConditions.visibilityOfElementLocated(passwordTextbox));
passwordElement.sendKeys(password);
return this;
}
// Method to click on the login button
public void clickLogin() {
// Wait until the login button is clickable, then click on it
WebElement loginButtonElement = new WebDriverWait(driver, Duration.ofSeconds(30))
.until(ExpectedConditions.elementToBeClickable(loginButton));
loginButtonElement.click();
}
}
Code language: PHP (php)
Write test class
With the page objects in place, the next step is to write test classes. Writing test classes becomes much simpler and more readable. The below code example shows how you can write a test class using the page objects we created above:
package myproject.tests;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class FacebookLoginTest {
private WebDriver driver;
@BeforeTest
public void setUp() {
// This uses the Chrome browser but you can use a different one if you choose
driver = new ChromeDriver();
driver.get("https://www.facebook.com/");
driver.manage().window().maximize();
}
@Test
public void testLoginValid() {
LoginPage loginPage = new LoginPage(driver);
loginPage.enterUsername("your_email@example.com")
.enterPassword("your_password")
.clickLogin();
// Add your assertions here
String pageTitle = driver.getTitle();
Assert.assertTrue(pageTitle.contains("Facebook"));
}
@AfterTest
public void tearDown() {
driver.quit();
}
}
Code language: JavaScript (javascript)
Note
This example ran the test script using the Chrome browser, but Selenium supports different browsers, such as Firefox, Edge, Safari, and more.
Maven pom.xml file
Here, we have used a pom.xml
file, which is very useful for running this POM project. The pom.xml file contains project dependencies, build settings, and other configurations required for managing and executing the project.
We have used Maven for this project, and the pom.xml file is mandatory in Maven. Alternatively, if you prefer not to use Maven, you must manually manage dependencies and configurations. In such cases, you can create and use a JAR file for packaging your project for execution.
Here’s a simplified version of the POM file with the bare minimum dependencies for the test execution:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>TestProject</artifactId>
<version>1.0.0-SNAPSHOT</version>
<dependencies>
<!-- Selenium WebDriver Dependency -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<!-- Visit https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java for the latest version -->
<version>4.12.1</version>
</dependency>
<!-- TestNG Dependency -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<!-- Visit https://mvnrepository.com/artifact/org.testng/testng for the latest version -->
<version>7.8.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Code language: HTML, XML (xml)
This minimal POM file includes only the dependencies required for running Selenium WebDriver tests with TestNG. You can add more dependencies as needed for your specific test requirements.
Run your tests
Once your test class is ready, you can run the tests against your web application to verify that everything works as expected. Here, we have used Eclipse IDE as both the editor and the platform for running tests. To start the run, right-click the test class, select Run As, and click TestNG Test. Otherwise, you can type the short key as Alt + Shift + X
.
You should see the web browser window open. Navigate to the Facebook login page, enter the provided username and password, and click the login button. If the test runs successfully, the web browser window will close automatically.
Note
If you prefer to work with Selenium using Python instead of Java, the process remains similar. Ensure that Selenium WebDriver for Python is added as a dependency. Create page objects with methods to interact with web elements using the Python Selenium library and write test classes to create test cases using assertions from Python’s built-in unittest or pytest framework. Although the language and libraries differ, the core concepts of the Page Object Model remain consistent, ensuring a structured and organized approach to automated testing.
Difference between Page Object Model and Page Factory
Page Object Model (POM) is a design pattern in which web pages are defined as classes, and the different elements on the page are defined as variables on the class. The class methods then provide interaction with these elements. POM provides an organized and easy-to-maintain way to interact with web elements, enhancing code reusability, as the logic related to specific pages is contained within respective Page classes.
On the other hand, Page Factory is a design pattern used within the Page Object Model to augment its functionality. It uses the @FindBy annotation, which allows the initialization of web elements of a page object without using the findElement
method repeatedly. Moreover, the Page Factory initializes the elements of a page object when they are created, making them available when the page object is initialized.
WARNING
According to the Selenium team, “you should not be using PageFactory.”
Components of the Page Object Model
Page objects
Page objects represent web pages in your application. Each page object encapsulates web elements and methods to interact with those elements. Page objects should have the following characteristics:
- Each web page of the application should have a corresponding page object.
- Page objects should contain the locators and methods to interact with the elements on the page.
Web elements
Web elements are the building blocks of web pages. They include buttons, input fields, drop-downs, etc. The Page Object Model identifies web elements using locators such as ID, name, class name, CSS selector, and XPath. These locators are stored within the page object.
Example of web elements:
- Username text field
- Password text field
- Login button
Locators
Selenium locators are used to identify web elements uniquely on a web page. Locators can be defined using different attributes such as ID, name, class name, CSS selector, or XPath. They help to find the web elements on the web page.
Here are some locator examples:
- ID: By.id(“email”)
- Name: By.name(“password”)
- CSS Selector: By.cssSelector(“#loginbutton”)
- XPath: By.xpath(“//input[@name=’username’]”)
Methods
Methods are used to perform actions on web elements, like entering text, clicking buttons, etc. Methods are defined in the page objects to interact with the web elements identified by locators. Examples of methods:
enterUsername(String username)
: Enters the username in the username text field.enterPassword(String password)
: Enters the password in the password text field.clickLogin()
: Clicks the login button.
Test classes
Test classes are responsible for writing test cases. They use page objects to interact with web elements and perform actions. Test classes should not contain direct references to web elements but call methods from the page objects, making the code more readable and maintainable.
Test classes play a crucial role in the Page Object Model. They ensure the application functions as expected, verifying its behavior to ensure it meets the specified requirements. Test classes promote cleaner and more maintainable code by separating the test logic from the page interaction.
Best practices for writing test classes
Test classes should only focus on the logic of the test case. They should call methods from the page objects to interact with the web elements. This separation of concerns improves the code’s maintainability and readability.
Verifying the application’s behavior by adding assertions within the test classes is important to ensure it functions as expected. Assertions allow you to validate specific outcomes, helping to catch bugs and ensure the application’s correctness.
Parameterize test classes to handle different test scenarios. By providing parameters, you can easily run the same test with different data, increasing the scope and depth of your testing.
Use clear and descriptive names for each test method. This will help you understand what the test is doing and what scenarios it covers.
Prepare the test environment and clean up after the test using setup and teardown methods. This ensures the consistency and independence of the tests.
Avoid hardcoding data within the test classes. Instead, use variables or configuration files. This makes it easier to update and maintain the test code.
Commonly asked questions on Page Object Model
Is POM a framework?
No.
The Page Object Model is not a framework but rather a design pattern. It’s a methodology for organizing and maintaining code in a structured way. POM helps improve the maintainability, reusability, and readability of Selenium WebDriver automation tests by separating the web pages or sections of a web application into separate classes. This approach makes it easier to manage the automation codebase and helps ensure consistency across the test suite.
How do you explain the Page Object Model in an interview?
When explaining the Page Object Model (POM) in an interview, you should describe it as a design pattern used in test automation. POM involves creating a page object for each application webpage. Each page object holds the web elements and methods to interact with those elements on the respective page. This approach significantly enhances code readability, maintainability, and reusability.
Final thoughts on Page Object Model
In conclusion, the Page Object Model design pattern is a valuable testing tool. By managing web elements into separate classes, POM significantly enhances code readability, maintainability, and reusability in automated testing.
Adopting POM ensures a structured approach, simplifying the testing process while saving time and minimizing potential headaches. With its clear separation of concerns between test code and page interaction code, POM offers a scalable and efficient solution, making it a fundamental best practice for anyone involved in automated testing. Happy testing!
Related articles
- Selenium WebDriver interview preparation
- How to run headless Chrome in Selenium
- Java interview questions and answers
Follow our blog
Be the first to know when we publish new content.
Page Object Model Selenium
- Rest Assured API Testing Tutorial - May 10, 2024
- Page Object Model (POM) Tutorial - May 8, 2024
- Selenium IDE Tutorial for Beginners - May 7, 2024