Page Factory
In this blog, I‘ll introduce how to use selenium page factory.
Page factory is the way selenium provided to reuse the web elements.
If you don’t use page factory or the page object model, the test scripts maintenance will take too much effort. The problem is that selenium only gives the page factory to us but does not tell us how to design our framework to use it.
Here is my design:
We will have 3 classes.
- Page: the parent class of all pages.
In the constructor, there are initial elements methods which are provided by selenium page factory. When we use its sub class, we can simply create new “xxxPage”. Java will call this constructor and initialize the elements for us.
- Test Case: the parent class of all test cases.
It defines what to do before and after executing a test case or a test method.
- Driver Manager: the class which handles Webdriver. Use this class can create and destroy the WebDriver.
Here is the code:
Page
public class Page {
/**
* it auto calls by all sub-page
*/
public Page() {
PageFactory.initElements(DriverManager.driver, this);
}
}
Test Case and Driver Manager
TestCase和DriverManager
package simplewebtest.core;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
public class TestCase {
/**
*
* print log
*/
protected Log log = LogFactory.getLog(this.getClass());
/**
* runs by testNG, it will be run before every test method to decide the driver type
*/
@BeforeMethod(alwaysRun=true)
@Parameters(“brwoser”)
protected void testMethodStart(@Optional(“firefox”) String browser){
DriverManager.setDriver(browser);
}
/**
* runs by testNG, it will be run after every test method to close the driver
*/
@AfterMethod(alwaysRun=true)
protected void testMethodEnd(){
DriverManager.quitDriver();;
}
/**
* Print Class Name
*/
@BeforeClass(alwaysRun=true)
protected void testCaseStart(){
log.info(“\\/\\/\\/\\/\\/\\/—TestCase = “+ this.getClass().getSimpleName()+”—\\/\\/\\/\\/\\/\\/”);
}
/**
* Print Class Name again and separator
*/
@AfterClass(alwaysRun=true)
protected void testCaseEnd(){
log.info(“/\\/\\/\\/\\/\\/\\—TestCase = “+ this.getClass().getSimpleName()+”—/\\/\\/\\/\\/\\/\\”);
log.info(“#####################################################”);
}
public static class DriverManager {
/**
shares the same web driver
*/
public static WebDriver driver;
/**
* crate and saves the driver according to the browser type
*/
public static void setDriver(String browser){
if (browser.equals(“firefox”)){
driver = new EventFiringWebDriver(new FirefoxDriver()).register(new LogEventListener());
}
//you can add ie/chrome or other driver here
}
/**
quit the driver
*/
public static void quitDriver(){
driver.quit();
}
}
}
It may be hard to understand. If you wish, you can download my source code and run it step by step. The full code of my small test framework build with selenium is here: