App自动化测试(四)之元素定位

App自动化测试之元素定位

通过uiautomatorviewer辅助元素定位工具,我们可以看到微信注册的注册按钮元素对应的信息:

text:元素的文本信息

resource-id:元素的ID

class:元素的className

package:元素所在的包名

content-desc:

bounds:元素的坐标

1、通过id进行定位(app中的id不一定是唯一的)

driver.find_element_by_id("com.tencent.mm:id/e4c")

2、通过className进行定位

driver.find_element_by_class_name("android.widget.Button")

3、通过xpath进行定位(在App当中text是用@text表示,不是text())

driver.find_element_by_xpath('//android.widget.Button[@resource-id=\"com.tencent.mm:id/e4c\"]')

4、通过content-desc进行定位(有些元素中可能content-desc为空)

driver.find_element_by_accessibility_id('')

5、通过uiautomator进行定位(这是安卓的原生定位方式,快)

driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.tencent.mm:id/e4c")')

附:UiSelector API 详细介绍

1、资源ID定位对象

返回值 API 说明
UiSelector resourceId(String id) 资源ID
UiSelector resourceIdMatches(String regex) 资源ID正则

2、文本属性定位对象

返回值 API 说明
UiSelector text(String text) 文本
UiSelector textContains(String text) 文本包含
UiSelector textMatches(String regex) 文本正则
UiSelector textStartsWith(String text) 文本起始匹配

3、描述属性定位对象

返回值 API 说明
UiSelector description(String desc) 描述
UiSelector descriptionContains(String desc) 描述包含
UiSelector descriptionMatches(String regex) 描述正则
UiSelector descriptionStartsWith(String desc) 描述开始字符匹配

4、索引与实例属性定位对象

索引与实例说明

1)索引index:指在同级中的编号,在兄弟类中的主键的编号
2)实例instance:整个布局文件中的编号,同一个类(比如6.2中的view类)的同级编号

返回值 API 说明
UiSelector index(int index) 索引
UiSelector instance(int instance) 实例

5、特殊属性定位对象

返回值 API 说明
UiSelector checked(booleean val) 选择属性
UiSelector clickable(boolean val) 可点击属性
UiSelector enabled(boolean val) enabled属性
UiSelector focusable(boolean val) 焦点属性
UiSelector focused(boolean val) 当前焦点属性
UiSelector longClickable(boolean val) 长按属性
UiSelector scrollable(boolean val) 滚动属性
UiSelector selected(boolean val) 背景选择属性

6、节点属性定位对象

返回值 API 说明
UiSelector childSelector(UiSelector selector) 从当前类中往下递归找符合条件的子类组件
UiSelector formPrent(UiSelector selector) 从父类往下递归找符合条件的组件

猜你喜欢

转载自www.cnblogs.com/desireyang/p/12394270.html