White Box Testing
Test your code
   Home      selenium2WebDriver-PageFactory
 
Page factory pattern
 
Factory class to make using Page Objects simpler and easier. Go through Page Object Pattern before going with Page factory pattern

The WebElements in a PageFactroy are really proxies to WebElements. This means that every time you access a WebElement it will perform a search to find the element on the page.

This has some advantages:

  • When the PageFactory is initialised the proxies are configured, but the WebElements are not found at that point (so you won't get a NoSuchElementException)
  • Every time you use a WebElement it will go and find it again so you shouldn't se StaleElementException's
 
 
@FindBy
 
Used to mark a field on a Page Object to indicate an alternative mechanism for locating the element or a list of elements. Used in conjunction withPageFactory this allows users to quickly and easily create PageObjects.

You can either use this annotation by specifying both "how" and "using" or by specifying one of the location strategies (eg: "id") with an appropriate value to use. Both options will delegate down to the matching By methods in By class. For example, these two annotations point to the same element:

 @FindBy(id = "foobar") WebElement foobar;
 @FindBy(how = How.ID, using = "foobar") WebElement foobar;
and these two annotations point to the same list of elements:
 @FindBy(tagName = "a") List links;
 @FindBy(how = How.TAG_NAME, using = "a") List links;
 
 
PageFactory.initElements
 
Instantiate an instance of the given class, and set a lazy proxy for each of the WebElement and List fields that have been declared, assuming that the field name is also the HTML element's "id" or "name". This means that for the class: public class Page { private WebElement submit; } there will be an element that can be located using the xpath expression "//*[@id='submit']" or "//*[@name='submit']" By default, the element or the list is looked up each and every time a method is called upon it. To change this behaviour, simply annotate the field with the CacheLookup. To change how the element is located, use the FindBy annotation. This method will attempt to instantiate the class given to it, preferably using a constructor which takes a WebDriver instance as its only argument or falling back on a no-arg constructor. An exception will be thrown if the class cannot be instantiated.
 
 
 For http://www.whiteboxtest.com/selenium-test1.php page following changes done in page object class
    @FindBy(how = How.ID, using = "id-012")
    WebElement verifybutton;

    public void clickButtonPresentByID_id012() {
        verifybutton.click();
    }

    // Find and click selenium-test2
    public SeleniumTest2 clickSeleniumTest2() {
        // Find the text link by its name
        WebElement someElement = driver.findElement(By.linkText("selenium-test2"));
       
        // Click the link text
        someElement.click();
       
        return PageFactory.initElements(driver, SeleniumTest2.class);
    }
  
 
For http://www.whiteboxtest.com/selenium-test2.php page following changes done in page object class
 
    @FindBy(how = How.ID, using = "q2")
    WebElement textBox;
   
    // Find the text box by ID
    public void inputTextBox(String txt) {
        textBox.clear();
        textBox.sendKeys(txt);
    }
   
    // Find and click selenium-test1
    public SeleniumTest1 clickSeleniumTest1() {
        // Find the text link by its name
        WebElement someElement = driver.findElement(By.linkText("selenium-test1"));
       
        // Click the link text
        someElement.click();
       
        return PageFactory.initElements(driver, SeleniumTest1.class);
    }
 
Test cases code following changes done
 
        SeleniumTest1 seleniumTest1 = PageFactory.initElements(driver, SeleniumTest1.class);
        seleniumTest1.clickButtonPresentByID_id012();
        SeleniumTest2 seleniumTest2 = seleniumTest1.clickSeleniumTest2();
        seleniumTest2.inputTextBox("hello");
        seleniumTest1 = seleniumTest2.clickSeleniumTest1();
 
 
 
Download example source code Selenium Webdriver Page Factory
 
 
External References