查看ocx控件方法_Appium自动化测试入门教程No.8—— 定位控件

2d4bee85e37565de92f49e7ea17a730c.png

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

常用的Appium定位控件有如下:

一、name 定位

打开uiautomatorviewer.bat工具:

使用方法:

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

13140c72a0e367bf6e09249731d7a8bd.png

二、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"))

f12e4abc5570d96183e96e01092c096b.png

三、Class name 定位

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

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

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

33d1412f4d831c466fa524800f6eb696.png

四、XPath定位

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

64d0c370dab250aa39f968f48c899e22.png

五、Accessibility ID定位

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

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

使用方法: java driver.findElementByAccessibilityId("plus").click();

46c3434b1f2c8fab8681bd5f5a8f8b5b.png

六、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属性。

e971da9be7030d0aec1c24fea04ce6a5.png
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值