Airtest

跨平台的UI自动化框架,适用于游戏和App


快速开始

  • 跨平台: Airtest可以在几乎所有平台上执行游戏和App的自动化。

  • 一次编写,随处运行: Airtest提供了跨平台的API,囊括了应用安装、模拟输入以及断言等。由于使用图像识别技术来定位UI元素,因此无需嵌入任何代码即可对游戏和应用进行自动化操作。

  • 可扩展性: 通过使用Airtest提供的命令行与python API接口,可以很轻松地在大规模设备集群上运行脚本。提供的HTML报告包含了详细操作步骤和截屏,能够迅速定位到失败点。同时,网易也提供了 Airlab 云测试平台服务。

  • AirtestIDE: AirtestIDE是一个强大的GUI工具,可以帮助你录制和调试测试脚本。 AirtestIDE提供了完整的自动化工作流程支持:录制脚本->真机回放->生成报告

从官网开始上手吧

支持平台

  • Android

  • iOS

  • Windows

  • Unity

  • Cocos2dx

  • 白鹭引擎

  • 微信小程序

Read more

安装

这部分说明了如何在本地python环境中安装Airtest测试框架。如果你需要使用GUI工具,请从 官网 直接下载AirtestIDE使用。

系统要求

  • 操作系统

    • Windows

    • MacOS X

    • Linux

  • Python2.7 & Python3.3+

安装Python Package

使用 pip 来管理安装包和自动安装所有依赖。

pip install -U airtest

你也可以直接从Git仓库安装。

git clone https://github.com/AirtestProject/Airtest.git
pip install -e airtest

因为Airtest还在快速开发中,这里使用 -e 来安装源码。以后你就可以直接使用 git pull 更新代码目录来升级Airtest了。

文档

完整的Airtest框架文档请查阅 readthedocs

例子

Airtest提供了简洁而且平台无关的API。这部分介绍了如何使用这些API来编写一个自动化脚本,步骤如下:

  1. 通过ADB连接一台安卓手机

  2. 安装应用APK

  3. 运行应用并截图

  4. 模拟用户输入(点击、滑动、按键)

  5. 卸载应用

from airtest.core.api import *

# connect an android phone with adb
init_device("Android")
# or use connect_device api
# connect_device("Android:///")

install("path/to/your/apk")
start_app("package_name_of_your_apk")
touch(Template("image_of_a_button.png"))
swipe(Template("slide_start.png"), Template("slide_end.png"))
assert_exists(Template("success.png"))
keyevent("BACK")
home()
uninstall("package_name_of_your_apk")

更多API和使用方法,请参考完整的 Airtest Python API reference ,或者直接看看 API code

基本使用方法

Airtest希望提供平台无关的API,让你的测试代码可以运行在不同平台的设备和应用上。

  1. 使用 connect_device 来连接任意Android/iOS设备或者Windows窗口。

  2. 使用 模拟操作 的API来自动化你的游戏或者App。

  3. 千万 不要 忘记 声明断言 来验证测试结果。

连接设备

使用 connect_device 来连接任意Android/iOS设备或者Windows窗口。

connect_device("platform://host:port/uuid?param=value&param2=value2")
  • platform: Android/iOS/Windows…

  • host: Android平台是adb host,iOS下是iproxy host,其他平台请留空

  • port: Android下是adb port,iOS下填写iproxy port,其他平台请留空

  • uuid: 目标设备的uuid,例如Android下是序列号,windows下是窗口句柄,iOS是uuid

  • param: 设备初始化的配置字段,例如cap_method/ori_method/…

  • value: 设备初始化字段的值。

查看 connect_devce 获取更多信息。

连接安卓设备

  1. 通过usb将手机与电脑相连

  2. 命令行输入 adb devices 命令,确保手机连接状态是 device

  3. 在Airtest中连接设备

  4. 如果你连接了多个设备,或者有远程设备,那么使用参数来指定要连接的设备

# connect an android phone with adb
init_device("Android")

# or use connect_device api with default params
connect_device("android:///")

# connect a remote device using custom params
connect_device("android://adbhost:adbport/1234566?cap_method=javacap&touch_method=adb")

连接iOS设备

根据 iOS-Tagent 的操作指引来配置环境。

# connect a local ios device
connect_device("ios:///")

