Appium win 系统 下获取当前 activity 有延迟吗?

li · July 06, 2016 · Last by li replied at July 07, 2016 · 1091 hits

大家好,目前在做应用商店的自动化脚本,想做的是,在下载管理界面,监控当前正在下载的进度,如果正常下载就print下载进度,如果出现异常,则print错误,脚本片段如下

from appium import webdriver as wd
#省略通用格式的代码
wbdriver = wd.Remote('http://127.0.0.1:4723/wd/hub',dc)
#紧跟着的脚本是点击一个下载,然后点击下载管理界面的按钮,跳转到下载管理的activity

downnumbutton = wbdriver.find_element_by_id('downPointsText') #显示 暂停、继续、重试三种下载状态的按钮
loading_text = wbdriver.find_element_by_id('loading_text') #进度条文字

while wbdriver.current_activity == '.mdcontent.usermanager.down.UserDownActivity':
if downnumbutton.text == '暂停':
print('正在下载,进度: ',loading_text.text,'\r',end='')
if downnumbutton.text == '继续' or downnumbutton.text == '重试':
print('下载出错')
wbdriver.quit()

运行以上脚本,每次python都是会报错,信息如下:

File "d:\haimamarket\downandinstall.py", line 37, in
print('正在下载,进度: ',loading_text.text,'\r',end='')
File "C:\Python35\lib\site-packages\selenium\webdriver\remote\webelement.py", line 69, in text
return self._execute(Command.GET_ELEMENT_TEXT)['value']
File "C:\Python35\lib\site-packages\selenium\webdriver\remote\webelement.py", line 456, in _execute
return self._parent.execute(command, params)
File "C:\Python35\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "C:\Python35\lib\site-packages\appium\webdriver\errorhandler.py", line 29, in check_response
raise wde
File "C:\Python35\lib\site-packages\appium\webdriver\errorhandler.py", line 24, in check_response
super(MobileErrorHandler, self).check_response(response)
File "C:\Python35\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: An element could not be located on the page using the given search parameters.

查看Appium中的信息是:

info: [debug] Getting focused package and activity
info: [debug] executing cmd: D:\Android\sdk\platform-tools\adb.exe -s 3DN4C16411024563 shell "dumpsys window windows"
info: [debug] Responding to client with success: {"status":0,"value":".mdcontent.usermanager.down.UserDownActivity","sessionId":"5827ad6c-6f80-4d81-a2ec-f46e6c6c8b7f"}
info: <-- GET /wd/hub/session/5827ad6c-6f80-4d81-a2ec-f46e6c6c8b7f/appium/device/current_activity 200 75.297 ms - 118 {"status":0,"value":".mdcontent.usermanager.down.UserDownActivity","sessionId":"5827ad6c-6f80-4d81-a2ec-f46e6c6c8b7f"}
info: --> GET /wd/hub/session/5827ad6c-6f80-4d81-a2ec-f46e6c6c8b7f/element/4/text {}
info: [debug] Pushing command to appium work queue: ["element:getText",{"elementId":"4"}]
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"element:getText","params":{"elementId":"4"}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: getText
info: [debug] [BOOTSTRAP] [debug] Returning result: {"status":0,"value":"鏆傚仠"}
info: [debug] Responding to client with success: {"status":0,"value":"鏆傚仠","sessionId":"5827ad6c-6f80-4d81-a2ec-f46e6c6c8b7f"}
info: <-- GET /wd/hub/session/5827ad6c-6f80-4d81-a2ec-f46e6c6c8b7f/element/4/text 200 49.005 ms - 80 {"status":0,"value":"鏆傚仠","sessionId":"5827ad6c-6f80-4d81-a2ec-f46e6c6c8b7f"}
info: --> GET /wd/hub/session/5827ad6c-6f80-4d81-a2ec-f46e6c6c8b7f/element/5/text {}
info: [debug] Pushing command to appium work queue: ["element:getText",{"elementId":"5"}]
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"action","action":"element:getText","params":{"elementId":"5"}}
info: [debug] [BOOTSTRAP] [debug] Got command of type ACTION
info: [debug] [BOOTSTRAP] [debug] Got command action: getText
info: [debug] [BOOTSTRAP] [debug] Returning result: {"status":7,"value":"UiSelector[INSTANCE=0, RESOURCE_ID=me.haima.androidassist:id\/loading_text]"}
info: [debug] Responding to client with error: {"status":7,"value":{"message":"An element could not be located on the page using the given search parameters.","origValue":"UiSelector[INSTANCE=0, RESOURCE_ID=me.haima.androidassist:id/loading_text]"},"sessionId":"5827ad6c-6f80-4d81-a2ec-f46e6c6c8b7f"}

我个人分析原因是:
当下载完成后,商店会自动调用安装,因此当前的activity就不再是 .mdcontent.usermanager.down.UserDownActivity ,理论上current_activity方法应该能检测到activity变化,但是,在商店调起安装的时候,current_activity返回的结果还是 .mdcontent.usermanager.down.UserDownActivity 。所以,依然进入了while循环,此时报错,找不到
控件

请教各位大神,我分析的原因是不是对的呢?这种情况下,我应该如何做,才能实现我的目的: 监控下载进度,当下载完成调起安装的activity后,能操作安装的activity 谢谢

共收到 2 条回复 时间 点赞
  1. 基于 Uiautomator 提供的 .getCurrentActivity() 方法获取的当前 Activity 并不准确, 如何获取当前页面的 Activity 名称?,安装界面会有这个 app 的名称、“xx权限”之类的文字,用这个判断有没有调起安装程序
  2. 下载时 uiautomatorviewer dump 一下,定位到进度条,截图发一下,顺便看看Uiautomator 自动化测试问题汇总与交流一楼
  3. 如果我做这个功能的自动化
    1. 准备一个安装文件体积很小的 app ,名称是“测试用”,在网络良好时15秒内能下载完
    2. 点击下载,等15秒,验证界面上有没有“测试用”,验证下载安装包的 md5 值对不对
    3. 准备一个安装文件体积很大的 app,点击下载,进入下载管理,验证 downnumbutton.text 是暂停,点击暂停,验证 downnumbutton.text 是"继续"
  4. 这是很适合分层的功能,网络处理相关的在 api 测试,界面逻辑和显示用 instrumentation + mock ,或者修改 SQLite 、preference 后用 Appium 验证界面(具体要看你们的 app 是怎么实现的)
li #2 · July 07, 2016 作者

#1楼 @sanlengjingvv 非常感谢您的解疑。我看Appium的log,感觉他应该是用adb shell dumpsys window windows来获取的activity,我手动试了一下这个方法

while True:
subprocess.check_output('adb shell dumpsys window windows | grep mFocused')

发现,在下载完成调起packageinstaller时,adb shell的返回结果还是比较及时的

然而,我通过一步步运行脚本发现,在调起packageinstaller的时候,Appium的current_activity方法,返回的结果还是商店的activity,并没有返回packageinstaller的activity

所以我觉得很困惑,感觉可能需要加上time.sleep,于是,我在if条件下边的print语句下边,加上了time.sleep(2),结果发现问题还是一样,这个问题我一直想不通怎么回事

您说的第二条,下载进度条是动态的,我只有暂停下载的时候,才能dump到,不知道这个会不会和我上边说的的问题有关

关于您说的第四点,能不能麻烦详细一点说一下,我是刚刚开始做自动化,请谅解

需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up