Saturday, January 17, 2015

PageFactory In Selenium WebDriver

PageFactory is a way to implement Page Object Model in Selenium Webdriver.

In PageFactory mainly there are two things

1. PageFactory.initElements Method
2. @FindBy annotation

There are lots of other annotation(Like @FindAll,@FindBys,@Cache etc ) which we will learn later.


1. PageFactory.initElements Method

initElements are static methods of PageFactory class.

If we use this methods then we can assign value to the webelement at runtime when it is required. That means class loaded with WebElements having  some proxy(garbage or default) value and when actually it is required ,means call form method or accessing the WebElement in any way then it will find the WebElement into the page and assign the actual value to this WebElement. initelements method only assign value to the WebElement which is required currently it is not load the all webelement which is declared in page object class.

2. @FindBy annotation

Used to mark a field on a Page Object to indicate an alternative mechanism for locating the element or a list of elements. This allows users to quickly and easily create PageObjects.
You can either use this annotation by specifying both "how" and "using" or by specifying one of the location strategies (eg: "name") with an appropriate value to use.

e.g
 @FindBy(name = "q") 
  WebElement searchTxtBox;
 
 @FindBy(how = How.NAME, using = "q") 
  WebElement searchTxtBox;
 
Example Code is given below:
 
  //***************************************
  // BLOGPAGE Class
  //*************************************** 

package com.core.pageobject;

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

public class BlogPage {
 
  //***************************************
  // Use @FindBy Annotation To Locate Elements
  // Within Blog Page
  //***************************************  
    @FindBy(name = "txtbox1")
    private WebElement inputBox;
    @FindBy(name = "btnsub")
    private WebElement submitBtn;
    @FindBy(linkText = "Google")
    private WebElement googleLink;
 
    private final WebDriver driver;

    public BlogPage(WebDriver driver) {
        this.driver = driver;
    }
  //***************************************
  // BlogPage Have A Submit Button After
  // Click On That It Stay On Same Page So
  // Return Same Page Object
  //*************************************** 
 
    public BlogPage submitForm(String inputTxt) {
        inputBox.sendKeys(inputTxt);
        submitBtn.click();
        return PageFactory.initElements(driver, BlogPage.class);
    }
  //***************************************
  // BlogPage Have A Google Page Link After
  // Click On That It Redirect To Google Page
  // So Return Google Page Object
  //***************************************  
 
    public GooglePage clickonGoogleLink() {
        googleLink.click();
        return PageFactory.initElements(driver, GooglePage.class);
    }
}
 
  //***************************************
  // GOOGLEPAGE Class
  //***************************************
 
package com.core.pageobject;

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

public class GooglePage {
 
  //***************************************
  // Use @FindBy Annotation To Locate Elements
  // Within Google Page
  //***************************************  
    
    private final WebDriver driver;
    @FindBy(name = "q")
    WebElement searchTxtBox;

    public GooglePage(WebDriver driver) {
        this.driver = driver;
    }
 
  //***************************************
  // Searching Anything On Google Page remain
  // on Same Google Page So Return Type Is
  // Google Page Object.
  //*************************************** 
 
    public GooglePage searchTxt(String searchTxt) {
        searchTxtBox.sendKeys(searchTxt);
        return PageFactory.initElements(driver, GooglePage.class);
    }
} 
 
  //***************************************
  // Test Class
  //***************************************

package com.core.pageobject;

import java.io.File;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class PageObjectModelTest {

    WebDriver driver;

    @BeforeTest
    public void testSetUp() {
        FirefoxBinary bin = new FirefoxBinary(new File("G:\\Program Files\\Mozilla Firefox\\firefox.exe"));
        FirefoxProfile prof = new FirefoxProfile();
        driver = new FirefoxDriver(bin, prof);
    }

    @BeforeMethod
    public void testMethodSetUp() {
        driver.get("http://startingwithseleniumwebdriver.blogspot.in/2013/12/frmset1.html");
    }

    @Test
    public void testSubmit() throws Exception {
        BlogPage blog = PageFactory.initElements(driver, BlogPage.class);
        blog.submitForm("Test");
    }

    @Test
    public void testGoogleLink() throws Exception {
        BlogPage blog = PageFactory.initElements(driver, BlogPage.class);
        blog.clickonGoogleLink().searchTxt("Test");
    }

    @AfterTest
    public void tearDown() {
        driver.quit();
    }
}

 
 
We can create a another pageobject class for google search result here we make 
one pageobject class for google page and google search result.

No comments:

Post a Comment