Page Object Model and Fluent Design I
Software automation test is to use specific software tools or automation code and the power of machines to simulate the manual execution of tests, compare the outcomes, and report defects. Once automated tests have been developed, they can be run quickly and repeatedly. Many times, this can be a cost-effective approach for regression testing of software products that have a long maintenance life and needs prompt response to dynamic market requirement.
Automation test framework facilitates a standard way for modifying, adding, and deleting the test scripts and functions, and provides scalability and reliability with less effort.
Most Automation Test Framework is purely based on a series of open source frameworks like Selenium webdriver, TestNG.
Page Object Model
Page objects are most commonly used in testing web page UI. A page object wraps an HTML page, or fragment, allowing you to manipulate page elements without digging around in the HTML. It is a typical Object Oriented design methodology. A Page Object class should contains all the elements in a specific page or fragment, and the operation on these elements, no assertion. Page objects act as a library for further testing.
All Page Object classes should be under src/main/java folder, and all unit test based on page objects and functional, regression test should be under src/test/java folder.
Some other important tips:
. a page object is supposed to be used in various testing, thus it is important to add comment at the beginning of the class to illustrate the purpose of this page object . a page object class name should be meaningful and ends with Page, and it should always extends BasePage class.– will illustrate BasePage class later . all the elements definition using @FindBy annotation . need to define methods which interaction with each element, normally each element has a correspondent method . After execution a specific method, if it navigates to a new page, return that new page object; if it stays on current page, return current page object. It can also return a String or Boolean or other value depends on the operation . method name should always start with an operation verb, like enter, click, check, and following with the element name text
Following is a simple example:
Refer what Martin Fowler explained on Page Object Model. https://martinfowler.com/bliki/PageObject.html
And if you can spend time reading relevant selenium source code, you can benefit a lot.
https://github.com/SeleniumHQ/selenium/wiki/PageObjects
https://github.com/SeleniumHQ/selenium/wiki/PageFactory
BasePage class
BasePage class is an abstract class which encapsulates those common functions required for each page object. It does not represent a specific page, that’s why it is abstract, and is the parent of all page object classes.
In the constructor of BasePage class, firstly it getts the webdriver which will be used extensively in each page object; then, with the help of PageFactory , initialize all the elements that are defined in web page classes or Page Objects and wait until the page being fully loaded.
It is strongly suggested that each page object class extends from BasePage, and in the constructor method use super(driver); as the first sentence.