Common methods

At this point, the abstract base class has been built with the page factory initialization, abstract methods, and common elements, and, finally, we need to add some common methods. What methods should go in the base class?

Basically, any method that would apply to each of the subclass page objects goes into this class. Examples would be: navigation bar methods; page methods to retrieve titles, copyrights, logos, and headings; methods for logging out of the application; methods to synchronize against spinner controls that appear on each page; methods that handle alert and error message windows; custom methods for drop-down list selections; methods for label and text verification; and so on.

Any method that can be made "generic" enough (by its locator) to operate on any page in the web or mobile app would go in the base class. Now, if some of the methods only apply to specific pages, or would require different behavior on different pages, then an interface can be created and added to the subclass signature to implement those methods:

// base class common methods

/**
* getTitle - method to return the title of the current page
*
* @throws Exception
*/
public String getTitle() throws Exception {
WebDriver driver = CreateDriver.getInstance().getDriver();

return
driver.getTitle();
}

/**
* getParagraph - method to return the paragraph using a pattern match
*
* @param pattern
* @return String
* @throws Exception
*/
public String getParagraph(String pattern) throws Exception {
WebDriver driver = CreateDriver.getInstance().getDriver();

// build a dynamic locator on the fly with text pattern in
//paragraph
String locator = "//p[contains(text(),'" + pattern + "') or
contains(.,'"
+ pattern + "')]";

return driver.findElement(By.xpath(locator)).getText();
}

/**
* getCopyright - method to return the page copyright text
*
* @return String
* @throws Exception
*/
public String getCopyright() throws Exception {
return copyright.getText();
}

// common base class overloaded loadPage methods

/**
* loadPage - method to load the page URL for the AUT
*
* @param pageURL
* @param timeout
* @throws Exception
*/
public void loadPage(String pageURL,
int timeout)
throws Exception {

WebDriver driver = CreateDriver.getInstance().getDriver();
driver.navigate().to(pageURL);

// wait for page download, sync. against login
BrowserUtils.isPageReady(driver);
BrowserUtils.waitFor(login, timeout);
}

/**
* loadPage - overloaded method to load the page URL and sync
* against WebElement
*
* @param pageURL
* @param element
* @throws Exception
*/
public void loadPage(String pageURL,
M element)
throws Exception {

WebDriver driver = CreateDriver.getInstance().getDriver();
driver.navigate().to(pageURL);

// wait for page download, sync. against element
BrowserUtils.isPageReady(driver);
BrowserUtils.waitFor(element, Global_VARS.TIMEOUT_MINUTE);
}

/**
* loadPage - overloaded method to load the page URL and sync
* against endpoint URL
*
* @param pageURL
* @param landingUrl
* @throws Exception
*/
public void loadPage(String pageURL,
String endPointUrl)
throws Exception {

WebDriver driver = CreateDriver.getInstance().getDriver();
driver.navigate().to(pageURL);

// wait for page download, sync. against endpoint URL
BrowserUtils.isPageReady(driver);
BrowserUtils.waitForURL(endPointUrl, Global_VARS.TIMEOUT_MINUTE);
}