Skip to main content

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);




Popular posts from this blog

Change IE Browser ZOOM settings

Lot of UI automation testers could have faced this problem as we could change the zoom settings while operating manually for our convenience and forgot to reset to 100%.
But our QTP and some other related tools would operate the browser perfectly if browser zoom is 100%. So wee need to change the zoom before start to run the scripts. Its better to have a code snippet in our framework to change this zoom setting right?
Here we go...

1. We can simply change the Registry values before Invoking IE
Function ChangeRegistry Dim objShell Set objShell = CreateObject("WScript.Shell")  objShell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Zoom\ZoomFactor", "100000", "REG_DWORD" Set objShell = Nothing EndFunction This option is very useful. But in real time, lot of customers could have restricted write access to windows registry. So we can try other options.

2. Use IE COM object and Change Settings
Function ChangeIEZoom Dim intZoomLevel, objIE  intZoo…

QTP - Object Identification - Prerequesties

While starting a new project or proof of concept or after new QTP installation, we would face the issue in identifying objects as QTP built objects like WinList, WebEdit, etc.; instead everything will be identified as WinObject.

Here are some steps to ensure you did the environment setup RIGHT before start automating.
Did you enabled required Add-ins only while launchingDid you selected "Record >> Record and Run Settings" - This should be based on what type of application you are working with
Windows app - Windows >> Record and run test on any windows applications. Web app - Web >> Record and run test on any open browser.
Did you tried to launch UFT/QTP and then AUT.If its web, ensure browser zoom level set to 100%Try disabling the protected mode in IE for web.Launch UFT and AUT using RUN AS ADMINISTRATORAlso see the Object Identification Methods if you are interested.

Run JavaScript from QTP

Yeah, You can run your pure JavaScript from QTP using RunScript method. Lot of times, we are in need of firing events or simulating actions on web page which is not supported by QTP. At that time, you can use your direct DOM methods and directly execute your script on the web page from QTP like,
Dim MyPage, SearchBox Set MyPage = Browser("title:=Google").Page("title:=Google") Set SearchBox = MyPage.RunScript("document.getElementsByName('q')(0);") SearchBox.Value="testing"
'if objects available in frames, Set SearchBox = MyPage.RunScript("document.frames(4).document.getElementsByName('q')(0);")
Set objFrame = Browser("title:=Google").Page("title:=Google").Frame("title:=something")
objFrame.RunScript("document.getElementsByName('q')(0);") Also, you can execute by obtaining actual browser window object if you have IE app like, Dim IEApp Set IEApp = CreateObject("Internet…