Appium常用方法总结

1、左滑

/**

* ThisMethod for swipe Left

*@paramduring滑动速度等待多少毫秒

*/

protected voidswipeLeft(intduring) {

int width =driver.manage().window().getSize().width;

int height =driver.manage().window().getSize().height;

driver.swipe(width*3/4,height /2,width /4,height /2,during);

}

2、向上滑动

/**

* This Method for swipe up

* @param during滑动速度等待多少毫秒

*/

protected void swipeUp(int during) {

int width = driver.manage().window().getSize().width;

int height = driver.manage().window().getSize().height;

driver.swipe(width / 2, height * 3 / 4, width / 2, height / 4, during);}

3、向下滑动

/**

* This Method for swipe down

* @param during滑动速度等待多少毫秒*/

protected void swipeDown(int during) {

int width = driver.manage().window().getSize().width;

int height = driver.manage().window().getSize().height;

driver.swipe(width / 2, height / 4, width / 2, height * 3 / 4, during);

// wait for page loading

}

4、等待page加载元素

/**

*等待Page加载某个元素

*@paramwebElement页面元素

*@paramtimeOutInSeconds最大等待时间,秒值

*@returnWebElement

*/

protectedWebElementwait(WebElement webElement, inttimeOutInSeconds) {

newWebDriverWait(driver,timeOutInSeconds,100).until(newExpectedCondition() {

@Nullable

@Override

publicWebElementapply(@NullableWebDriver driver) {

if(isExist(webElement)) {

return webElement;

}else{

return null;}}});

return webElement;

}

5、判断page元素是否存在

/**

*判断Page元素是否存在

*@paramwebElementPage元素

*@returnboolean

*/

protected booleanisExist(WebElement webElement) {

if(null== webElement) {

return false;

}try{

webElement.isDisplayed();

return true;

}catch(NoSuchElementException e) {

return false;

}}

6、判断page元素是否存在(加入等待时间)

*判断Page元素是否存在

*@paramwebElementPage元素

*@paramtimeOutInSeconds超时时间

*@returnboolean

*/

protected booleanisExist(WebElement webElement, inttimeOutInSeconds) {

if(null== webElement) {

return false;}

try{

newWebDriverWait(driver,timeOutInSeconds,100).until(newExpectedCondition() {

@Nullable

@Override

publicWebElementapply(@NullableWebDriver driver) {

if(isExist(webElement)) {

returnwebElement;

}else{

return null;}}});

return true;

}catch(Exception e) {

return false;}}

7、手势解锁

final TouchAction gesture = new TouchAction(driver).press(startX, stratY)       

  .moveTo(startX, stratY + height)

.moveTo(startX, stratY + height + height)

.moveTo(startX + width, stratY + height + height).release();

gesture.perform();

8、移动到内容最后删除内容

private void ReplaceEdittext(WebElement editext,Strings){

editext.click();

//获取内容

String context=driver.findElementByAccessibilityId("A").getAttribute("A");

//光标移到末尾

driver.sendKeyEvent(123);

for (int i = 0; i < context.length(); i++) {

//删除

driver.sendKeyEvent(AndroidKeyCode.DEL);

}

editext.sendKeys(s);

//回车

//driver.sendKeyEvent(66);

}

TouchAction

AppiumDriver的辅助类,主要针对手势操作,比如滑动、长按、拖动等

9press(WebElement el)在控件上执行press操作。

10press(int x, int y)在坐标为(x,y)的点执行press操作

11press(WebElement el, int x, int y)在控件el的左上角的x坐标偏移x单位,y左边偏移y单位的坐标上执行press操作。

12moveTo(WebElement el)el为目标,从另一个点移动到该目标上。

13moveTo(int x, int y)以(x,y)点为目标,从另一个点移动到该目标上。

14、moveTo(WebElement el, intx, int y)

以控件el的左上角为基准,x轴向右移动x单位,y轴向下移动y单位。以该点为目标,从另一个点移动到该点上。

15、tap(WebElement el)

在控件的中心点上敲击一下

16、tap(int x, int y)

在(x,y)点轻击一下

17、tap(WebElement el, int x,int y)

以控件el的左上角为基准,x轴向右移动x单位,y轴向下移动y单位。在该点上轻击。

18、longPress(WebElement el)

控件长按

19、longPress(int x, int y)点长按

20longPress(WebElement el, int x, int y)

偏移点长按

21、cancel()取消执行该动作

22、perform()执行该动作

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 由于appium是扩展了Webdriver协议,所以可以使用webdriver提供的方法,比如在处理webview...
    涛b阅读 1,096评论 0 0
  • 1.contexts contexts(self): Returns the contexts within th...
    七月尾巴_葵花阅读 2,082评论 0 10
  • 我们在做Andorid 自动化测试时,用到最多的操作就是点击,输入,验证,滑动之类的,针对这些常用的操作我们可以进...
    长新阅读 1,043评论 0 0
  • 没隔多久,我又来到了美丽神奇的’白山灵石’。 与其说惦记白山景区的奇岩怪石、垂直异隙和风光逶迤的山脉,不如说约朋友...
    六月卧龙阅读 693评论 2 0
  • 这里的每一张图片,都是别人的故事,我借用他们的故事,所以没权利截去任何一寸的角落。 这些照片里,藏着人间最深的情和...
    崖柏今天早起了阅读 422评论 3 3