Page Object Model in Selenium


Page-Object-Model-in-Selenium


Hi All, in this article, we'll be understanding Page Object Model in Selenium, famously known as POM. This is one of the most famous questions asked in automation testing interviews, mostly for 3+ years experienced candidates, who are expected to have sound knowledge on the Selenium framework as well.


Also, try: Selenium Quiz with Answers

What is Page Object Model in Selenium?


Page Object Model is a design pattern in Selenium in which we create Classes for Pages and Member Variables for Web Elements.

So for every page on the website, we create a Class which is also called Object Repository.

To summarize,
Class -> Page
Member Variable -> Web Element
Methods -> perform actions on Web Elements.

Also, the Page Object Model classes are defined independently of the test scripts which make them reusable and reduces code duplicacy.

Suppose we were to test Login Page of a website named FB then we would define the pages and test scripts in the following way:

Consider the following folder structure :
src -> pages -> FBLoginPage.java //POM Class
src -> tests -> FBLoginTest.java //Test Class

In the below code,
FBLoginPage.java defines the Login page, its web elements, and operations that can be performed on the web elements.

And FBLoginTest.java defines the test script that will be executed for testing the login page.

FBLoginPage.java
 
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
class FBLoginPage{  
      By email = By.id("email");  
      By password = By.id("password");  
      By loginBtn = By.id("btn-login");  
      Webdriver driver;  
      public FBLoginPage(WebDriver driver){  
           this.driver = driver;  
      }  
      public WebElement getEmail(){  
           return driver.find(email);  
      }  
      public WebElement getPassword(){  
           return driver.find(password);  
      }   
      public WebElement getLoginBtn(){  
           return driver.find(loginBtn);  
      }  
      public void setEmail(String email){  
           getEmail().sendKeys(email);  
      }  
      public void setPassword(String passwprd){  
           getPassword().sendKeys(password);  
      }  
      public void clickLoginButton(){  
           getLoginBtn().click();  
      }  
 }  



FBLoginTest.java
 
import org.openqa.selenium.WebDriver;
import org.testng.annotations.*;
class FBLoginTest{  
      WebDriver driver;  
      @BeforeClass  
      public void setup(){  
           System.setProperty("webdriver.chrome.driver","c://drivers/chrome.exe");  
           driver = new ChromeDriver();            
      }  
      @Test  
      public void runLoginTest(){  
           FBLoginPage loginPage = new FBLoginPage(driver);  
           loginPage.setEmail("");  
           loginPage.setPassword("");  
           loginPage.clickLoginButton();  
      }  
 }  


Page Factory

Page Factory is an inbuilt mechanism of the Selenium Webdriver which is used to define classes in Page Object Model. It uses @FindBy annotation to find Web Elements inside the web page. You can replace this with the traditional approach of defining Page Object Model classes.

Consider the following code which can be replaced with the above FBLoginPage.java

 
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

class FBLoginPage{  
      @FindBy(id="email")  //finds web element
      WebElement email;  
      @FindBy(id="password")  
      WebElement password;  
      @FindBy(id="btn-login")  
      WebElement loginBtn;       
      Webdriver driver;  
      public FBLoginPage(WebDriver driver){  
           this.driver = driver; 
           PageFactory.initElements(driver,this); //initializing web elements 
      }  
      public WebElement getEmail(){  
           return email;  
      }  
      public WebElement getPassword(){  
           return password;  
      }   
      public WebElement getLoginBtn(){  
           return loginBtn;  
      }  
      public void setEmail(String email){  
           this.getEmail().sendKeys(email);  
      }  
      public void setPassword(String passwprd){  
           this.getPassword().sendKeys(password);  
      }  
      public void clickLoginButton(){  
           this.getLoginBtn().click();  
      }  
 }