Understanding PageFactory

Another important class that the WebDriver library provides to support the PageObject pattern is the PageFactory class. Once the PageObject class declares elements using the FindBy annotation, you can instantiate that PageObject class and its elements using the PageFactory class. This class supports a static method named initElements. The API syntax for this method is as follows:

initElements(WebDriver driver, java.lang.Class PageObjectClass)

Now, let's see how this can be used in our test case to create AdminLoginPage:

public class TestAddNewPostUsingPageObjects { public static void main(String... args){AdminLoginPage loginPage= PageFactory.initElements(driver, AdminLoginPage.class);loginPage.login();

The PageFactory ...

Get Selenium WebDriver 3 Practical Guide - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.