Site icon TestingDocs.com

Learning Selenium PageFactory

Overview

In this post, we will discuss about Selenium PageFactory. PageFactory class makes using Page Objects simpler and easier. We can call the ‘initElements’ method of the Webdriver specific PageFactory class, which declares a proxy to the page objects found in the class. The elements in the page objects class are initialized with a lazy proxy, which means that the compiler will look if the element exists on the page only when they will be used

initElements(WebDriver driver, Class<T> pageClassToProxy)

Selenium PageFactory Example

pageObjectInstance = PageFactory.initElements(webDriver, SamplePageObject.class);

We need to initialize the page object class, so that the elements declared in it can be used to interact with the page .webDriver is the browser instance; ‘SamplePageObject’ is the class where the page object elements are declared.

PageFactory has other overloaded versions of ‘initElements’ method has shown in the below picture.

 

 

By default, the element or the list is looked up each and every time a method is called upon it. To change this behavior, simply annotate the field with the CacheLookup. We can change how the element is located, by using the FindBy annotation.

Sample Code

package com.testingdocs.selenium.demo;

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

public class LoginPage 
{
  WebDriver driver ;
  @FindBy(how=How.ID , using="username")
  WebElement username;
  
  @FindBy(how=How.ID , using="password")
  WebElement password;
  
  @FindBy(how=How.ID , using="loginSubmit")
  WebElement loginSubmit ;

  /**
   * @param driver
   */
  public LoginPage(WebDriver driver) {
    this.driver = driver;
    
  }
  
  public void login()
  {
    driver.get("sample_login_url");
    username.sendKeys("demo");
    password.sendKeys("demo123");
    loginSubmit.click();
  }
  
}

 

See also to learn more about FindBy annotation : Page Object FindBy Annotation

This method will attempt to instantiate the class given to it, preferably using a constructor which takes a WebDriver instance as its only argument or falling back on a no-arg constructor.An exception will be thrown if the class cannot be instantiated.

After the page object class initialization, you can start writing the tests. In the tests, interaction with the web elements is done by using the appropriate page object methods as shown below sample test.

Sample Test

package com.testingdocs.selenium.demo;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.Test;

public class LoginPageTest 
{
  WebDriver driver = new FirefoxDriver();
  LoginPage loginPage ;
  
  @Test()
  public void samplelogintest()
  {
  loginPage = PageFactory.initElements(driver, LoginPage.class);
  loginPage.login();
  }

}

 

PageFactory class instantiates the login page object and then populates all the web elements annotated with the FindBy annotation.

More Information on Selenium

https://www.selenium.dev/

Exit mobile version