Appium: Page element initialization at the operation layer, and appium Initialization
Page element Initialization is encapsulated into a class. You only need to initialize the page element once for the first time, and the data is stored in ArrayList.
The Code is as follows:
Package com. datas. api; import com. appium. api. log; import org. openqa. selenium. by; import java. util. arrayList; import java. util. hashMap; import io. appium. java_client.android.AndroidDriver;/*** Created by kitwit on 0011. * initialize the page element */public class InitTestPage {public AndroidDriver driver; private String pageName; private LocaterPaser locaterPaser = new LocaterPaser (); private HashMap
LocatorMap; InitTestPage () {}/ *** initialize a page object, create a LocaterPaser, and HashMap * parameter: Page name * return none **/public InitTestPage (String pageName) {locatorMap = locaterPaser. getLocator (pageName); this. pageName = pageName;}/*** multiple constructors are provided for selection * parameter: Page name and drive object * return none **/public InitTestPage (AndroidDriver driver, String pageName) {locatorMap = locaterPaser. getLocator (pageName); this. driver = driver; this. pageName = pageName;}/*** get button information * parameter: itemName needs to read the information of the button, * return By type **/public By getBy (String buttonName) {ArrayList valueList = getXmlDatas (buttonName); Locator. byType idType = (Locator. byType) valueList. get (0); String value = (String) valueList. get (1); By = null; switch (idType) {case xpath: by =. xpath (value); break; case id: by =. id (value); break; case name: by =. name (value); break; case className: by =. className (value); break; default: by =. id (value);} return by;}/*** get the value information of the button * parameter: itemName needs to read the information of the button, * return By type **/public String getButtonTypeValue (String buttonName) {ArrayList valueList = getXmlDatas (buttonName); // Locator. byType idType = (Locator. byType) valueList. get (0); return (String) valueList. get (1);}/*** read data from the specified page * locatorMap is the Map that stores the page element data, and the value of String is key (the button name is used here ), the Locator object stores the value and type of the key * parameter: itemName needs to read the information of the button, * Find the returned ArrayList type, idType and value information are stored. **/private ArrayList getXmlDatas (String buttonName) {ArrayList valueList = new ArrayList (); Locator. byType idType; String value; Log.info ("start to search for page elements in HashMap... "); for (String key: locatorMap. keySet () {if (key. equals (buttonName) {idType = locatorMap. get (key ). getByType (); value = locatorMap. get (key ). getButtonValue (); valueList. add (idType); valueList. add (value); Log.info ("locate the required key in the database, and return the button type and value of the key... "); return valueList ;}}