Page Object Model and Implementation in Selenium

In testing department of today’s IT sector, Automation has a significant role. IT companies are leaning towards automation testing because there are endless advantages of automating an application. For programming language, automation gives flexibility. There are distinct types of frameworks that companies use for automating their applications. Some of which are mentioned below, and one of which is Page Object Model also popular as POM.

●    Page Object Model (POM)
●    Hybrid
●    Data Driven
●    Keyword Driven

POM is a type of framework which is very easy to understand and easy to implement while making architecture of any automation process. It basically enhances the test maintenance and reduces the possibility of duplication of code, which is very concerned thing in test automation. In other words POM is a structured base object repository design.

In POM we create a page class for each corresponding web page in the application. Now, the page class that we have created contains all the web-elements of that page and also that methods that we will perform on those web-elements, so the name that we give to a method should be according to its functionality, for example- for a log-in page, the name of the method can be login() which only has few elements like user-name, password, log-in button, forget password link, etc. and methods like passing strings in the field and clicking the buttons.

For making a robust and easy to maintain framework we use POM with data driven by collating excel with POM. The best combination would be POM with data driven through excels and run test cases through TestNG

Below mentioned the flowchart will make it clearer to understand:

Flow Chart

Implementation Example in Selenium 

  1. Create a new package file as Practice; we will be creating different packages for Page Objects, Utilities, Test Data, Test Cases and Modular actions. It is always recommended to use this structure, as it is easy to understand, simple to use and easy to maintain.
  2. Create a new class file and refer the name to the actual page from the test object. In our case it is Home Screen and Login Screen.
  3. Create a static method for each element in Home Screen. Each method will have an argument (driver) and returns a value (element).

package Practice;

   import org.openqa.selenium.By;

    import org.openqa.selenium.WebDriver;

    import org.openqa.selenium.WebElement;

public class Home_Screen {

    private static WebElement element = null;

public static WebElement MyAccount(WebDriver driver){

    element = driver.findElement(By.id(“id”));

    return element;

    }

public static WebElement LogOut(WebDriver driver){

    element = driver.findElement(By.id(“logout”));

return element;

    }

}

4.Reason of passing driver as argument selenium is able to locate the element on the browser (driver). Element is returned so that action can be performed on it.
5.Method is declared as public static so that it can be called in any other method without creating instance of the class.
6.Follow same rule for creating another class LogIn Screen.

package Practice;

import org.openqa.selenium.*;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

public class LogIn_Screen {

        private static WebElement element = null;

    public static WebElement UserName(WebDriver driver){

         element = driver.findElement(By.id(“id”));

         return element;

         }

     public static WebElement Password(WebDriver driver){

         element = driver.findElement(By.id(“id”));

         return element;

         }

     public static WebElement LogIn(WebDriver driver){

         element = driver.findElement(By.id(“id”));

         return element;

         }

}

7. Now create a new class which will be our test case, let’s say we are creating it in package called Framework by name POM.

package Framework;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;

// Import package pageObject.*

import pageObjects.Home_Screen;

import pageObjects.LogIn_Screen;

public class POM{

private static WebDriver driver = null;

public static void main(String[] args) {

driver = new FirefoxDriver();

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

driver.get(“http://www.store.demoqa.com”);

// Use page Object library now

Home_Screen.MyAccount(driver).click();

LogIn_Screen.UserName(driver).sendKeys(“testuser_1”);

LogIn_Screen.Password(driver).sendKeys(“Test@123”);

LogIn_Screen.LogIn(driver).click();

System.out.println(” Login Successfully, now it is the time to Log Off buddy.”)

Home_Screen.LogOut(driver).click();

driver.quit();

}

}

8.You will notice that once you type HomeScreen in your test script and the moment you press dot, all the methods in the Home Page will display. We can expose methods in order to reduce duplicated code. We are able to call these method multiple times. This will ensure a better maintainable test code, because we only have to make adjustments and improvements in one particular place.

**Implementation reference is taken from: toolsqa.com.