Simple Page Object Model

In this example we will see a very simple Page Object Model example. To explain we have taken Google application and created BasePage, Sign In page and Create Account page.
  • To know what is page object model framework, Please refer Page Object Model Introduction
  • Steps to Create a Simple Page Object Model. The structure of the sample project should look like in the screen shot below
This the main class for page object model, where we will create Webdriver object based on the browser type passed as a parameter in textng.xml file. We will also need to pass the base page application URL in testng.xml as parameter.
  • In this example we have taken only two browsers the default Firefox and chrome browser. To initiate Chrome browser, we need to set the System Property by providing the chromedriver exe path.
  • We need to create a method to return Webdriver 'driver' where this is used for test execution.
  • Below is the example TestBaseSetup.class file
Now We will create Page Object classes. For each page we will create a separate class with constructor. We identify the locators and keep all together on just below the class. This will help us to re-use the locators for multiple methods and the main important is, we can easily find the locator and change if required.
  • We need to identify and list of all the possible functionalities on this page and we should write methods in such a way they are re-used. When ever there is a method to click on a button or link, we should return driver object of that page.
  • We will look into the below example. For method SignInBtn, we are returning after clicking on Sign In button as it is redirecting to sign in page.
  • 1. Add Junit jar into bulid path as – Right click on Project->Properties->Libraries->Add Library->JUnit->Junit-4->ok.
  • In the below SignInpage.class, we have methods to click on create account and sign in button. We are just verifying an error in sign in page for now in this example.
    SignInPage.class
  • CreateAccountPage.class
Now we will see how to write Tests for the above pages. For all the page Objects, we will now create Tests for each page. Which will help us in easy maintainability. if there is any change in the UI, we can simply change in one Page one place. We will see first example test for "BasePageTest.class".
  • In the below example we verifying home page by taking the title. We have written a method in BasePage and we are just calling it here.
  • We will look into the next test 'SignInPageTest'. In the below test, we are verifying page title, page text, and Sign in functionality. For all these verifications we have defined methods in SignInPage class which we are calling from tests.
  • Now the create test 'CreateAnAccountTest'. Now we should be able to understand the verification that we are doing in the below test.
We are done with Base Setup, Page Objects creation, Tests creation. Now we will execute them using 'testng.xml' file. We will add the classes which we want to test.
  • Remember we need to pass the parameters 'browserType' and 'appURL' for the base Setup class.
  • Steps to Create a Simple Page Object Model. The structure of the sample project should look like in the screen shot below
  • The above Page Object Model framework is the simple one without using any Build tools, Loggers, Listeners and Utilities. In the next coming post we will add all these things and design a Robust Page Object Model Framework which are used in the Industry.
  • The below are the list of things which are commonly used in the Real time Page Object Model Framework.
  • 1. Selenium Webdriver with Java
    2. Maven Build Tool
    3. TestNG (We will user TestNG Listeners also)
    4. Log4j
    5. Eclipse IDE