Page Object Model – Selenium FrameWork

This will give you an introduction to “Page Object Model” and its implementation in Selenium using java. Before starting with Page Object Model let’s first know what are design patterns. Design patterns. A Design pattern is a generic solution to a common software design/architecture problem. Implementation of these design patterns leads to inclusion of best practices and best solution, evolved over the 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 in 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 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 Page Object Model 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 – and Then we will create a test class that will create instance of these page objects and invoke there methods to create tests. Let’s take the scenario where in the login page user enters valid credentials and on clicking submit button, user is redirected to home page.

Content of

public class LoginPage {

public LoginPage(WebDriver driver) { this.driver = driver; } //Using FindBy for locating elements /*Citing documentation here- Used to mark a field on a Page Object to indicate an alternative mechanism for locating the element or a list of elements. Used in conjunction with PageFactory this allows users to quickly and easily create PageObjects.*/ @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- as clicking submit will navigate user to Home page, so return type of this method is marked as HomePage. Also note the return type of this method*/ public HomePage clickSubmit() {; return new HomePage(driver); } public HomePage loginWithValidCredentials(String userName, String password) { typeUserName(userName); typePassword(password); return clickSubmit(); } } Content of public class HomePage extends AbstractPageObject { public HomePage(WebDriver driver) { this.driver = driver; } @FindBy(id = “userInfo”) private WebElement userInfo; public void clickUserInfo(){; } public String showUserInfo(){ String userData = clickUserInfo(); return userData; } }

Content of Test class –

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(“charan”,”password”); //Fetch user info String userInfo = homePage.showUserInfo(); //Asserting user info Assert.assertTrue(userInfo.equalsIgnoreCase(“XYZ”),”Incorrect user info”); } } This was just a demo for understanding of Page Object Model, 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, for passing test data through config files etc.

10 thoughts on “Page Object Model – Selenium FrameWork

  1. Anonymous June 1, 2015 / 10:52 pm

    Thanks a lot !!


    • charan163 June 1, 2015 / 11:01 pm

      Most Welcome.


  2. Amit Jain June 2, 2015 / 6:47 pm

    How we will handle case where we have below situation

    loginWithValidCredentials(uname,passcode,role) – Normal user – Goes to normal user page (Home Page)
    loginWithValidCredentials(uname,passcode,role) – Admin user – Goes to admin page (Admin Page)

    what will be return type here for loginWithValidCredentials function because based on my data it returns different page object class…..


    • charan163 June 2, 2015 / 8:04 pm

      I see few pages are reusable, you need to create page objects for login page and normal user pages,Admin user Pages, create a method for login_Action as well as other methods which could be used by both admin and normal user. Once the user logged in,based on the page displayed,you must write the code.

      The main objective of POM is reusability.


  3. Anonymous July 30, 2016 / 1:45 am

    Charan .. Can this page object model be used to used the same code but for 2 different applications of similar type. Can you please share a sample project


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s