My Blog List

Sunday, October 30, 2011

PageObjects and PageFactory - Selenium 2.0

Hi all,
Today I am going to speak about an important and well equipped feature of Selenium2.0 PageObjects and PageFactory.
1. Max re-usabality
2. Easy to modify the elements locator strings as it will have only one place for each element
3. Well understandable framework
4. Neatly designed and easy to code.
Page Objects:
Elements on a page or parts of a page called page objects. We can design a class which consists constructors, WebElements including locators, Getters, Setters of some web elements of a page. ie. Services done by part of a page (application).
By using this class, we will instantiate using Page Factories and use the instance to do the services.
So for every element, our full test script contains the locators in one place only.
Likewise, we have design classes for every operations done by particular page and use the instances to do the operations.

You can build your Page Object of the Common Web Elements (just invented this name :)) - each CWE will represent a "widget" that is used on different pages. In your example this will be a some sort of Date Widget - it contains the Year, Month and a Day. Basically it will be a Page Object

PageFactory requires the string constants to be used in @FindBy annotations.
To resolve this limitation we created our own ElementLocators..

You can use the DateWidget in your test:

DateWidget widget = new DateWidget(driver, "yearId", "monthId", "dayId");

public void testYearNumeric() {
        //Logic to determine Error message shows up

        // ... and day
        //Logic to determine Error message shows up

The DateWidget class, which contains custom locators and annotation parsers is:

package pagefactory.test;
import java.lang.reflect.Field;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class DateWidget {

    // These constants are used to identify that they should be changed to the actual IDs
    private static final String YEAR_ID = "$YEAR_ID$";
    private static final String MONTH_ID = "$MONTH_ID$";
    private static final String DAY_ID = "$DAY_ID$";

    // Elements whose ids will be replaced during run-time
    /** Year element */
    @FindBy(id = YEAR_ID)
    private WebElement year;

    /** Month element */
    @FindBy(id = MONTH_ID)
    private WebElement month;

    /** day element */
    @FindBy(id = DAY_ID)
    private WebElement day;

    // The ids of the elements
    /** ID of the year element */
    private String yearId;

    /** ID of the month element */
    private String monthId;

    /** ID of the day element */
    private String dayId;

    public DateWidget(WebDriver driver, String yearId, String monthId,
            String dayId) {
        this.yearId = yearId;
        this.monthId = monthId;
        this.dayId = dayId;

        PageFactory.initElements(new CustomLocatorFactory(driver, 15), this);

    public String getYear() {
        return year.getValue();

    public void setYear(String year) {
        setValue(this.year, year);

    public String getMonth() {
        return month.getValue();

    public void setMonth(String month) {
        setValue(this.month, month);

    public String getDay() {
        return day.getValue();

    public void setDay(String day) {
        setValue(, day);

    public void submit() {

    private void setValue(WebElement field, String value) {

    private class CustomLocatorFactory implements ElementLocatorFactory {
        private final int timeOutInSeconds;
        private WebDriver driver;

        public CustomLocatorFactory(WebDriver driver, int timeOutInSeconds) {
            this.driver = driver;
            this.timeOutInSeconds = timeOutInSeconds;

        public ElementLocator createLocator(Field field) {
            return new CustomElementLocator(driver, field, timeOutInSeconds);

    private class CustomElementLocator implements ElementLocator {
        private WebDriver driver;
        private int timeOutInSeconds;
        private final By by;

        public CustomElementLocator(WebDriver driver, Field field,
                int timeOutInSeconds) {
            this.driver = driver;
            this.timeOutInSeconds = timeOutInSeconds;
            CustomAnnotations annotations = new CustomAnnotations(field);
   = annotations.buildBy();

        public WebElement findElement() {
            ExpectedCondition<Boolean> e = new ExpectedCondition<Boolean>() {
                public Boolean apply(WebDriver d) {
                    return Boolean.TRUE;
            Wait<WebDriver> w = new WebDriverWait(driver, timeOutInSeconds);

            return driver.findElement(by);

    private class CustomAnnotations extends Annotations {

        public CustomAnnotations(Field field) {

        protected By buildByFromShortFindBy(FindBy findBy) {

            if (!"".equals( {
                String id =;
                if (id.contains(YEAR_ID)) {
                    id = id.replace(YEAR_ID, yearId);
                } else if (id.contains(MONTH_ID)) {
                    id = id.replace(MONTH_ID, monthId);
                } else if (id.contains(DAY_ID)) {
                    id = id.replace(DAY_ID, dayId);

            return super.buildByFromShortFindBy(findBy);



1 comment:

  1. Thanks for sharing great information in your blog. Got to learn new things from your Blog . It was very nice blog to learn about Selenium