触屏操作自动化
TouchAction
- 常用方法
- press
- release
- moveTo
- tap
- wait
- longPress
- cancel
- perform
MultiTouch
- 多点触控
定位技巧
xpath定位进阶
- 层级定位
- 父节点定位子节点
- 子节点定位父节点
- 子节点定位兄弟节点
- 爷爷节点定位孙子节点
uiautomator定位
- 调用方法
- driver.find_element_by_android_uiautomator()
- 优点
- uiautomator是android自带的工作引擎,速度快
- 缺点
- 表达式书写复杂,没有IDE提示,容易写错,并且无法兼容iOS
- 参考资料
- developer.android.com
- 书写方式
-
通过resourceid定位
- new UiSelector().resourceId("id")
-
通过classname定位
- new UiSelector().className("className")
-
通过content-desc定位
- new UiSelector().description("content-desc")
-
通过文本定位
- new UiSelector().text("text")
- new UiSelector().textContains("text")
- new UiSelector().textStartsWith("text")
- new UiSelector().textMatches("RE表达式")
-
组合定位
- new UiSelector().resourceId("id").text("text")
- 其他的都可以组合,可以一直组合,类似链式调用
-
通过关系定位
- 父子关系定位
- new UiSelector().resourceId("id").childSelector(text("mytext"))
- 兄弟关系定位
- new UiSelector().resourceId("id").fromParent(text("mytext"))
- 父子关系定位
-
滚动查找定位
- new UiScrollable(new Uiselector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text("查找文本").instance(0))
-
显示等待
- 在客户端等待
- 使用两个类
- WebDriverWait
- timeout
- poll_frequency: 间隔时长,默认0.5
- igonred_exceptions:抛出的异常,默认NoSuchElement
- expected_conditions
- WebDriverWait
- 显示等待可以等待异步(ajax)加载的元素
属性与断言
属性
- 通过get_attribute()方法获取属性
断言
- 普通断言 assert
- Hamcrest断言
- 是一个断言框架
- 支持多种语言
用例参数化
- pytest.mark.parametrize
capability进阶
- newCommandTimeout: 命令执行的间隔时间,默认60s
- udid:设备唯一标识,多设备时使用
- autoGrantPermissons: app自动授权,默认值为false
toast控件识别
- toast显示时间有限,Toast会根据用户设置的显示时间后自动消失
- Toast本身是一个系统级别的控件,它归属与系统settings,这类控件不在app内,由系统同一进行弹框。
- 定位方法
- 触发toast
- 打印pageSource
- 通过xpath查找
- //*[@class='android.widget.Toast']