Раздел: Автоматизация > Практикум > Automation Test Framework (ATFwk)
Automation Test Framework (ATFwk) with Selenium Implementation
ATFwk is an automation test framework based on the Page Object design pattern. Describing it a special attention should be given to the following key entities:
- Working context configuration (ATFwkContext)
- Web browser interface (ATFwkWebBrowser) and its realization (Selenium2WebBrowser)
- Page object implementation (ATFwkPage)
- Page static resources
- GUI element's seeking strategy
Working Context Configuration (ATFwkContext)
Main ATFwk object is a working context - ATFwkContext. It is based on a singleton pattern. To initialize the working context it is required to refer browser object to the initInstance(...) method:
public synchronized static ATFwkContext initInstance(ATFwkWebBrowser browser)
It is also possible to reuse old or create a new (parallel) context by initializing it by referring the context object into initInstance(...) method:
public synchronized static ATFwkContext initInstance(ATFwkContext newContext)
Web Browser Interface (ATFwkWebBrowser) and Realization (Selenium2WebBrowser)
ATFwkWebBrowser is an interface which is the only point of contact between a test tool and automation test framework.
public interface ATFwkWebBrowser { void start(); void close(); void openURL(String s); public URL getCurrentURL(); public String getTitle(); public String getUrl(); public void maximize(); public void bringToFront(); public void bringToFront(int windowIndex); public void getScreenCapture(String s); public boolean exists(); public void goBack(); public String getRowText(); public void refresh(); public ATFwkWebBrowser getChildBrowser(); public ATFwkWebBrowser getChildBrowser(int i); public String getAlertText(); public void confirmAlert(); public void closeAlert(); public boolean saveToFile(String location, String path); public void waitForPageToLoad(); public void waitForAjax(); public void waitForElementBy(ATFwkFinder finder, String locator); public void waitForAlert(); public void waitFor(long seconds); public void setWaitingTimeout(long timeout); public void quit(); public ATFwkAction call(); public ATFwkVerifier verify(); }
In current ATFwk the only Selenium 2 WebDriver implementation of the ATFwkWebBrowser (Selenium2WebBrowser) is implemented in package org.protesting.atfwk.selenium2:
public class Selenium2WebBrowser implements ATFwkWebBrowser { ... ... //see the implementation in the SVN source ... }
In case if you need to use another tool for test automation you will have to implement ATFwkWebBrowser interface and refer it to the working context.
Page Object Implementation (ATFwkPage)
Page object pattern represents a model which implements the mapping of GUI elements and functionality to interact with page. In order to use ATFwk page object implementation class ATFwkPage has to be inherited by all page classes of the application under test. ATFwkPage provides a number of service methods to work with pages, content and resources.
Page Static Resources
Page static resources are all types of resources which are taking part in the description of pages (titles, descriptions and etc.), GUI elements (links, buttons, lists, tables and etc.) and texts on the page and also for searching of elements by different attributes (id, name, value, text, xpath and etc.). When using ATFwk framework all static resources have to be stored in properties files in the resources folder of the project.
The location of resource properties files is defined in the working context. The name of the properties file have to be the same as name of the page object for which this properties file is defined.
Example: Page Object TestAccessPage (package org.testapp.pages.mobapp). Static resources for this object are located in the following property file: resources/org/testapp/pages/mobapp/TestAccessPage.properties
To get property from the properties file you need to call the method getProperty(String key) which is implemented in the parent PageObject class ATFwkObjectWithProperties of the ATFwk.
GUI Element Seeking Strategy
In order to start working with element (click on it, read data, write data and so on) it should be found on the page. Automation test tool Selenium has such functionality. It uses WebDriver methods findElements(By by) and findElement(By by). For isolation purposes ATFwk implements wrappers for this methods and for By object. The enumeration public enum ATFwkFinder is introduced for defining the search parameter.
ATFwkWebBrowser class introduces method public ATFwkAction call(). Interface ATFwkAction consists of a number of abstract methods for working with GUI elements on the page. These methods are implemented in the inner class Action of the Selenium2WebBrowser class: class Action implements ATFwkAction {...}.
Example: for clicking the button user should call the method clickBy(...) passing parameters ATFwkFinder finder and String locator: getBrowser().call().clickBy(ATFwkFinder.CSS, getProperty("link.help.image.css"));
In current example the element with CSS locator will be found and then click event will be performed on this element:
public boolean clickBy(ATFwkFinder finder, String locator) { LOG.info("Click the {"+locator+"}"); WebElement element = getElementBy(finder, locator); boolean isOk = click(element); if(isOk) { LOG.debug("Click is OK"); return true; } LOG.debug("Element {"+locator+"} is not found"); return false; } private boolean click(WebElement element) { if (element!=null) { highlight(element); new Actions(webDriver).moveToElement(element).click().build().perform(); // set focus and click return true; } return false; }
The implementation of the getElementBy(...) is the following:
WebElement getElementBy(ATFwkFinder finder, String value) { List<WebElement> webElements = getElementsListBy(finder, value); if(webElements.size()==0) { return null; } return getElementsListBy(finder, value).get(0); }
The method getElementsListBy(...) has the following implementation:
private List<WebElement> getElementsListBy(ATFwkFinder finder, String value) { List<WebElement> elementList = webDriver.findElements(getBy(finder, value)); if(elementList != null && elementList.size() != 0) { return elementList; } return new ArrayList<WebElement>(); }
The method getBy(...) returns the particular By object corresponding to the ATFwkFinder object passed to the clickBy(...) method:
private By getBy(ATFwkFinder finder, String value) { switch(finder) { case ID: { return By.id(value); } case NAME: { return By.name(value); } case CLASS: { return By.className(value); } case TAG: { return By.tagName(value); } case CSS: { return By.cssSelector(value); } case XPATH: { return By.xpath(value); } case LINK_TEXT: { return By.linkText(value); } case PARTIAL_LINK_TEXT: { return By.partialLinkText(value); } default: { return By.id(value); } } }