当前位置:网站首页>Airtest解决“自动装包”过程中需要输入密码的问题(同适用于随机弹框处理)
Airtest解决“自动装包”过程中需要输入密码的问题(同适用于随机弹框处理)
2022-07-25 20:51:00 【测试界的飘柔】
前言
前俩天看到Airtest讨论群里面提出了1个有意思的问题:一位同学在 测试自动装包 的过程中,发现像oppo、vivo这类品牌的手机在装包过程中都需要输入账号密码,而这会直接让 install() 指令运行失败 。
值得思考的是,我们如何保证在安装应用的过程中,同时完成输入账号密码的任务而不中断安装应用的任务呢?
利用多线程解决装包过程中
输入密码的问题
看到这里,相信很多同学已经想到了可以利用多线程来解决这个问题了。没错,利用python的多线程确实可以帮助我们轻松地实现同时 运行多个任务 。
关于新建线程,我们需要用到python标准库里面的 threading 模块:
from threading import Thread
# 新建1个新的线程
# 其中function为线程函数,args为传递给线程函数的参数,它必须是tuple类型
t1 = threading.Thread(function, args)
# 启动线程活动
t1.start()
# 将线程设置为守护线程
简单了解完python线程的一些基础用法,我们回到刚才的问题上来,在执行安装应用的任务之前,我们可以先开启1个线程,用于等待输入账号密码弹窗的弹出,以及输入账号密码、点击安装等操作:

完整的示例代码如下:
PS:感谢Airtest用户“jxou”提供了上述的解决思路。
扩展阅读:学会使用Python的threading模块、掌握并发编程基础
知识拓展
1.install()与uninstall()
install() 与 uninstall() 都是Airtest的核心API,分别用于 安装应用和卸载应用 ,并且它们都 仅支持安卓平台 :
install(filename) ,其中 filename 为被安装应用的路径
uninstall(package), 其中 package 为被卸载应用的包名
示例:
from airtest.core.api import *
install(r"D:\demo\tutorial-blackjack-release-signed.apk")
uninstall("org.cocos2dx.javascript")
2.install_app()与uninstall_app()
在 airtest的安卓模块 下,也提供了安装和卸载应用的方法,分别是 install_app() 和 uninstall_app() 。
相比于 install() 方法,install_app(filename,replace=False,install_options=[]) 的参数更加丰富:
filepath ,apk文件在PC上的完整路径
replace , 如果应用已存在,是否替换,默认为False
install_options ,install 命令的额外选项,默认是[],可填入 “-l”、“-t”、“-s”、“-d"和”-g" 等参数,用于 控制安装apk的行为
其中,install_options 的各参数的含义如下:
“-l” ,将应用安装到保护目录/mnt/asec
“-t” ,允许安装AndroidManifest.xml里application指定android:testOnly="true"的应用
“-s” ,将应用安装到sdcard
“-d” ,允许降级覆盖安装
“-g” ,授予所有运行时权限
示例:
from airtest.core.android.android import *
android = Android()
# 非覆盖安装
android.install_app(r"D:\demo\tutorial-blackjack-release-signed.apk",False)
# 覆盖安装
android.install_app(r"D:\demo\tutorial-blackjack-release-signed.apk",True)
android.uninstall("org.cocos2dx.javascript")
另外这个安卓模块下,还提供了一些其它的与应用相关的方法:
list_app(third_only=False) ,返回packages列表,third_only如果为 True ,只返回所有第三方应用列表
path_app(package) ,打印出package的完整路径
check_app(package) ,检查package在设备中是否存在
实际的测试应用中,比如我们要往设备中安装某个应用,可以先使用 check_app(package) 检查当前设备是否已经安装了该应用,然后再决定是直接安装还是覆盖安装:
from airtest.core.android.android import *
android = Android()
try:
android.check_app("org.cocos2dx.javascript")
print("当前设备已存在待安装应用,执行覆盖安装")
android.install_app(r"D:\demo\tutorial-blackjack-release-signed.apk",True)
except AirtestError:
print("当前设备不存在待安装应用,执行非覆盖安装")
android.install_app(r"D:\demo\tutorial-blackjack-release-signed.apk",False)
3.start_app()、stop_app()与clear_app()
其它常用于应用操作的方法还有 start_app() 、stop_app() 与 clear_app():
start_app() ,在设备上启动目标应用,适用于Android和iOS平台
stop_app() ,终止目标应用在设备上的运行,适用于Android和iOS平台
clear_app() ,清理设备上的目标应用数据,仅适用于Android平台
小结
今天主要跟大家分享了用多线程解决自动装包过程需要输入账号密码的问题,另外还给大家补充了一些airtest在应用操作这方面的方法,奉上新鲜整理好的思维导图,有需要的同学可以先保存起来方便后续查看哦:

