Page Factory class involves the following:
- Defining all the HTML elements on the HTML Page
- Getting XPATH Id’s of all the page elements
- Writing Methods that involve interaction of the elements that can be used on the actual Test Class Page
Code Sample:
import org.junit.Assert;
import org.openqa.selenium.JavascriptExecutor;
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.PageFactory;
import org.openqa.selenium.support.pagefactory.AjaxElementLocatorFactory;
public class PageFactoryClass
{
@FindBy( how = How.ID, using = “gwt-debug-button” )
WebElement _button;
@FindBy( how = How.ID, using = “gwt-debug-resultTextBox” )
WebElement _resultTextBox;
@FindBy( how = How.ID, using = “gwt-debug-buttonWithConfirm” )
WebElement _buttonWithConfirm;
@FindBy( how = How.ID, using = “gwt-debug-confirmationResultTextBox” )
WebElement _confirmationResultTextBox;
@FindBy( how = How.ID, using = “gwt-debug-buttonWithAlert” )
private WebElement _buttonWithAlert;
@FindBy( how = How.ID, using = “gwt-debug-alertResultTextBox” )
WebElement _alertResultTextBox;
@FindBy( how = How.XPATH, using = “//div/a[text()='AnchorButton']” )
WebElement _anchorButtonLink;
public WebDriver _driver;
public ButtonTestPage( WebDriver driver )
{
_driver = driver;
PageFactory.initElements( new AjaxElementLocatorFactory( _driver, 15 ), this );
}
//Sample method that can be reused in the Test class
public void clickOnButtonHandlerFires()
{
_button.click();
Assert.assertEquals( “Button Clicked”, _resultTextBox.getValue() );
}
}
}