PageFactory is an extended support to POM (PageObjectModel), used to locate webelement in adifferent manner than mentioned here.
Before we learned to declare and locate an WebElement as below :
WebElement txt_GoogleSrch = driver.findElement(By.id("q"));
Using PageFactory
@FindBy
@FindBy(ID = "q")
private WebElement txt_GoogleSrch;
or this can even read as,
@FindBy(how = How.ID, using = "q")
private WebElement txt_GoogleSrch;
The above 2 lines are not sufficient to locate, we need to instantiate the class object through PageFactory, so this is as follows
PageFactory.initElements(driver, this)
“this”, as we want to instantiate the webelement is the same class.
If the pagefactory elements are in different class or java file, let’s see how we can access
LoginPage.java – page object class that holds all the weblements and methods related to login page
public class LoginPage { @FindBy(name="userName") private WebElement username; @FindBy(name="password") private WebElement password; @FindBy(name="login") private WebElement login; public void Login() { username.sendKeys("mercury"); password.sendKeys("mercury"); login.click(); } public String getLoginPageTitle(WebDriver driver) { return driver.getTitle(); } }
LoginPageTest.java – this is the test case which actually instantiate LoginPage class object and weblements
public class LoginTest { static WebDriver driver; LoginPage LoginPage; @BeforeTest public void setup() { System.setProperty("webdriver.gecko.driver","c:\\Grid\\geckodriver.exe"); driver = new FirefoxDriver(); driver.get("http://newtours.demoaut.com/"); LoginPage = PageFactory.initElements(driver, LoginPage.class); } @Test public void LandingPage() { Assert.assertEquals(LoginPage.getLoginPageTitle(driver), "Welcome: Mercury Tours"); } @Test public void validLogin() { LoginPage.Login(); Assert.assertTrue(LoginPage.getLoginPageTitle(driver).contains("Find a Flight: Mercury Tours")); } }
some more PageFactory insights
Supported locators
@FindBy(“locatorName”, “value”)
locator name could be any one of the following:ID, CLASS_NAME, NAME, CSS, PARTIAL_LINK_TEXT, TAG_NAME, XPATH
@CacheLookup
Every time we use (use only, not declaring multiple times) the WebElement, it searches if element present or not,
but if we are sure element is present on current page, then use @CacheLookup to avoid checking the presence and save some time… code as follows
@FindBy(NAME = "btnG") @CacheLookup private WebElement txt_GoogleSrch;
@FindBys
This will return list of webElements matching all the conditions [AND operator]
@FindBys({ @FindBy(id = "idval"), @FindBy(name = "nameval"), @FindBy(classname= "classval") }) private List<WebElement> IdAndNameAndClassName
@FindAll
This will return list of webElements matching any of the [at least one] condition [OR operator]
@FindAll({ @FindBy(id = "idval"), @FindBy(name = "nameval"), @FindBy(classname= "classval") }) private List<WebElement> IdORNameORClassName
Reference : https://github.com/SeleniumHQ/selenium/wiki/PageFactory
1 Comment