Protected by Copyscape
Powered By Blogger

Sunday, April 4, 2021

What is Page object Model ? How we can implement this using Selenium API?

Page Object Model or POM is nothing but a design pattern which you can use to make your Automation framework more readable and reusable. The major benefit of this would be the maintenance of your framework would be less.

In POM we store all locators and methods in separate Java class and we can use these classes while writing the test scripts. It means first you will create the java class for each page and then store the description of each web element of that page inside that class along with the relevant actions in terms of method which you want to perform on the web elements. The benefit of using POM would be if any change or update happen in the web element locator then you have to make change in a respective page where you have defined the locator for it and that change would be reflected everywhere wherever you are using the web element in the entire framework.

POM can be implemented by using the below 2 ways:

  1. POM without Page Factory
  2. POM with Page Factory

The only difference I see between with Page Factory and without Page factory is that with Page Factory we get the Cache lookup feature which allow us to store the frequently used locators in the cache which makes the retrieval performance faster.

Now I will explain that, how we can implement POM without Page Factory? 

First, we need to see that how many pages we have in the Web Application which we are going to automate. Suppose, we are going to Automate facebook.com.

In Facebook, we have the following basic pages.

  • Login Page
  • Forgotten Password
  • Create New Account etc.

In the below example I have created three packages with the following names

  • com.facebook.base
  • com.facebook.pages
  • com.facebook.testcases

👉Inside com.facebook.base package I have created one java class with the name of "StartUp.java" which is the starting point of the execution as it contains 2 annotations @Beforesuite & @Aftersuite. Below is the code which I have written inside the StartUp class.

package com.facebook.base;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import java.util.concurrent.TimeUnit;

public class StartUp
{
public static WebDriver driver;

@BeforeSuite
public static void setUp()
{
System.setProperty("webdriver.chrome.driver","c:/Browser Executables/chromedriver.exe");
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("https://www.facebook.com");

}
@AfterSuite
public static void tearDown()
{
driver.close();
}
}

👉Inside com.facebook.page package I have created a class called LoginPage where I have defined all the locators which belongs to the facebook login page. Now, you have to create another pages for "Forgotten Password" and "Create New Account" where all locators should be defined in similar format like Login Page.

 package com.facebook.pages;


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class LoginPage
{
public WebDriver driver;

//Define all the locators below which are on login page.
By id=By.id("email");
By pwd=By.xpath("//*[@id='pass']");
By btnlogin=By.xpath("//*[@name='login']");

public LoginPage(WebDriver driver)
{
this.driver=driver;
}
public void typeUserName(String UName)
{
driver.findElement(id).sendKeys(UName);
}
public void typePassword(String pass)
{
driver.findElement(pwd).sendKeys(pass);
}
public void clickOnLogin()
{
driver.findElement(btnlogin).click();
}

}

👉Inside com.facebook.testcase package I have created one test case with the name of ValidateFBLogin. Further test cases would be created inside this package for Forgotten Password and Create New Account Scenario. Below is the code for the same.

package com.facebook.testcases;

import com.facebook.base.StartUp;
import com.facebook.pages.LoginPage;
import org.testng.annotations.Test;

public class ValidateFBLogin extends StartUp
{
@Test
public void validatelogin()
{
LoginPage l=new LoginPage(driver);
l.typeUserName("vikas4903");
l.typePassword("ffffffff");
l.clickOnLogin();
}
}

So, this is the way to implement the POM without PageFactory. Now we should see the implementation of POM with Page Factory mode. I have created another page class i.e. "ForgotPwdPage" inside com.facebook.page package.
The following way we use to define the locators in POM with Page Factory.

@FindBy(how=How.XPATH,using="//*[text()='Forgotten password?']")
@CacheLookup
WebElement lnkForgotLink;

@FindBy(id="identify_email")
@CacheLookup
WebElement txtmobile;
Please find the below complete code.
package com.facebook.pages;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;

public class ForgotPwdPage
{

@FindBy(how=How.XPATH,using="//*[text()='Forgotten password?']")
@CacheLookup
WebElement lnkForgotLink;

@FindBy(id="identify_email")
@CacheLookup
WebElement txtmobile;

public void enterData(String mobNumber)
{
lnkForgotLink.click();
txtmobile.sendKeys(mobNumber);
}
}
Now we need to see that how we can call or use the methods inside the com.facebook.testcase package. Please find the below code.

package com.facebook.testcases;

import com.facebook.base.StartUp;
import com.facebook.pages.ForgotPwdPage;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.Test;

public class ValidateForgotFunction extends StartUp
{
@Test
public void ChkFunctionality()
{
ForgotPwdPage f= PageFactory.initElements(driver,ForgotPwdPage.class);
f.enterData("7011516489");
}
}

I will keep adding more automation concepts in my upcoming posts. Please keep me posted if you have any questions and concerns.



Best of Luck.

No comments: