Page Object Model |
What is Page Object Model (POM)?
POM is a design pattern used in Selenium Automation testing to create a framework for testing web applications. It separates the test code from the code responsible for interacting with the user interface (UI) of the web application, making it easier to maintain and update tests as changes are made to the application.
- In POM, Each page of the web application is represented as an object, with methods and properties that represent the elements on that page.Â
- These objects are used in the test code to interact with the application instead of directly interacting with the HTML elements on the page.
- The benefits of using POM include improved test maintenance, increased code reusability, improved test structure, and better collaboration between testers and developers.
The Architecture of the Page Object Model (POM):
The Architecture of the Page Object Model in Selenium is based on the concept of separating the Page Objects from the test code. It follows the principle of "separation of concerns," where the test code only interacts with the page objects, and the page objects handle the interactions with the web elements on the page.
The Architecture of POM typically consists of three components:
1. Page Classes
A page class is a Java class that contains the elements and actions for a specific page in the application. Each page in the application will have its own page class. The page class defines the web elements on the page and the actions that can be performed on them.
2. Test Classes
A test class is a Java class that contains the actual test code. It interacts with the page objects to perform the desired actions and verify the expected results. The test class should not contain any direct references to the web elements on the page.
3. Driver Class
The driver class is responsible for initializing the web driver and providing it to the page and test classes. The driver class should only be instantiated once in the test suite, and all page and test classes should use the same instance of the driver.
How to implement the Page Object Model (POM) using Selenium with Java:
Step 1: Create a Maven Project
Create a new Maven project in your IDE (such as Eclipse or IntelliJ IDEA). This will manage your project's dependencies and make it easier to package and distribute your code.
Step 2: Add Selenium and TestNG Dependencies
Add the Selenium and TestNG dependencies to your project's pom.xml file.Â
Here's an example:
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.4.0</version>
</dependency>
</dependencies>
Step 3: Create Page Classes using Page Factory
Create a new Java class for each page of the web application you want to test.Â
For example, if you have a login page, create a LoginPage class.
public class LoginPage {
private WebDriver driver;
Â
public LoginPage(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
Â
@FindBy(id = "username")
private WebElement usernameInput;
Â
@FindBy(id = "password")
private WebElement passwordInput;
Â
@FindBy(id = "login-btn")
private WebElement loginButton;
Â
public void enterUsername(String username) {
usernameInput.sendKeys(username);
}
Â
public void enterPassword(String password) {
passwordInput.sendKeys(password);
}
Â
public void clickLoginButton() {
loginButton.click();
}
}
In this example, we're using the @FindBy annotation to locate the web elements on the page. We're also using the PageFactory class to initialize the web elements with the driver.
Step 4: Create Test Classes
Create a new Java class for each test you want to run. Here's an example:
public class LoginTest {
private WebDriver driver;
Â
@BeforeClass
public void setup() {
driver = new ChromeDriver();
}
Â
@AfterClass
public void teardown() {
driver.quit();
}
Â
@Test
public void testLogin() {
LoginPage loginPage = new LoginPage(driver);
loginPage.enterUsername("myusername");
loginPage.enterPassword("mypassword");
loginPage.clickLoginButton();
Â
// Assert that the user is logged in
Assert.assertTrue(isUserLoggedIn());
}
Â
private boolean isUserLoggedIn() {
// Code to check if the user is logged in
}
}
In this example, we're using the @BeforeClass annotation to set up the driver before the tests run, and the @AfterClass annotation to quit the driver after the tests run. We're also using the @Test annotation to specify that this method is a test.
Step 5: Run the Tests
Finally, run the tests using TestNG. You can do this from the command line or from within your IDE.Â
Here's an example command to run the tests from the command line:
mvn clean test
This will run all the tests in your project.
Benefits of Page Object Model with Selenium Java:
- Separation of concerns: The test code and the code responsible for interacting with the UI are separated, making the code easier to maintain and update.
- Code reusability: The page objects can be reused in multiple tests, reducing code duplication.
- Improved test structure: The use of page objects and test classes makes the test structure more organized and easier to understand.
- Easy maintenance: If the UI changes, only the page object needs to be updated, rather than modifying all the tests that use that page.
- Reduced code complexity: The use of page objects abstracts the details of interacting with the UI, making the test code simpler and easier to read.
Overall, the Page Object Model is a widely-used design pattern in Selenium automation testing that can help simplify the test code, improve code reusability, and make test maintenance easier.
Post a Comment