Wednesday, 14 November 2018

Selenium Frameworks - POM Framework (Page Object Model Framework)

POM Framework:
In Page Object Model framework, Object identification code and validation code are separated
Package01 (Object Repository)
Class-1
{page01: webelements identification code}
Class-2
{page02: webelements identification code}
Package02 (TestCases)
{Validation Code}
 









In this approach we create individual classes for every page webelements with identification code and then in each class we create individual functions for each Webelement to return identification value for Webelement
Advantages of POM:
-In future or next versions if the object properties are changed, then we need to update only Webelement identification code (i.e. Class-1)
-it will Increase Readability of the script
-it will reduces duplication of code
-it is easy for maintainability


There are 2 types of approaches in POM framework
            i. using By class
            ii. **using PageFactory annotation @FindBy


i.                   Using By class:
By- is a static class provided in WD to assign webelement locator values
Syntax:
By obj= By.locatorName(Locator value);

Exp:
Create script to check login functionality in mercury tours web application using POM framework with help of By class
Procedure:
Step1:
create 2 packages
      package-1: "object.repository"
      package-2: "test.cases"
Step 2:
i.                    create non-executable class in "object.repository" package with
name as "MtHomePg"
ii.                  assign webelement identification locator values to variables
      syntax:           By obj= By.locatorName("Locator value");
iii.               create individual methods/functions to return each webelement
identification value
      syntax:
      public WebElement methodName(WebDriver driver){
                  return (driver.findElement(obj));
      }

Script:
package object.repository;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class MtHomePg {
     
      //to assign locator values
      By userid=By.name("userName");
      By pwd=By.name("password");
      By signin= By.name("login");
     
      public WebElement uidObj(WebDriver driver){
                  return(driver.findElement(userid));
      }
     
      public WebElement pwdObj(WebDriver driver){
                  return(driver.findElement(pwd));
      }
     
      public WebElement signinObj(WebDriver driver){
                  return(driver.findElement(signin));
      }

}


Step 3: create class in "test.cases" package and write script to initialize
 browser and to perform login validation
package test.cases;

public class LoginValidation extends MtHomePg{

      public static void main(String[] args) throws InterruptedException {
                  WebDriver driver= new FirefoxDriver();
                  driver.get("http://newtours.demoaut.com");
                  driver.manage().window().maximize();
                 
                  //to perform login operation
                  LoginValidation lv= new LoginValidation();
                  lv.uidObj(driver).sendKeys("mercury");
                  lv.pwdObj(driver).sendKeys("mercury");
                  lv.signinObj(driver).click();
                 
                  Thread.sleep(3000);
                 
                  String pgTitle= driver.getTitle();
                  if (pgTitle.contains("Find a Flight")){
                              System.out.println("Successful login operation");
                  }
                  else{
                              System.out.println("Unsuccessful login operation");
                  }
                 

      }

}
---------------------------------------------------------------------------------
POM framework using PageFactory annotation @FindBy
==================================================
Exp:
create script to validate login functionality in http://rediffmail.com
application using POM Framework with PageFactory annotation

Procedure:
Step1:
create 2 packages
      package-1: "object.repository"
      package-2: "test.cases"
Step 2:
create 2 non-executable classes in "object.repository" package
class-1: RediffHomePg
Class-2: RediffLoginPg

Step 3:
assign home page webelement locator value to the object
using @FindBy annotation in "RediffHomePg" class
syntax:
      @FindBy(Locator="Locator value")
      WebElement obj;


Step 4:
create method/function to return obj from "RediffHomePg" class
syntax:
      public WebElement methodname(WebDriver driver){
                  return(obj);
      }

Script in "RediffHomePg" class
----------------------------------------
public class RediffHomePg {
     
      //Assign locator value
      @FindBy(linkText="Sign in")
      WebElement signin;
     
      //to return obj
      public WebElement signinObj(WebDriver driver){
                  return(signin);
      }

}

Step 5: assign Login page webelement locator values to the objects
using @FindBy annotation in "RediffLoginPg" class and create
method/functions to return each obj

class
script in "RediffLoginPg" class
--------------------------------
public class RediffLoginPg {

      //Assign locator value
                  @FindBy(name="login")
                  WebElement emailId;
                 
                  //Assign locator value
                  @FindBy(name="passwd")
                  WebElement pwd;
                 
                  //Assign locator value
                  @FindBy(name="proceed")
                  WebElement go;
                 
                  //to return obj
                  public WebElement emailObj(WebDriver driver){
                              return(emailId);
                  }
                  public WebElement pwdObj(WebDriver driver){
                              return(pwd);
                  }
                  public WebElement goObj(WebDriver driver){
                              return(go);
                  }
}
Step 6: create class in "test.cases" package to validate login functionality
public class RediffLoginValidation {

      public static void main(String[] args) throws InterruptedException {
                  WebDriver driver= new FirefoxDriver();
                  driver.get("http://rediffmail.com");
                  driver.manage().window().maximize();
                 
                  //to initialize objects from "object.repository" package classes
                  RediffHomePg RHP= PageFactory.initElements(driver,
RediffHomePg.class);
                  RediffLoginPg RLP= PageFactory.initElements(driver,
RediffLoginPg.class);
                 
                  RHP.signinObj(driver).click();
                  RLP.emailObj(driver).sendKeys("SampleUserID");
                  RLP.pwdObj(driver).sendKeys("Password");
                  RLP.goObj(driver).click();
                 
                  Thread.sleep(3000);
                 
                  String pgTitle= driver.getTitle();
                  if (pgTitle.contains("Inbox")){
                              System.out.println("Successful login operation");
                  }
                  else{
                              System.out.println("Unsuccessful login operation");
                              }
      }
}
=============================================================

No comments:

Post a Comment