Appium: Common Operation encapsulation at the operation layer, and appium Encapsulation
This class encapsulates common operation methods based on your own needs or project needs, with no special requirements or difficulty. There is also an appium api layer that encapsulates various methods provided by the appium framework to increase stability and handle exceptions. This is followed by code.
The operation methods on all subsequent pages directly add this class, and each page will only create a new Action class, that is, initialize a page element, Singleton mode. The Code is as follows:
Package com. datas. api; import com. appium. api. elementMethodObject; import com. appium. api. log; import com. appium. api. swipeMethodObject; import org. openqa. selenium. by; import org. openqa. selenium. webElement; import java. text. messageFormat; import java. util. list; import io. appium. java_client.TouchAction; import io. appium. java_client.android.AndroidDriver; import io. appium. java_client.android.AndroidKeyCode; import static com. appium. api. threadSleepMethod. threadSleep;/*** Created by kingwit on April /9/11 0011. * provides operations on the category class, click set value, and complete element initialization * inherits the InitTestPage class */public class ActionTest extends InitTestPage {public static AndroidDriver driver; private String pageName; private ElementMethodObject emb = new ElementMethodObject (); private SwipeMethodObject sm = new SwipeMethodObject (driver); public ActionTest () {} public ActionTest (AndroidDriver driver) {this. driver = driver;} public ActionTest (AndroidDriver driver, String pageName) {super (driver, pageName); this. driver = driver; this. pageName = pageName;}/*** enter a character for an element, which is used to reply to the comment class * parameter: buttonName, value */public void setCommentValue (String buttonName, String value) {WebElement element = emb. waitForElementPresent (driver, getBy (buttonName), 10); element. sendKeys (value); threadSleep (2000);}/*** enter a character for an element. This is not suitable for replying to the comment class * parameter: buttonName, value */public void setValue (String buttonName, String value) {WebElement element = emb. waitForElementPresent (driver, getBy (buttonName), 10); int size = element. getText (). length (); driver. pressKeyCode (123); for (int I = 0; I <size; I ++) {Log.info ("the search box contains content to delete the original data... "); driver. pressKeyCode (AndroidKeyCode. BACKSPACE);} // if (isElementExist ("earse_search") {// Log.info ("edit the content and delete the original data... "); // click (" earse_search "); //} element. sendKeys (value); threadSleep (2000);}/*** get the text content of the id * parameter: buttonName */public String getValue (String buttonName) {WebElement element = emb. waitForElementPresent (driver, getBy (buttonName), 10); return element. getText ();}/*** get the text content of the id * parameter: by object */public String getValue (By by) {return emb. waitForElementPresent (driver, by, 10 ). getText ();}/*** click element * parameter: buttonName */public void click (String buttonName) {emb. waitForElementPresent (driver, getBy (buttonName), 10 ). click ();}/*** click element directly * parameter: element */public void click (WebElement element) {element. click () ;}// the custom click method is used to flexibly click the unfixed xpath public void click (By byPathName) {emb. waitForElementPresent (driver, byPathName, 10 ). click (); // TouchAction tap = new TouchAction (driver); // tap. tap (emb. waitForElementPresent (driver, byPathName )). release (). perform ();}/*** click screen (based on element) * parameter: buttonName */public void tap (String buttonName) {TouchAction tap = new TouchAction (driver); tap. tap (emb. waitForElementPresent (driver, getBy (buttonName), 10 )). release (). perform ();}/*** click the screen (based on coordinates) * parameter: buttonName */public void tapXY (int x, int y) {TouchAction ta = new TouchAction (driver); ta. tap (x, y ). perform ();}/*** get screen size * parameter: driver object * return screen width and height **/public int [] getAppScreenWH () {int [] appWidthAndHeight; appWidthAndHeight = sm. getAppScreen (driver); return appWidthAndHeight;}/*** get element List * parameter: buttonName */public List
GetElementList (String buttonName) {return (List
) Driver. findElements (getBy (buttonName);}/*** get Element Object * parameter: buttonName */public WebElement getElement (String buttonName) {return emb. waitForElementPresent (driver, getBy (buttonName), 1);}/*** sliding screen. The sliding speed is fixed to 2.5 s, and the sliding frequency is 1 time. * parameter: ction slide direction */public void swipeScreen (String direction) {switch (direction) {case "up": Log.info ("start to slide up... "); sm. slideUP (driver, 1); break; case "down": Log.info ("start to slide down... "); sm. slideDown (driver, 1); break; case "right": Log.info ("start sliding to the right... "); sm. slideRight (driver, 1); break; case "left": Log.info ("start sliding left... "); sm. slideLeft (driver, 1); break; default: break;}/*** slide an element, Slide left and right * parameter: buttonName */public void swipElement (String buttonName, string direction) {WebElement element = emb. waitForElementPresent (driver, getBy (buttonName), 10); sm. slidingElement (element, direction,);}/*** determines whether an element exists * parameter: buttonName */public boolean isElementExist (String buttonName) {boolean exist = false; if (emb. isElementPresent (driver, getBy (buttonName) {exist = true; Log.info ("the element to be searched exists, return True... ");} return exist; // If yes, true is returned.}/*** press the * parameter on the element: buttonName */public void loogPress (String buttonName) {try {WebElement element = emb. waitForElementPresent (driver, getBy (buttonName), 10); TouchAction ta = new TouchAction (driver); ta. longPress (element ). release (). perform ();} catch (Exception e) {e. printStackTrace () ;}}/*** get the current activity Declaration as a static function. For other classes, you can call * parameter: none */public String getCurrentActivity () {return driver. currentActivity ();}/*** returns the previous page physically quietly * parameter: none */public void goBack () {driver. pressKeyCode (AndroidKeyCode. BACK);} // used to locate the xpath path using dynamic parameters. Use placeholders to obtain the parameters. // return the actual xpath path String public String stringFormate (String xpath, int index) {return MessageFormat. format (xpath, index);} // used to locate the xpath path using dynamic parameters. The placeholder is used to obtain the parameter. // return the actual operation. The xpath path String is public String stringFormate (String xpath, String str) {return MessageFormat. format (xpath, str);} // used to locate the xpath path using dynamic parameters. The placeholder is used to obtain the parameter. // return the actual operation. The xpath path String is public String stringFormate (String xpath, int index, string string) {return MessageFormat. format (xpath, index, string );}}