In this tutorial, we will study about “Page Object Model”, a design pattern in UI automation testing. Before starting with the Page Object Model, let’s first know what are design patterns.
Content
Design pattern
A Design pattern is a generic solution to a common software design/architecture problem. Implementation of these design patterns leads to the inclusion of best practices and best solutions, evolved over time by others while working with similar problems.
Page Object Model in Selenium
A Page Object Model is a design pattern that can be implemented using Selenium WebDriver. It essentially models the pages/screen of the application as objects called Page Objects.
All the functions that can be performed on the specific page are encapsulated in the page object of that screen. In this way any change made in the UI will only affect that screens page object class thus abstracting the changes from the test classes.
Advantages of using the Page Object Model
- Increases code reusability – Code to work with events of a page is written only once and used in different test cases.
- Improves code maintainability – Any UI change leads to updating the code in page object classes only leaving the test classes unaffected.
- Makes code more readable and less brittle.
Creating a Page Object Model in Java
Here, we’ll create an automation framework implementing the POM design pattern using Selenium with Java.
Suppose we have to test a dummy application with only a login page and a home page. To start with we first need to create page objects for all available pages in our application – LoginPage.java and HomePage.java. Then we will create a test class that will create instances of these page objects and invokes their methods to create tests.
Let’s take the scenario of the login page. Here, user enters valid credentials, and on clicking submit button, the user is redirected to the home page.
LoginPage.java
public class LoginPage {
public LoginPage(WebDriver driver) {
this.driver = driver;
}
//Using FindBy for locating elements
@FindBy(id = "userName")
private WebElement userName;
@FindBy(id = "password")
private WebElement password;
@FindBy(id = "submitButton")
private WebElement submit;
/*Defining all the user actions that can be performed in the loginPage
in the form of methods*/
public void typeUserName(String text) {
userName.sendKeys(text);
}
public void typePassword(String text) {
password.sendKeys(text);
}
/*Take note of return type of this method, clicking submit will navigate
user to Home page, so return type of this method is marked as HomePage.*/
public HomePage clickSubmit() {
submit.click();
return new HomePage(driver);
}
public HomePage loginWithValidCredentials(String userName, String pwd) {
typeUserName(userName);
typePassword(pwd);
return clickSubmit();
}
}
HomePagePage.java
public class HomePage {
public HomePage(WebDriver driver) {
this.driver = driver;
}
@FindBy(id = "userInfo")
private WebElement userInfo;
public void clickUserInfo(){
userInfo.click();
}
public String showUserInfo(){
String userData = clickUserInfo();
return userData;
}
}
Test class – POMTest.java
public class POMTest{
//Create firefox driver's instance
WebDriver driver = new FirefoxDriver();
@Test
public void verifyUserInfo() {
//Creating instance of loginPage
LoginPage loginPage = new LoginPage(driver);
//Login to application
HomePage homePage = loginPage.loginWithValidCredentials("user1","pwd1");
//Fetch user info
String userInfo = homePage.showUserInfo();
//Asserting user info
Assert.assertTrue(userInfo.equalsIgnoreCase("XYZ"),"Incorrect userInfo");
}
}
This was just a demo for the understanding of the POM. An actual project would require several updations like creating abstract classes for page objects, creating base classes for test classes, creating helper and utility classes for database connectivity, passing test data through config files, etc.
That’s all we have in this post, please share it with your friends. Check out our complete selenium tutorial here – Selenium Tutorial.