Android之UiAutomator测试框架源码分析(第十五篇:查找控件功能的集中介绍)

本文详细介绍UIAutomator测试框架中查找控件的各种方法,包括UiDevice、UiObject2、UiObject、UiCollection及UiScrollable类提供的API,并强调了官方推荐使用的最佳实践。

(注意:本文基于UI Automator测试框架版本为2.2.0)   

    UI Automator测试框架、提供多种查找控件的方法,使用控件之前、必须先查找到控件,可见查找控件的方法有多重要。查找控件功能也是UI Automator测试框架使用最多的API,官方给的API非常灵活,满足你各种查找控件的需求,比如查找1个,比如查找多个,再来个一句话概括:View树包含的控件,通过这些API一定可以找到!这些API集中在UiDevice、UiObject2、UiObject、UiCollection、UiScrollable类中,接下来我们一起了解一下!

 

UiDevice中查找控件的方法

位于UiDevice类中的四个方法,findObject(BySelector)与findObject(UiSelector)用于查找一个控件,findObjects(BySelector)可用于查找出多个控件,hasObject(BySelector)则负责查找指定的控件是否存在

1、查找控件的起始位置

UiDevice中的查找控件的方法均是以View树的根节点作为起点,开始查找对应的控件

2、三个Searchable接口规范的方法

Searchable接口位于androidx.test.uiautomator包中,UiDevice类与UiObject2类都实现了Searchable接口。findObject(BySelector)、findObjects(BySelector)、hasO

