Win Pycharm + Airtest + 夜神模拟器 实现APP自动化

2023-07-29,,

前言:

前面已经讲过了Airtest的简单配置与使用了,相信大家已经对于操作Airtest没有什么问题了(#^.^#)

但是在Airtest IDE中编写代码是有局限性的,而且不能封装Airtest的高级属性,也不能对Airtest的各种功能进行耦合,此篇文件简单介绍下脱离与IDE来使用Pycharm进行操作。

一、安装airtest和poco库

1.进入File → Settings → Project → Python Interpreter

2.执行如下命令安装

pip3 install airtest
pip3 install pocoui

二、使用

1.确保模拟器已启动

2.代码实操

# -*- coding: utf-8 -*-

from airtest.core.api import *
from poco.drivers.android.uiautomation import AndroidUiautomationPoco poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False) auto_setup(__file__) print(poco.adb_client.get_device_info()) # 获取设备信息

3.运行结果

{'platform': 'Android', 'serialno': '127.0.0.1:62001', 'memory': '3G', 'storage': '128G', 'display': {'width': 2160, 'height': 3840, 'density': 4.0}, 'cpuinfo': None, 'cpufreq': '2.5GHz', 'cpuabi': 'x86', 'sdkversion': 25, 'gpu': {'gpuModel': 'Adreno (TM) 640', 'opengl': 'OpenGL ES 3.1'}, 'model': 'SM-G988N', 'manufacturer': 'samsung'}

三、简单示例(实现1+1=2)

# -*- coding: utf-8 -*-

from airtest.core.api import *
from poco.drivers.android.uiautomation import AndroidUiautomationPoco poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False) auto_setup(__file__) print(poco.adb_client.get_device_info()) # 获取设备信息 if poco(text="计算器").exists():
poco(text="计算器").click()
print("open suc")
else:
print("open fail") # 判断控件是否存在
if poco(text="计算器").exists():
poco(text="计算器").click()
print("打开计算器APP成功")
else:
print("未找到计算器APP") # 点击 1
poco(text="1").click()
# 点击 +
poco(text="+").click()
# 点击 1
poco(text="1").click()
# 点击 =
poco(text="=").click()

四、踩坑错误

如果出现此错误,新建个工程,把代码复制过去即可(原因未知 [○・`Д´・ ○])

                                  to be continued...

Win Pycharm + Airtest + 夜神模拟器 实现APP自动化的相关教程结束。

《Win Pycharm + Airtest + 夜神模拟器 实现APP自动化.doc》

下载本文的Word格式文档,以方便收藏与打印。