Monday 29 October 2018

Page Factory

What is Page Factory?

Page Factory is an inbuilt Page Object Model concept for Selenium WebDriver but it is very optimized.


Here as well, we follow the concept of separation of Page Object Repository and Test Methods. Additionally, with the help of PageFactory class, we use annotations @FindBy to find WebElement. We use initElements method to initialize web elements

EXAMPLE :

step:1 Create two new packages and give name pages and tests
           right click on src->new->packages

step:2 Crate pom of pages
           right click on pages->new-.class
            here you give class name "Loginpage"

Login page using page factory

--------------------------------------------------------------------------------------------------------------------
public class Loginpage {

 WebDriver driver;

@FindBy(xpath="html/body/div[1]/div[3]/div/div/div/div/div[2]/form/table/tbody/tr[2]/td[1]/in  put"]")
WebElement mail;

 @FindBy(xpath="html/body/div[1]/div[3]/div/div/div/div/div[2]/form/table/tbody/tr[2]/td[2]/input"]");
WebElement pass;

@FindBy(xpath="html/body/div[1]/div[3]/div/div/div/div/div[2]/form/table/tbody/tr[2]/td[3]/label/input");
WebElement login;

public Homepage(WebDriver driver)
{
this.driver = driver;
PageFactory.initElements(driver, this);
                

}
//create constructor of class with pagefactory

public void getmail(String mailid)
{
mail.sendKeys(mailid);

}
//make method of get value of emaild id

public void getpass(String password)
{
mail.sendKeys(password);

}


public void log()
{
               login.click();
}
//make method for click on loging button

//now we make one common method who provide all in one

public void logintofb(String mailid, String password)
{
this.getmail(mailid);
                this.getpass(password);
this.log();
}


--------------------------------------------------------------------------------------------------------------------
now we create all pages which need in testing

Step:4 make main java class in tests package



package tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import pages.Loginpage;
import pages.fbhomepage;

public class Setup {

public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver","D:\\vishal\\selenium\\chromedriver_win32\\dasdf\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.get("https://www.facebook.com");
Loginpage objlog;
                 //create object of page
objlog = new Loginpage(driver);
objlog.logintomail("vishallaniya1997", "vlaniya@123");
Thread.sleep(3000);
driver.close();

}

}


No comments:

Post a Comment

Jmeter

Database Testing: (1) Add mysql JDBC to Jmeter lib folder and restart Jmeter (2) Right click on test name -> add  Thread group (3)...