连接windows应用

# connect local windows desktop
connect_device("Windows:///")

# connect local windows application
connect_device("Windows:///?title_re=unity.*")

Airtest使用了 pywinauto 作为操作Windows应用的底层库,更多窗口搜索的参数请看 pywinauto documentation

模拟输入

支持以下常用API:

  • touch

  • swipe

  • text

  • keyevent

  • snapshot

  • wait

支持更多API,其中部分是平台相关的API,请查看 API reference

在使用这些通用API时,Airtest会自动根据当前平台调用对应的操作,例如以下代码示例:

from airtest.core.api import *  # import the general APIs such as touch/swipe/...

connect_device("Android:///")
touch((100, 100))

connect_device("Windows:///")
touch((100, 100))

Airtest还针对各个平台,提供了一些平台专用API,可以分别在每个平台的模块API中查询到:

>>> from airtest.core.api import *
>>> connect_device("Android:///")
>>> dev = device()  # get current device, which is an Android object
>>> print(type(dev))
<class 'airtest.core.android.android.Android'>
>>> dev.get_top_activity()  # use Android ADB to get the top activity
('com.google.android.apps.nexuslauncher', '.NexusLauncherActivity', '2720')

声明断言

Airtest提供了以下断言方法:

  • assert_exists

  • assert_not_exists

  • assert_equal

  • assert_not_equal

当断言失败,会抛出 AssertsionError。所有断言都会在html报告中显示。

All assert statements can be found here: airtest.core.assertions

用命令行运行 .air 脚本

使用AirtestIDE你可以非常轻松地录制一个自动化脚本并保存为 .air 目录结构。Airtest命令行则让你能够脱离IDE,在不同宿主机器和被测设备上运行测试脚本。

你可以在命令行参数中指定连接的被测设备,这样就可以运行在不同的手机平台和宿主机器上。只要你的测试代码本身是平台无关的,你就可以在一个平台上录制脚本,然后在不同平台上运行。

下面的例子介绍了命令行的基本用法。可以配合我们提供的示例 airtest/playground/test_blackjack.air/ 来学习使用:

运行自动化用例

# run automated cases and scenarios on various devices
> airtest run "path to your .air dir" --device Android:///
> airtest run "path to your .air dir" --device Android://adbhost:adbport/serialno
> airtest run "path to your .air dir" --device Windows:///?title_re=Unity.*
> airtest run "path to your .air dir" --device iOS:///
...
# show help
> airtest run -h
usage: airtest run [-h] [--device [DEVICE]] [--log [LOG]]
                   [--recording [RECORDING]]
                   script

positional arguments:
  script                air path

optional arguments:
  -h, --help            show this help message and exit
  --device [DEVICE]     connect dev by uri string, e.g. Android:///
  --log [LOG]           set log dir, default to be script dir
  --recording [RECORDING]
                      record screen when running
  --compress
                      set snapshot quality, 1-99
  --no-image [NO_IMAGE]
                      Do not save screenshots

生成报告

> airtest report "path to your .air dir"
log.html
> airtest report -h
usage: airtest report [-h] [--outfile OUTFILE] [--static_root STATIC_ROOT]
                      [--log_root LOG_ROOT] [--record RECORD [RECORD ...]]
                      [--export EXPORT] [--lang LANG]
                      script

positional arguments:
  script                script filepath

optional arguments:
  -h, --help            show this help message and exit
  --outfile OUTFILE     output html filepath, default to be log.html
  --static_root STATIC_ROOT
                        static files root dir
  --log_root LOG_ROOT   log & screen data root dir, logfile should be
                        log_root/log.txt
  --record RECORD [RECORD ...]
                        custom screen record file path
  --export EXPORT       export a portable report dir containing all resources
  --lang LANG           report language

获取脚本信息

# print case info in json if defined, including: author, title, desc
> python -m airtest info "path to your .air dir"
{"author": ..., "title": ..., "desc": ...}

引用其他的 .air 脚本

可以将一些通用的操作写在一个 .air 脚本里,然后在其他脚本中 import 它。Airtest提供了 using 接口,能够将需要引用的脚本加入 sys.path 里,其中包含的图片文件也会被加入 Template 的搜索路径中。

from airtest.core.api import using
using("common.air")

from common import common_function

common_function()