本文将介绍如何使用uiautomator2对手机app进行点击、翻页、滑动等操作,本次目标app为考研帮
为什么要使用uiautomator2 ?
- 化境搭建便捷
- UI控件识别有专业的工具(weditor),可视化好
- UI自动化编写采用python,学习成本低,并且文档为中文文档,非常友好
- UI自动化脚本运行稳定。
uiautomator2 支持的环境
- Android版本 4.4 +
- Python 3.6 + (3.8暂不支持)
首先在我们本机进行adb工具安装,那么什么是adb工具呢?adb工具全称(Android调试桥)命令行窗口,用于通过电脑端与模拟器或者是设备之间的交互。adb工具包含以下3个部分
- adb client :命令行程序,“adb”用于从shell或者脚本中运行adb命令
- adb server: ADB Server是运行在PC上的一个后台进程
- adbd: 程序“adbd” 作为一个后台进程在Android设备或者模拟器系统中运行
adb能用来做什么?
可以安装卸载apk、移动设备和PC之间拷贝文件、查看设备上安装的应用信息、文件管理、按键操作等等。
夜神安装目录
adb命令
网络设置
并在cmd命令行输入adb devices,如下输出说明已成功连接上模拟器上的移动设备,如果连接的是真机将会显示一串英文字母
adb devices
考研帮App安装,首先将考研帮的apk文件下载到本地,然后执行 adb install 文件(apk)路径,如下显示安装成功
应用安装
应用安装
uiautomator2 项目初始化
- 通过 pip install uiautomator2 在python环境下安装uiautomator2包。
-
在cmd命令行python -m uiautomator2 init (python37 -m uiautomator2 init)对夜神模拟器上的移动设备进行一个初始化,也就是在移动设备上安装一个Agent,如下图所示初始化初始化
-
uiautomator2连接移动设备,移动设备的连接方式有多种,本次采用wifi的形式连接,首先查看移动设备的ip查看ip
完成以上准备工作后,我们就可以对设备进行操作了
import time
import uiautomator2 as u2
#通过手机(夜神模拟器)wifi来进行连接,需要查看手机(夜神模拟器)的ip
d = u2.connect_wifi("192.168.0.100")
print(d.device_info) # 查看设备信息
# uiautomator服务的启动
d.uiautomator.start()
time.sleep(2)
# 查看uiautomator的运行状态,是否在运行
print(d.uiautomator.running())
# uiautomator服务的关闭
d.uiautomator.stop()
time.sleep(2)
# 查看uiautomator的运行状态
print(d.uiautomator.running())
# 查看atx-agent运行状态
print(d.agent_alive)
# 查看屏幕的分辨率信息
print(d.window_size())
# 查看获取到的ip地址 注意:模拟器获取和真机不同,模拟器获取的是错误的
print(d.wlan_ip)
# 打开 考研帮 app
d.app_start("com.tal.kaoyan")
time.sleep(15)
# 关闭app
d.app_stop("com.tal.kaoyan")
# 获取当前前台运行的app信息
print(d.app_current())
# 获取app详细信息
print(d.app_info(package_name="com.tal.kaoyan"))
#清除app缓存
d.app_clear(package_name="com.tal.kaoyan")
# 获取所有app的列表
print(d.app_list())
#获取正在运行的app列表
print(d.app_list_running())
# 卸载app
d.app_uninstall("com.tal.kaoyan")
# 卸载所有的app, 卸载所有的第三方包,u2项目包不会卸载
d.app_uninstall_all()
#通过app_install 方法安装apk,传入app下载地址
d.app_install("http://xxxxxxxxxxxxxxx")
安装weditor
pip install weditor -i https://pypi.tuna.tsinghua.edu.cn/simple
weditor启动
直接在CMD命令行输入weditorweditor启动
weditor
import uiautomator2 as u2
class HandleKaoyanbang:
def __init__(self, ip_addr="192.168.0.108"):
# 当前是通过wifi的方法来连接移动设备
self.d = u2.connect_wifi(ip_addr)
self.size = self.get_windowsize()
self.handle_watcher()
def handle_watcher(self):
"""定义一个监控器"""
#监控器会单独启动一个线程
# 点击弹出的用户隐私协议
self.d.watcher.when('//*[@resource-id="com.tal.kaoyan:id/tip_commit"]').click() #参数传入xpath路径
# 广告点击取消
self.d.watcher.when('//*[@resource-id="com.tal.kaoyan:id/tv_skip"]').click()
# 监控器写好之后,要通过start方法来启动
self.d.watcher.start()
def get_windowsize(self):
"""获取手机屏幕的大小"""
return self.d.window_size()
def close_app(self):
# 监控器关闭
self.d.watcher.stop()
# 停止考研帮app
self.d.app_stop("com.tal.kaoyan")
# 清理缓存
self.d.app_clear("com.tal.kaoyan")
def handle_kaoyanbang_app(self):
"""启动考研帮app,并实现自动化操作"""
self.d.app_start(package_name="com.tal.kaoyan")
# 在点击之前需要判断控件是否存在
self.d(text='密码登录').click_exists(timeout=10)
# 通过找到相关控件之后,文本控件,set_text这个方法来输入文字
if self.d.wait_activity("com.kaoyan.kylogin.ui.login.LoginActivity", timeout=10):
self.d(resourceId="com.tal.kaoyan:id/login_email_edittext").set_text("你的个人账号")
# 输入密码
self.d.xpath('//*[@resource-id="com.tal.kaoyan:id/kylogin_unamelogin_operate_layout"]/android.widget.RelativeLayout[1]').set_text("登录密码")
self.d.click(361, 657) # 点击登录按钮
if self.d.wait_activity("com.tal.kaoyan.ui.activity.HomeTabActivity", timeout=10):
self.d(text='研讯').click_exists(timeout=10)
# 获取到屏幕的中心点,x轴
# 再获取到y轴远方点,获取到y轴近点(y轴坐标从下向上越来越小)
x1 = int(self.size[0] * 0.5)
y1 = int(self.size[1] * 0.9)
y2 = int(self.size[1] * 0.15)
while True:
# get toast,是安卓系统系统的一个信息提示操作
if self.d.toast.get_message(0) == "内容已经全部加载完了":
self.close_app()
break
# 开始滑动研训进行翻页操作
self.d.swipe(x1, y1, x1, y2)
if __name__ == "__main__":
k = HandleKaoyanbang()
k.handle_kaoyanbang_app()