Page Factory example

The Page Factory Class is an extension to the Page Object design pattern. It is an inbuilt page object model concept for Selenium WebDriver but it is very optimized. It is used to initialize the elements of the Page Object or instantiate the Page Objects itself. Annotations for elements can also be created (and recommended) as the describing properties may not always be descriptive enough to tell one object from the other.

It is used to initialize elements of a Page class without having to use ‘FindElement’ or ‘FindElements’. This can be done simply through the use of initElements function on PageFactory. Annotations can be used to supply descriptive names of target objects to improve code readability.

Page Factory will initialize every WebElement variable with a reference to a corresponding element on the actual web page based on configured “locators”. Some of the most commonly used annotations are:

@FindBy: With this annotation, we can define a strategy for looking up the element, along with the necessary information for identifying it

@CacheLookup: If we know that element is always present on the page and is not going to change then we can cache the looked up field by using this annotation in order to make the code more efficient. If we don’t do it, then every time when we turn to our element, WebDriver will check if the element is present on the page.

@FindBys: This annotation comes in handy when we are dealing with a list of elements on the page.

@FindAll: This annotation is used with multiple @FindBy annotations to look for elements that match any of the given locators

This is how you can create Page Factory class:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import java.util.List;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindAll;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.FindBys;
import org.openqa.selenium.support.How;

public class pomExPF {
/*
 * I will be listing Page Factory methods that I have used
 * For some methods I am using gmail example.
*/
 WebDriver driver;
 
 @FindBy(id="some id") //identifies the element
//USE THIS @CacheLookup annotation ONLY IF THE ELEMENT IS NOT GOING TO CHANGE 
 @CacheLookup  
 WebElement someName1; //assigns the element to a variable

//Using 'how' class 
 @FindBy(how=How.CLASS_NAME,using="gmail-nav__nav-link gmail-nav__nav-link__sign-in")
 @CacheLookup
 WebElement SignIn;
 
 //@FindBy(how=How.NAME,using="signIn")
 @FindBy(name="signIn") // another way to write the same code as above
 @CacheLookup
 WebElement next;
 
 //@FindBy(id="Email")
 @FindBy(how=How.ID,using="Email")
 @CacheLookup
 WebElement username;
 
 @FindBy(how=How.ID_OR_NAME,using="some id or name")
 @CacheLookup
 WebElement someName4;
 
 @FindBy(how=How.LINK_TEXT,using="some link text")
 @CacheLookup
 WebElement someName5;
 
 @FindBy(how=How.PARTIAL_LINK_TEXT,using="some partial link text")
 @CacheLookup
 WebElement someName7;
 
 @FindBy(how=How.TAG_NAME,using="some tag name")
 @CacheLookup
 static WebElement someName8;
 
 @FindBy(how=How.XPATH,using=".//*[@id='Passwd']")
 @CacheLookup
 public WebElement password;
 
 @FindBy(css="#some css content")
 private WebElement text;
 
 @FindBys(@FindBy(css="div[class='cssClass1 cssClass2']"))
 private List<WebElement> cssList;
 
 @FindAll({@FindBy(how=How.ID, using="username"),
  @FindBy(className="username-field")})
 private WebElement user_name;

//Now lets create a constructor to call this reusable class 
 
 public pomExPF(WebDriver localDriver)
 { 
  this.driver=localDriver;  
 }
 
//Now lets create methods to use the elements created above 
 
 public void logintoGmail(String uid, String pwd)
 {
  SignIn.click();
  username.clear();
  username.sendKeys(uid);
  next.click();
  password.clear();
  password.sendKeys(pwd);
 }
}


And this is how to use this reusable method:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
// Sample to call the reusable Page Factory class 
@Test
public void pfLoginSample()
{
//Initializing and launch driver with user parameters   
driver = browserFactory.startBrowser("firefox", "https://www.gmail.com");
  
//This returns the page object of a particular page using PageFactory
pomExPF loginPagePF = PageFactory.initElements(driver, pomExPF.class);

//This how to create another object for another page  
pomExample loginPage = PageFactory.initElements(driver, pomExample.class); 
  
//Calling method and passing parameters
loginPagePF.logintoGmail("username", "password");
}


Page Factory wiki can be found here. By far Page Factory is the most crisp and efficient method to create re-usable object repository that is easy to maintain and manipulate.



No comments:

Post a Comment