Saturday, April 16, 2016

SELENIUM PAGE OBJECT MODEL


PAGE OBJECT MODEL, PAGE OBJECT DESIGN PATTERN WITH and WITHOUT PAGE-FACTORY

_______________________________________________________________________________


1. Each page within the AUT (Application Under Test) is considered as a Java class./ A major functionality within page could be considered as class or several functionality within a page could be considered as inner class.
2. Identify what you need to test on a particular page.
3. Identify all the locators on that page which are required to perform identified tests

I am taking an example of Wordpress as AUT

Here I will show you example of Page Object Design Pattern using PageFactory as well as without using PageFactory.




As you can see every pages is considered as a java class. All locators which are required to perform tests on that page are considered as instance variable or class(static) variable on that page. For example lets say we are working on a login page. To perform a login action on that page we need username, password and finally need to click on submit or login button, so clearly we have need three web elements here for which we need three locators.

// login page locators
By userName = By.id("user_login");
By password = By.id("user_pass");
By loginButton = By.id("wp-submit");








The main difference between PageFactory and non PageFactory design pattern is, in PageFactory design pattern the locators are used within @FindBy annotation with How enum and that returns an object of type WebElement where as in non PageFactory design pattern the locators are used as an argument to "By class" static methods.

which returns an Object of type By.

Non pagefactory

By userName = By.id("user_login");
By password = By.id("user_pass");
By loginButton = By.id("wp-submit");

PageFactory using @FindBy annotation

@CacheLookup
@FindBy(how = How.ID, using = "user_login")
WebElement userName;
@CacheLookup
@FindBy(how = How.ID, using = "user_pass")
WebElement password;
@CacheLookup
@FindBy(how = How.ID, using
WebElement loginButton;

In non-page factory design  we need an argument  constructor which which will pass driver instance from test class to page class.



In pageFactory design approach we use PageFactory.initElements, this initElements takes argument as 
driver The driver that will be used to look up the elements  and 
pageClassToProxy A class which will be initialised.

which return An instantiated instance of the class with WebElement and List<WebElement> fields proxied





1 comment:

  1. Selenium is the best tool ever to test an application effectively. Your blog made me to realize that through the best examples you have explained. Thanks for sharing a wonderful article.
    Regards:
    Selenium Training Chennai
    software testing selenium training

    ReplyDelete