Using PageFactory annotations

We are going to take our existing LoginPage class and convert it to one backed by the Selenium PageFactory class. Let's start with the most common annotation that is in use, the @FindBy annotation. You have a couple of options when it comes to defining them: 

@FindBy(how = How.ID, using = "username")
private WebElement usernameField;

@FindBy(id = "username")
private WebElement usernameField;

The preceding two examples are functionally equivalent and both are correct; it doesn’t matter which one you decide to use.

I personally prefer the first option; I have configured a live template in IntelliJ that creates the annotation and allows me to just press cmd + J (or Ctrl + J if you are not using OS X) and then . (period) to trigger it. I can then press . (period) again and IntelliJ IDEA then gives me a series of options for the How enum.  I don’t have to remember all the selector options and it's very quick and easy for me to create annotations.

The live template I use in IntelliJ IDEA for creating @FindBy annotations is:

@FindBy(how = How$VAR$, using = "$END$")

To learn more about live templates, have a look at https://www.jetbrains.com/idea/help/creating-and-editing-live-templates.html.

Other people will prefer the second, less verbose, option. Either option is perfectly fine.

So, what does the @FindBy annotation do? Well, it is a way to pass a By object into a driver.findElement() call to create a WebElement. This driver.findElement() call is completely transparent to you and will be performed in the background whenever you use a WebElement that has this annotation applied to it. This also means that you are much less likely to get StaleElementReferenceException because the element will be found every time you try to interact with it.

A good general rule of thumb to use when writing page objects using the PageFactory implementation is to make all of the WebElement objects that you define private.  This forces you to write functions in your page objects that interact with the WebElement objects that you have defined, rather than using the page object as a glorified WebElement store.

Let's convert our LoginPage object into one backed by the PageFactory class:

package com.masteringselenium.page_factory_objects;

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

public class LoginPage {

@FindBy(how = How.ID, using = "username")
private WebElement usernameField;

@FindBy(how = How.ID, using = "password")
private WebElement passwordField;

@FindBy(how = How.ID, using = "login")
private WebElement loginButton;

public void logInWithUsernameAndPassword(String username,
String password) throws Exception {
usernameField.sendKeys(username);
passwordField.sendKeys(password);
loginButton.click();
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.200.143