Page Factory in Selenium
In order to provide additional support for the Page Object pattern, the package ‘org.openqa.selenium’ has a PageFactory Class. One can import the PageFactory class in Java using the following statement
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.PageFactory;
By importing this package we can use multiple pages just we have to make an object of that class.
Let me take an example
Here If my page name is Login then i will call as
Login l = PageFactory.initElements(driver, Login.class);
Generally we make the object of that class and use but in page factory we use init method.
You can also this Page Factory as Reference.
In page factory we define Webelements like this.
WebElement firstname;
how = How.XPATH, [We can define locators as How.ID,How.name,How.css etc]
In using we have to define which locator we are using id,xpath name,css etc.
using = //*[@value='First name']
If we use locator By name Then we will write
@FindBy(how = How.NAME, using = "firstname")
We don't used driver.findelements();
We simply call these as:
firstname.sendKeys("Ritika");
How to implement Page factory
In page factory we define Webelements like this.
Annotations in PageFactory are like this:
We use annotation@FindBy
If we use locator as xpath then we will write
@FindBy(how = How.XPATH, using = "//*[@value='First name']")WebElement firstname;
how = How.XPATH, [We can define locators as How.ID,How.name,How.css etc]
In using we have to define which locator we are using id,xpath name,css etc.
using = //*[@value='First name']
If we use locator By name Then we will write
@FindBy(how = How.NAME, using = "firstname")
WebElement firstname;
We don't used driver.findelements();
We simply call these as:
firstname.sendKeys("Ritika");
How to implement Page factory
- Create a ‘New Package file and name it as ‘pageobject’ by right click on the Project and select New > Package.
- Create a ‘New Class’ file and refer the name to the actual page from the test object, by right click on the above created Package and select New > Class. In our case it as FacebookRegistration
package pageobject;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.ui.Select;
public class FacebookRegistration {
WebDriver driver;
public FacebookRegistration(WebDriver ldriver){
this.driver= ldriver;
}
@FindBy(how = How.XPATH, using = "//*[@value='First name']")
WebElement firstname;
@FindBy(how = How.XPATH, using = "//*[@value='Last name']")
WebElement lastname;
@FindBy(how = How.XPATH, using = "//*[@value='Email or mobile number']")
WebElement email;
@FindBy(how = How.XPATH, using = "//*[@value='Re-enter email or mobile number']")
WebElement reemail;
@FindBy(how = How.XPATH, using = "//*[@value='New password']")
WebElement NewPassword;
@FindBy(how = How.XPATH, using = "//*[@id='month']")
WebElement month;
@FindBy(how = How.XPATH, using = "//*[@id='day']")
WebElement day;
@FindBy(how = How.XPATH, using = "//*[@id='year']")
WebElement year;
@FindBy(how = How.XPATH, using = "//*[@id='u_0_d']")
WebElement female;
@FindBy(how = How.XPATH, using = "//button[@name='websubmit']")
WebElement signup;
// Type firstname
public void typefirstname(String fname) {
firstname.sendKeys(fname);
}
// Type lastname
public void typelastname(String lstname) {
lastname.sendKeys(lstname);
}
// Type email
public void typeemail(String emailid) {
email.sendKeys(emailid);
}//Typereemail
public void typeremail(String remailid) {
reemail.sendKeys(remailid);
}
//typenewpassword
public void typenewPassword(String newpassrd) {
NewPassword.sendKeys(newpassrd);
}
//selectmonth
public void selectmonth(String mont){
month.sendKeys(mont);
}
//Select date
public void selectdate(String date){
day.sendKeys(date);
}
//selecting year
public void selectyear(String yea){
year.sendKeys(yea);
}
//clicking on femaleradiobutton
public void ClickingGender(){
female.click();
}
//clicking sign up button
public void clicksignup(){
signup.click();
}
}
/In this We have created a Registration page of Facebook Now we need to call this page in our Testcase.Generally we create pages in page object factory and after that we call that pages in Testcase.
- Create a ‘New Class‘ and name it as TestCase by right click on the ‘pageobjectFramework‘ Package and select New > Class.
package pageobjectframework;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import pageobject.FacebookRegistration;
public class TestCase {
WebDriver driver;
@BeforeTest
public void openBrowser(){
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("http://www.facebook.com");
}
//This test will fill the sign up page of facebook login
@Test
public void registerDetails(){
//By this init method we can call our pages in Page factory.
FacebookRegistration fo = PageFactory.initElements(driver, FacebookRegistration.class);
fo.typefirstname("Ritika");
fo.typelastname("Gulati");
fo.typenewPassword("seleniumgyan");
fo.typeemail("submityouremailid@gmail.com");
fo.typeremail("submityouremailid@gmail.com");
fo.selectmonth("Jul");
fo.selectdate("22");
fo.selectyear("1991");
fo.ClickingGender();
fo.clicksignup();
}}

















