appium元素定位之AndroidUiAutomator

  UIAutomator 元素定位是 Android 系统原生支持的定位方式,虽然与 xpath 类似,但比它更好用,并且支持元素全部的属性定位,定位原理是通过 android 自带的android uiautomator 的类库去查找元素

  方法名:find_element_by_android_uiautomator('uiSelector 表达式')

  该方法的参数为 UiSelector 类定位元素的表达式:new UiSelector().函数名称("定位表达式")

  实例化一个 UiSelector 对象,然后通过实例调用对应的方法,每一个方法返回的都是UiSelector 对象本身

  注意:UiSelector 类下面的函数的参数如果是字符串,必须是双引号,这是 Java 的语法风格,在 Java 中,双引号才表示字符串

UiSelector 类的函数如下:

1.资源id方法

  • resourceId(String id)  资源 id
  • resourceIdMatches(String regex)  资源 id 正则

2.文本方法

  • text(String text)  文本匹配
  • textContains(String text)  文本包含
  • textStartsWith(String text)  文本开始字符
  • textMatches(String regex)  文本正则

3.描述方法

  • description(String desc)  描述
  • descriptionContains(String desc)  描述包含
  • descriptionStartsWith(String desc)  描述开始字符
  • descriptionMatches(String regex)  描述正则

4.类名方法

  • childSelector(UiSelector selector)  子类
  • fromParent(UiSelector selector)  父类
  • className(String className)  类名

5.索引、实例方法

  • index(int index)  编号
  • instance(int instance)  索引

6.特有属性方法

  • checked(boolean val)  选择属性
  • checkable(boolean val)  点击属性
  • enabled(boolean val)  enabled 属性
  • focusable(boolean val)  焦点属性
  • longClickable(boolean val)  长按属性
  •  scrollable(boolean val)  滚动属性
  • selected(boolean val)  选择属性

7.包名方法

  • packageName(String name)  包名
  • packageNameMatches(String regex)  包名正则

示例:

new UiSelector().text("text")  # 使用元素 text 属性定位
new UiSelector().resourceId("id")  # 使用 id 定位
new UiSelector().className("className")  # 使用元素类型定位

# 还可以元素多个属性自由组合
new UiSelector().className("className").text("text")
new UiSelector()..resourceId("id").clickable(val)

猜你喜欢

转载自www.cnblogs.com/xiaogongjin/p/11784109.html