当前位置:网站首页>自动化测试 - uiautomator2框架应用 - 自动打卡
自动化测试 - uiautomator2框架应用 - 自动打卡
2022-06-29 06:41:00 【qazw9600】
说明
- 最近公司更改了打卡方式,改用手机APP定位打卡,类似于阿里的钉钉打卡。
- 为了学以致用,特编程实现自动打卡。
框架说明
- UIAutomator是Android提供的自动化测试框架,支持大部分Android系统操作,是用来做UI黑盒测试的,也就是模拟人工的手工测试,例如:点击控件元素看看显示是否符合预期,特点如下:
- 黑盒测试,模拟人的操作,不需要了解实现细节。
- 不依赖于某个APP,能跨App运行。
- 不需要root权限。
- 缺点是只支持SDK 16(Android 4.1)及以上,不支持Hybird App、WebApp。
- 使用java编写,需要特殊运行方式。
- 本文中使用的uiautomator2框架是UIAutomator的python封装框架,使用python编写(编写简单,方便),远程(通过wifi或者usb连接设备)独立运行。
环境准备
- 个人是在ubuntu18.04上配置的uiautomator2环境,windows也一样,之前安装过。
- 依赖环境
- 安装adb;用来识别和连接设备
- Python3,pip3环境;脚本运行环境和Uiautomator2库安装工具
- 库安装
pip3 install uiautomator2 //自动化测试库
- 手机环境配置
python3 -m uiautomator2 init //会自动在手机上安装用到的工具,需要用户同意安装
- 注意:
- 网上很多博客中,使用的工具是pip和python,使用pip也能安装,但是安装的uiautomator2比较老,可能会遇到很多异常,pip安装的可能是python2的库,使用python也类似,最好是使用python3环境下的工具,比较新,兼容性更好,一般不会出太多异常。
- 环境验证脚本;环境配置好后,测试下库是否可用
* 脚本
import uiautomator2 as u2
d = u2.connect('xxxx') #设备ip或者设备名(adb devices中显示的)
print(d.info)
* 按照普通python脚本方式运行,正常会打印出设备的一些信息。
框架使用
- 由于框架是UI黑盒测试,需要识别和定位界面元素,才能够进行相应操作,需要使用到界面元素识别工具,如下:
pip3 install weditor #安装
python3 -m weditor #工具运行
- weditor工具运行后,会自动弹出网页界面,连接设备后会实时显示设备UI界面,并显示界面元素的相关信息,例如:可用来编程定义的resource id等。
编码实现
- 能够识别和定义界面元素后即可使用uiautomator2框架的api对界面元素进行操作,就能实现操作的自动化。
- uiautomator2框架接口文档 [待补充]。
自动打卡实现
- 分析:类似的打卡app都有实现设备绑定,通信的加密等防作弊手段,想要从这些方面入手,比较难,只能通过最简单也是最无解的手段(模拟用户操作)实现。
- 打卡代码示例:
* 使用定时库schedule,实现定时操作
schedule.clear()
schedule.every(1).days.at("08:45").do(clockIn)
* 打卡操作
d = u2.connect('c746dd057d28') #连接设备
while 1:
try:
d.app_start("com.weaver.emobile7")#打开app
d(resourceId="com.weaver.emobile7:id/layout_three").wait(timeout=3.0)
d(resourceId="com.weaver.emobile7:id/layout_three").click() #点击菜单tab 3
d(text="app016").wait(timeout=1.0)
d(text="app016").click() #点击考勤项,进入打卡界面
except Exception as e:
d(resourceId="com.android.systemui:id/home").click()
try:
d.app_stop("com.weaver.emobile7") #异常关闭app
except Exception as e:
pass
continue
try:
d(text="打卡").wait(timeout=3.0)
a = d(text="打卡").get_text() == '打卡'
except Exception as e:
pass
else:
try:
d(text="打卡").click()
time.sleep(1)
except:
pass
try:
a = d(text="更新打卡时间").get_text() == '更新打卡时间'
except Exception as e:
try:
d.click(0.54, 0.826) #打卡控件的坐标,避免上面代码未识别到打卡控件,直接点击固定坐标
except:
pass
d(resourceId="com.android.systemui:id/back").click()
else:
d(text="更新打卡时间").click()
exit(0) #出现更新打卡时间,表示打卡成功
time.sleep(1)
- 注意事项:
- 异常处理;界面显示可能不会那么及时,需要做延时或者等待处理,界面元素不存在会导致脚本操作超时异常,脚本运行结束,需要加上异常处理。
- 稳定性;当前是通过usb连接手机,adb操作手机,这一套是否满足长时间后再使用不出错,如果能在手机上运行应该会更稳定,但是看其他人的博客实现起来比较麻烦。
- 手机异常;手机自动息屏,息屏后需要输入密码,个人定的早起闹钟等都可能影响界面显示,需要关闭。
- 网络异常;有些打卡app需要连接公司网络,为防止被发现,不应该一直连接公司网络,需要定时开启。
边栏推荐
- code::blocks代码格式化快捷键
- 蓝桥杯——最小框架
- Detailed explanation of communication principle between [industrial control old horse] single chip microcomputer and Siemens S7-200
- 719. 找出第 K 小的数对距离(二分)
- [industrial control old horse] detailed explanation of design principle of pattern fountain based on PLC
- 358. K distance interval rearrange string sorting
- Matlab Simulink simulation and analysis of power grid sweep frequency
- 帆船动力学仿真分析
- Postman pre request
- 【深度之眼吴恩达机器学习作业班第四期】Logistic Regression 逻辑回归总结
猜你喜欢

Listen to textarea input through Keyup to change button style

Alicloud access resource: nosuchkey

Appium automation test foundation ADB common commands (III)
[translation] swarmed out. Design methods for building modern applications

【工控老马】PLC六路抢答器系统设计详解

SQL injection bypass (6)

cv2.cvtColor

Dump (cl\alv\tree\base================================cp|set\items\for\column) when expanding node or clicking toolbar button

Kingbasees v8r6 cluster maintenance case -- single instance data migration to cluster case

【工控老马】基于西门子S7-200PLC的跑马灯控制系统的设计方案详解
随机推荐
【量化投资系统】问题记录及解决方法
SAP ui5 Beginner (I) Introduction
打包时提示: Property ‘sqlSessionFactory‘ or ‘sqlSessionTemplate‘
Simulation analysis of sailing dynamics
【工控老马】基于西门子S7-200PLC的跑马灯控制系统的设计方案详解
[industrial control old horse] detailed explanation of design principle of pattern fountain based on PLC
What you should know about databases
Appium environment setup
Roblox剑九之剑二
4年工作经验,多线程间的5种通信方式都说不出来,你敢信?
498. 对角线遍历(模拟)
ShapeShifter: Robust Physical Adversarial Attack on Faster R-CNN Object Detector
Kingbasees v8r6 cluster maintenance case -- single instance data migration to cluster case
From XX import* is equivalent to from XX import *, and no space is required
Using cdockablepane to realize floating window in MFC
【工控老马】西门子PLC s7-300SCL编程详解
Vulnhub's dc6 target
Problem solving -- > online OJ (13)
Vulnhub's dc7 target
Explanation of swing transformer theory