Page Object Module (POM)- Page Factory


Page Object Module- Page Factory


Page Object Model is a Design Pattern which has become popular in Selenium Test Automation.
What is page factory module.
The PageFactory Class in Selenium is an extension to the Page Object design pattern. 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’. Annotations can be used to supply descriptive names of target objects to improve code readability. There is however a few differences between C# and Java implementation – Java provide greater flexibility with PageFactory.

Annotations in PageFactory are like this:

@FindBy(xpath="//input[@name='t1']")
static WebElement userid;

Page factory can be implemented in same class or other class as well, both the examples given below

Same class example-


static WebDriver driver;
@FindBy(xpath="//input[@name='t1']")
static WebElement userid;

@BeforeMethod
public void beformethods()
{
System.setProperty("webdriver.chrome.driver", "C:\\ChromeDriver\\chromedriver_78.exe");
driver= new ChromeDriver();
}
@Test
public void launchUrl()
{
driver.get("file:///D:/Selenium/Website/first.html");
PageFactory.initElements(driver, PageFactory_sameclass.class);
userid.clear();
userid.sendKeys("Yogen");
}


Example of Other class-


public class PageFactory_OtherClass {
WebDriver driver;
@BeforeMethod
public void beformethods()
{
System.setProperty("webdriver.chrome.driver", "C:\\ChromeDriver\\chromedriver_78.exe");
driver= new ChromeDriver();
driver.get("https://google.com");
}
@Test
public void launchBrowser()
{
SubClass sb= new SubClass(driver);
sb.enterSearchText();
}
}
public class SubClass  {
WebDriver driver=null;
@FindBy (xpath="//input[@title='Search']")
WebElement search;
public SubClass(WebDriver driver)
{
this.driver=driver;
PageFactory.initElements(driver, this);
}
public void enterSearchText()
{
search.sendKeys("new search");
}
}

Output-






No comments:

Post a Comment

Software Testing Automation Guide

  get methods in Selenium Webdriver Below are the list of get methods that can be use with driver. Get()- This com...