协议滚动到最底了,agree也变为可点击状态,还在尝试滚动 C:\own\app\python_code\.venv\Scripts\python.exe C:\own\app\python_code\work\IBL_click_all\full_automation.py 🚀 启动 Android 自动化测试框架... ℹ️ 应用已安装 📱 正在启动应用... 🔍 检测用户协议... 📄 用户协议页面已加载 👇 正在滚动协议内容... ⚠️ 达到最大滚动次数仍未激活 Agree 按钮 🟢 未检测到用户协议,跳过 🔄 开始探索当前页面... 🔴 点击失败: resId=generic_scroll_view | desc='null' | class=ScrollView, 错误: Message: Method is not implemented Stacktrace: NotImplementedError: Method is not implemented at AndroidUiautomator2Driver.execute (C:\Users\qusg\.appium\node_modules\appium-uiautomator2-driver\node_modules\appium-android-driver\lib\commands\execute.js:20:11) at runCommandPromise (C:\own\app\Node\node_global\node_modules\appium\node_modules\@appium\base-driver\lib\basedriver\driver.ts:116:20) at C:\own\app\Node\node_global\node_modules\appium\node_modules\async-lock\lib\index.js:171:12 at AsyncLock._promiseTry (C:\own\app\Node\node_global\node_modules\appium\node_modules\async-lock\lib\index.js:306:31) at exec (C:\own\app\Node\node_global\node_modules\appium\node_modules\async-lock\lib\index.js:170:9) at AsyncLock.acquire (C:\own\app\Node\node_global\node_modules\appium\node_modules\async-lock\lib\index.js:189:3) at AndroidUiautomator2Driver.executeCommand (C:\own\app\Node\node_global\node_modules\appium\node_modules\@appium\base-driver\lib\basedriver\driver.ts:160:39) at processTicksAndRejections (node:internal/process/task_queues:105:5) at defaultBehavior (C:\own\app\Node\node_global\node_modules\appium\lib\appium.js:730:14) at AppiumDriver.executeWrappedCommand (C:\own\app\Node\node_global\node_modules\appium\lib\appium.js:843:16) at AppiumDriver.executeCommand (C:\own\app\Node\node_global\node_modules\appium\lib\appium.js:742:17) at asyncHandler (C:\own\app\Node\node_global\node_modules\appium\node_modules\@appium\base-driver\lib\protocol\protocol.ts:409:19) 🔴 点击失败: resId=eula_privacy_policy_text | desc='null' | class=TextView, 错误: Message: Method is not implemented Stacktrace: NotImplementedError: Method is not implemented at AndroidUiautomator2Driver.execute (C:\Users\qusg\.appium\node_modules\appium-uiautomator2-driver\node_modules\appium-android-driver\lib\commands\execute.js:20:11) at runCommandPromise (C:\own\app\Node\node_global\node_modules\appium\node_modules\@appium\base-driver\lib\basedriver\driver.ts:116:20) at C:\own\app\Node\node_global\node_modules\appium\node_modules\async-lock\lib\index.js:171:12 at AsyncLock._promiseTry (C:\own\app\Node\node_global\node_modules\appium\node_modules\async-lock\lib\index.js:306:31) at exec (C:\own\app\Node\node_global\node_modules\appium\node_modules\async-lock\lib\index.js:170:9) at AsyncLock.acquire (C:\own\app\Node\node_global\node_modules\appium\node_modules\async-lock\lib\index.js:189:3) at AndroidUiautomator2Driver.executeCommand (C:\own\app\Node\node_global\node_modules\appium\node_modules\@appium\base-driver\lib\basedriver\driver.ts:160:39) at processTicksAndRejections (node:internal/process/task_queues:105:5) at defaultBehavior (C:\own\app\Node\node_global\node_modules\appium\lib\appium.js:730:14) at AppiumDriver.executeWrappedCommand (C:\own\app\Node\node_global\node_modules\appium\lib\appium.js:843:16) at AppiumDriver.executeCommand (C:\own\app\Node\node_global\node_modules\appium\lib\appium.js:742:17) at asyncHandler (C:\own\app\Node\node_global\node_modules\appium\node_modules\@appium\base-driver\lib\protocol\protocol.ts:409:19) 🔴 点击失败: resId=no | text='I disagree' | desc='null' | class=Button, 错误: Message: Method is not implemented Stacktrace: NotImplementedError: Method is not implemented at AndroidUiautomator2Driver.execute (C:\Users\qusg\.appium\node_modules\appium-uiautomator2-driver\node_modules\appium-android-driver\lib\commands\execute.js:20:11) at runCommandPromise (C:\own\app\Node\node_global\node_modules\appium\node_modules\@appium\base-driver\lib\basedriver\driver.ts:116:20) at C:\own\app\Node\node_global\node_modules\appium\node_modules\async-lock\lib\index.js:171:12 at AsyncLock._promiseTry (C:\own\app\Node\node_global\node_modules\appium\node_modules\async-lock\lib\index.js:306:31) at exec (C:\own\app\Node\node_global\node_modules\appium\node_modules\async-lock\lib\index.js:170:9) at AsyncLock.acquire (C:\own\app\Node\node_global\node_modules\appium\node_modules\async-lock\lib\index.js:189:3) at AndroidUiautomator2Driver.executeCommand (C:\own\app\Node\node_global\node_modules\appium\node_modules\@appium\base-driver\lib\basedriver\driver.ts:160:39) at processTicksAndRejections (node:internal/process/task_queues:105:5) at defaultBehavior (C:\own\app\Node\node_global\node_modules\appium\lib\appium.js:730:14) at AppiumDriver.executeWrappedCommand (C:\own\app\Node\node_global\node_modules\appium\lib\appium.js:843:16) at AppiumDriver.executeCommand (C:\own\app\Node\node_global\node_modules\appium\lib\appium.js:742:17) at asyncHandler (C:\own\app\Node\node_global\node_modules\appium\node_modules\@appium\base-driver\lib\protocol\protocol.ts:409:19) 🔴 点击失败: resId=yes | text='I agree' | desc='null' | class=Button, 错误: Message: Method is not implemented Stacktrace: NotImplementedError: Method is not implemented at AndroidUiautomator2Driver.execute (C:\Users\qusg\.appium\node_modules\appium-uiautomator2-driver\node_modules\appium-android-driver\lib\commands\execute.js:20:11) at runCommandPromise (C:\own\app\Node\node_global\node_modules\appium\node_modules\@appium\base-driver\lib\basedriver\driver.ts:116:20) at C:\own\app\Node\node_global\node_modules\appium\node_modules\async-lock\lib\index.js:171:12 at AsyncLock._promiseTry (C:\own\app\Node\node_global\node_modules\appium\node_modules\async-lock\lib\index.js:306:31) at exec (C:\own\app\Node\node_global\node_modules\appium\node_modules\async-lock\lib\index.js:170:9) at AsyncLock.acquire (C:\own\app\Node\node_global\node_modules\appium\node_modules\async-lock\lib\index.js:189:3) at AndroidUiautomator2Driver.executeCommand (C:\own\app\Node\node_global\node_modules\appium\node_modules\@appium\base-driver\lib\basedriver\driver.ts:160:39) at processTicksAndRejections (node:internal/process/task_queues:105:5) at defaultBehavior (C:\own\app\Node\node_global\node_modules\appium\lib\appium.js:730:14) at AppiumDriver.executeWrappedCommand (C:\own\app\Node\node_global\node_modules\appium\lib\appium.js:843:16) at AppiumDriver.executeCommand (C:\own\app\Node\node_global\node_modules\appium\lib\appium.js:742:17) at asyncHandler (C:\own\app\Node\node_global\node_modules\appium\node_modules\@appium\base-driver\lib\protocol\protocol.ts:409:19) ✅ 当前页无新元素可操作 ⏸️ 暂时处于外部页面: com.google.android.apps.nexuslauncher/.NexusLauncherActivity Process finished with exit code -1 修改代码
最新发布
10-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值