A quick demo on how to wait for an element in Page Object Factory.

As we know, there are two ways of using page object pattern, one is using “By” and another one is using page object factory (i.e. “FindBy”).

In the PageObjectModel using “By” we can use the explicit and implicit wait for the element to load or to be visible. But this functionality is not available in PageObjectFactory. Hence we are going to use AjaxElementLocatorFactory available in POF.

Here our objective is:

  1. Open the login page of WalletHub (https://wallethub.com/join/login)
  2. Login using credentials


This is our Page Object class for the login page. We are going to use “AjaxElementLocatorFactory ” here to wait for the elements to be visible/load. Following is the code for the same.

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

public class WalletHubLoginPage {
    WebDriver driver;

    @FindBy(xpath = "//*[@id='join-login']/form/div[1]/input")
    WebElement emailField;

    @FindBy(xpath = "//*[@id='join-login']/form/div[2]/input")
    WebElement passwordField;

    @FindBy(xpath = "//button[contains(.,'Login')]")
    WebElement loginBtn;

    public WalletHubLoginPage(WebDriver driver){
        this.driver = driver;
        PageFactory.initElements(new AjaxElementLocatorFactory(driver, 15), this);

    public void login(String username, String password){

In the above code we have imported “org.openqa.selenium.support.pagefactory.AjaxElementLocatorFactory” and have initialized our driver to wait for 15 seconds in iniElements.

PageFactory.initElements(new AjaxElementLocatorFactory(driver, 15), this);

And we are done. Next up is to simply call the “login” class with username and password to login.


This is a demo test class to utilize the above created page object class. Paste the following code in Test.java

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;

public class Test{
    WebDriver driver;
    WebDriverWait wait;
    WalletHubLoginPage objLogin;

    public void loginTest(){
        String username = "username";
        String password = "password";
        System.setProperty("webdriver.chrome.driver", "path to chromedriver/chromedriver.exe");
        driver = new ChromeDriver();
        objLogin = new WalletHubLoginPage(driver);
        objLogin.login(username, password);


And we are done!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s