appium新手入门(8)—— 定位控件

虫师 创建于 over 6 years 之前

最后更新: about 1 month 之前

阅读数: 1

appium新手入门(8)—— 定位控件

appium 通过 uiautomatorviewer.bat 工具来查看控件的属性。该工具位于 Android SDK/tools/bin/ 目录下。

id 定位


通过uiautomatorviewer.bat 工具可以查看对象的id属性。

如果目标设备的API Level低于18则UIAutomatorViewer不能获得对应的Resource ID,只有等于大于18的时候才能使用。

打开uiautomatorviewer.bat工具:

resource-id 就是我们理解的id属性了。

使用方法:

driver.findElement(By.id("com.android.calculator2:id/formula"))

name 定位


打开uiautomatorviewer.bat工具:

text就是我们要查找的name了,为什么在命名上毫无关联啊!

使用方法:

driver.findElement(By.name("9"))

class name 定位


计算器界面上的的class属性是:android.widget.Button。 使用方法:

WebElement button = driver.findElement(By.className("android.widget.Button"));

使用 Class Name 一般获得的 view 都不止一个,所以应该需要遍历一遍得到的 views,然后缩小搜索条件来获得目标控件。

XPath定位


在 WebDriver 上 XPath 定位是功能强大的一种定位方式。我个人惯用于此方法来定位Web页面上的元素。下面看看在 Android 上 XPath 定位的用法。

用class的属性来替代做标签的名字。

使用方法:

driver.findElement(By.xpath("//android.view.ViewGroup/android.widget.Button"))  //7

当果如果出现class 相同的情况下可以用控件的属性值进行区分。

driver.findElement(By.xpath("//android.widget.Button[contains(@text,'7')]")).click(); //7
driver.findElement(By.xpath("//android.widget.Button[contains(@content-desc,'times')]")).click(); //*
driver.findElement(By.xpath("//android.widget.Button[contains(@text,'7')]")).click();  //7
driver.findElement(By.xpath("//android.widget.Button[contains(@content-desc,'equals')]")).click(); //=

XPath 在 Appium 上的用法依然很强大,有时需要写更臭更长的定位语法,因为APP上元素的class命令本来就长,再加上多层级,结果可想而知。

Accessibility ID定位


这个方法属于Appium扩展的定位方法。

其实,我们的核心是要找到元素的contentDescription属性。它就是元素的 content-desc 。

使用方法:

driver.findElementByAccessibilityId("plus").click();

android uiautomator定位


这个方法也属于 Appium(Android)扩展的定位方法。同样使用 UIAutomatorViewer.bat 工具直接查看。

也就是说一个元素的任意属性都可以通过android uiautomator方法来进行定位,但要保证这种定位方式的唯一性。

使用方法:

driver.findElementByAndroidUIAutomator("new UiSelector().text(\"clr\")").click();
driver.findElementByAndroidUIAutomator("new UiSelector().text(\"8\")").click();
driver.findElementByAndroidUIAutomator("new UiSelector().description(\"plus\")").click();
driver.findElementByAndroidUIAutomator("new UiSelector().text(\"5\")").click();
driver.findElementByAndroidUIAutomator("new UiSelector().description(\"equals\")").click();

需要注意的是 description() 方法用的是content-desc属性。

0

相关课程

Android测试基础教程
图文
Android测试基础教程

课程分类: 移动端测试

安卓测试入门

  • 已完结
  • 已更新8集
  • 最后更新时间: 2024-03-18 09:52:28

免费

查看详情