Introducing the Java PageFactory class
Inside the WebDriver
support library, the PageFactory
class provides a series of annotations that you can use when you create your page objects.
You can use this class to predefine a series of WebElement
objects that can be used later in your test. The PageFactory
class turns these WebElement
objects into proxied objects using a Java Proxy
class. When you try to use them, the annotations that you specify are used to transform these proxied objects into real WebElement
objects that can be used in your tests. We need to do two things to use the PageFactory
class:
- Annotate the variables that we want to proxy
- Initialize the proxied objects before we try to use them
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...