Page Object Model (POM) Framework

A web based project consists of a number of web pages like Home page, Login page, Create Account and Forgot password page etc and each web page consists of a number a web elements. In Page Object Model all web pages are implemented in a separate class and the corresponding elements in a page are implemented as functions in the class. In this approach functions/elements of page/class cab be reused which supports reusability of code. Apart from this, if the UI for any page changes in future, it doesn’t require to change any tests, we just need to change  the code within the corresponding page class (Only at one place).

Without PageObjectModel:

package package1;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TestClass {
      
       public static WebDriver driver;
       public static String URL="https://www.facebook.com/";
      
       //Login Page
       public static By username= By.id("email");
       public static  By password= By.id("pass");
       public static By acclogin= By.id("u_0_2");
      
       public static void main(String[] args) throws Exception {
              // TODO Auto-generated method stub

              //Firefox
              System.setProperty("webdriver.gecko.driver", "D:\\Automation\\AutomationFramework\\TestFramework\\Lib\\BrowserDrivers\\geckodriver-v0.18.0-win32\\geckodriver.exe");
              driver=new FirefoxDriver();
             
              driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
              driver.get(URL);

              //login Page
             
              driver.findElement(username).sendKeys("xxx@gmail.com");
              driver.findElement(password).sendKeys("xxxxx");
              driver.findElement(acclogin).click();
             
              driver.close();
             
       }


}

Page Object Model without Page Factory
LogInPage.java
package PageObject;
import org.openqa.selenium.*;

 public class LogInPage {
      
       public WebDriver driver;
      
       //Login Page
       public By email=By.id("email");
       public By password=By.id("pass");
       public By login=By.xpath("u_0_2");
      
       public LogInPage(WebDriver driver){
              this.driver=driver;
       }
      
       public void txtBox_UserName(String emailid){
             
              driver.findElement(email).sendKeys(emailid)
       }
       public void txtBox_Password(String pass){
             
              driver.findElement(password).sendKeys(pass);   
       }
       public void btn_LogIn(){
      
              driver.findElement(login).click();
       }


}

DriverScript.java

package ExecutionEngine; 
import java.util.concurrent.TimeUnit;
import PageObject.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
  
public class DriverScript {
      
       public static WebDriver driver;  
       public static String URL="https://www.facebook.com/";

public String emailid=”xxx@gmail.com”;
       public String password="xxxxxx";

      
       public static void main(String[] args) throws Exception{
                          
              System.setProperty("webdriver.gecko.driver", "D:\\Automation\\AutomationFramework\\TestFramework\\Lib\\BrowserDrivers\\geckodriver-v0.18.0-win32\\geckodriver.exe");
              driver=new FirefoxDriver();
             
              driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
              driver.get(URL);

              LogInPage lp=new LogInPage(driver);
              lp.txtBox_UserName(emailid);
              lp.txtBox_Password(password);
              lp.btn_LogIn();
             
              driver.quit();
       }


}

Screenshot of project structure
                               


Page Object Model with Page Factory

LogInPage.java
package pageObjects;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.How;

public class LoginPage {
            public static WebDriver driver;
            public LoginPage(){
                        this.driver=driver;
            }
           
            @FindBy(how=How.ID,using="email")
            public WebElement txtBox_UserName;
           
            @FindBy(how=How.ID, using="pass")
            public WebElement txtBox_Password;
           
            @FindBy(how=How.ID, using="u_0_2")
            public WebElement btn_LogIn;


}

DriverScript.java

package executionEngine;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.support.PageFactory;
import pageObjects.LoginPage;

public class DriverScript {

            public static WebDriver driver;           
            public static String URL="https://facebook.com/";
public String emailid=”xxx@gmail.com”;
       public String password="xxxxx";

            public static void main(String[] args) throws Exception{
                        
                        System.setProperty("webdriver.gecko.driver", " D:\Automation\AutomationFramework\PageObjcetWithPageFactory\Lib\BrowserDrivers\geckodriver-v0.14.0-win32\\ geckodriver.exe");
                                              
                        driver=new FirefoxDriver();                      
                        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                        driver.get(URL);
                       
                        // This is to Instantiate Home Page
                        LoginPage login=PageFactory.initElements(driver, LoginPage.class);
                        login.txtBox_UserName.sendKeys(emailid);
                        login.txtBox_Password.sendKeys("password");
                        login.btn_LogIn.click();

            }


}

No comments:

Post a Comment