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

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 相同的情况下可以用控件的属性值进行区分。 java 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 。

使用方法: java 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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值