Appium移动UI自动化随笔-知识点(Java)

一、元素定位

(初级)

1. 通过CONTENT-DESC定位元素

driver.findElementByAccessibilityId("")

2. 通过UIAutomator对象定位元素

driver.findElementByAndroidUIAutomator("");

3. 通过class属性定位元素

driver.findElementByClassName("");

4. 通过css定位元素

driver.findElementByCssSelector("");

5. 通过resource-id定位元素

driver.findElementById("");

6. 通过文本信息定位元素

driver.findElementByLinkText("");

7. 通过text属性定位元素

driver.findElementByName("");

8. 通过部分文本信息定位元素

driver.findElementByPartialLinkText("");

9. 通过HTML里的标签名定位元素

driver.findElementByTagName("");

10. 通过用X-PATH来定位元素

driver.findElementByXPath("//view[contains(@text,"value")]");

(进阶)

1. 没有name,没有ID的元素的定位,通用篇

因为没有name,id;其实剩下的选择已不多,要么xpath,要么className。这里采用了className。

List<WebElement> lis = driver.findElementsByClassName("android.widget.ImageView");//获取ImageView的所有元素
WebEelement targetEle = lis.get(0);//获取列表中第一个元素

可以将上面的操作封装成一个方法,使用的时候直接调用getAllImges(),缺点系统需要去获取List,所以运行效率不高。

public List<WebElement> getAllImges(){
List<WebElement> lis = driver.findElementsByClassName(AndroidClassName.IMAGEVIEW);
return lis;
}

2. 没有name,没有ID的元素的定位,特别篇

场景1:同一个页面有10个ImageView对象,而我们的目标元素的index为4,而同为ImageView且index为4的页面只有2个。那我们就可以根据这2个条件来组合查询条件,提高效率。

List<WebElement> lis =

driver.findElementsByAndroidUIAutomator("new UiSelector().className("+"android.widget.ImageView"+").index(4)");

//封装方法
public List<WebElement> getElementsByClassAndIndex(String classname,int index){
List<WebElement> lis =null;
lis = driver.findElementsByAndroidUIAutomator("new UiSelector().className("+classname+").index("+index+")");
return lis;
}

场景2:同一个页面有10个ImageView对象,而其中index为4的有5个,而这时我们发现我们的目标元素的是clickable的。然后review页面发现,同时满足上述条件的只有2个。

List<WebElement> lis =

driver.findElementsByAndroidUIAutomator("new UiSelector().className("+"android.widget.ImageView"+").index(4).clickable(true)");

//封装
public List<WebElement> getElementsByClassAndIndexAndClickable(String classname,int index){
List<WebElement> lis =null;
lis = driver.findElementsByAndroidUIAutomator("new UiSelector().className("+classname+").index("+index+").clickable(true)");
return lis;
}

场景3:在分析页面元素的时候发现,页面相对比较简单,而且其中只有目标元素的index为4.

driver.findElementByAndroidUIAutomator("new UiSelector().index("+index+")");

//封装方法
public WebElement getElementByIndex(int index){
return driver.findElementByAndroidUIAutomator("new UiSelector().index("+index+")");
}

二、元素操作(常用)

点击↓

.click()

清空

.clear()

输入

.sendKeys(“”)

获取

.getText()

三、手势操作

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

press(WebElement el)↓

在控件上执行press操作。

press(int x, int y)

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

press(WebElement el, int x, int y)

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

release()

释放操作,代表该系列动作的一个结束标志。

moveTo(WebElement el)

以el为目标,从另一个点移动到该目标上

moveTo(int x, int y)

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

moveTo(WebElement el, int x, int y)

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

tap(WebElement el)

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

tap(int x, int y)

在(x,y)点轻击一下

tap(WebElement el, int x, int y)

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

waitAction()

代表一个空操作,等待一段时间

waitAction(int ms)

等待ms秒

longPress(WebElement el)

控件长按

longPress(int x, int y)

点长按

longPress(WebElement el, int x, int y)

偏移点长按

cancel()

取消执行该动作

perform()

执行该动作

//模拟向右滑动
protected AppiumDriver<MobileElement> driver;
private void swipeRight() {
int startx = getWindowWidth() / 5;
int offsetx = getWindowWidth() / 2;
int starty = getWindowHeight() / 2;
new TouchAction(driver)
    .press(startx, starty)
    .moveTo(-offsetx, 0)
    .waitAction(Duration.ofSeconds(1))
    .release()
    .perform();
}

四、断言

详见:链接:https://pan.baidu.com/s/1hsgrHDa 密码:wo5o

断言

public static void assertTrue(java.lang.String message, boolean condition) { /* compiled code */ }

public static void assertTrue(boolean condition) { /* compiled code */ }

public static void assertFalse(java.lang.String message, boolean condition) { /* compiled code */ }

public static void assertFalse(boolean condition) { /* compiled code */ }

public static void fail(java.lang.String message) { /* compiled code */ }

public static void fail() { /* compiled code */ }
原文地址:https://www.cnblogs.com/MR-FANWB/p/7910404.html