最后: 可以在公众号:伤心的辣条 ! 自行领取一份216页软件测试工程师面试宝典文档资料【免费的】。以及相对应的视频学习教程免费分享!,其中包括了有基础知识、Linux必备、Shell、互联网程序原理、Mysql数据库、抓包工具专题、接口测试工具、测试进阶-Python编程、Web自动化测试、APP自动化测试、接口自动化测试、测试高级持续集成、测试架构开发测试框架、性能测试、安全测试等。
现在我邀请你进入我们的软件测试学习交流群:【746506216】,备注“入群”, 大家可以一起探讨交流软件测试,共同学习软件测试技术、面试等软件测试方方面面,还会有免费直播课,收获更多测试技巧,我们一起进阶Python自动化测试/测试开发,走向高薪之路。
喜欢软件测试的小伙伴们,如果我的博客对你有帮助、如果你喜欢我的博客内容,请 “点赞” “评论” “收藏” 一 键三连哦!
软件测试工程师自学教程:
这才是2022最精细的自动化测试自学教程,我把它刷了无数遍才上岸字节跳动,做到涨薪20K【值得自学软件测试的人刷】
软件测试工程师月薪2W以上薪资必学技能 — Python接口自动化框架封装.
美团面试真题_高级测试25K岗位面试 — 软件测试人都应该看看
软件测试必会_Jmeter大厂实战 — 仅6步可实现接口自动化测试

边栏推荐
- Volcanic engine Xiang Liang: machine learning and intelligent recommendation platform multi cloud deployment solution officially released
- [cloud native] use of Nacos taskmanager task management
- 预处理指令
- Detailed explanation of document operation
- Miscellaneous notes -- a hodgepodge
- [fiddlertx plug-in] use Fiddler to capture the package Tencent classroom video download (unable to capture the package solution)
- Struct, enum type and union
- Cesium 多边形渐变色纹理(Canvas)
- Scan delete folder problems
- Google guava is just a brother. What is the real king of caching? (glory Collection Edition)
猜你喜欢

Key network protocols in tcp/ip four layer model

Leetcode-6131: the shortest dice sequence impossible to get

leetcode-6127:优质数对的数目

If the order is not paid for 30 minutes, it will be automatically cancelled. How to achieve this? (Collection Edition)

Yolov7 training error indexerror: list index out of range

Detailed explanation of document operation

FanoutExchange交换机代码教程
![[onnx] export pytorch model to onnx format: support multi parameter and dynamic input](/img/bd/e9a1d3a2c9343b75dbae5c7e18a87b.png)
[onnx] export pytorch model to onnx format: support multi parameter and dynamic input

Character function and string function (2)

Kubernetes进阶部分学习笔记
随机推荐
PayPal PHP product trial period "recommended collection"
Increase swap space
租房二三事
Yolov7 training error indexerror: list index out of range
KEGG通路的从属/注释信息如何获取
leetcode-6126:设计食物评分系统
"Chain" connects infinite possibilities: digital asset chain, wonderful coming soon!
Remote—实战
In June 2021, the interview suffered a Waterloo. Is it so convoluted now
Compilation and operation of program
Vulnhub | dc: 5 | [actual combat]
Question and answer 47: geeks have an appointment - the current monitoring system construction of CSC
Recommended books | essentials of industrial digital transformation: methods and Practice
牛客-TOP101-BM38
If the order is not paid for 30 minutes, it will be automatically cancelled. How to achieve this? (Collection Edition)
Online XML to JSON tool
IEC61131 address representation
Use of C log4net: add file name and line number to the output log content; Repackaged class output file name and line number
Embedded development: embedded foundation -- threads and tasks
Too many passwords, don't know how to record? Why don't you write a password box applet yourself
