POM, PageFactory Automation framework in selenium
WebDriver supports POM (Page Object Model) via PageFactory class. In order to use PageFactory, all the elements needs to be declared on a PageObject as “WebElement” or “List<WebElement>” as follow:
import org.openqa.selenium.WebElement;
public class GoogleSearchPage {
// Here’s the element
@FindBy(how = How.NAME, using = “q”)
private WebElement searchBox;
public void searchFor(String text) {
// And here we use WebElement as defined above.
searchBox.sendKeys(text);
searchBox.submit();
}
}
In order to execute above code, we need to Initialize PageObjects as follow:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.support.PageFactory;
public class UsingGoogleSearchPage {
public static void main(String[] args) {
// Create a new instance of a driver
WebDriver driver = new HtmlUnitDriver();
// Navigate to the right place
driver.get(“http://www.google.com/”);
// Create a new instance of the search page class
// and initialise any WebElement fields in it.
GoogleSearchPage page = PageFactory.initElements(driver, GoogleSearchPage.class);
// And now do the search.
page.searchFor(“Cheese”);
}
}
Explanation
The PageFactory relies on using sensible defaults: the name of the field in the Java class is assumed to be the “id” or “name” of the element on the HTML page. That is, in the example above, the line:
q.sendKeys(text);
is equivalent to:
driver.findElement(By.id("q")).sendKeys(text);
0 comments