Airtest

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


快速开始

Airtest is a cross-platform automated testing framework focusing mainly on games, but can also be used for native apps. Windows and Android are currently supported; iOS support will be available in the near future.

Airtest provides cross-platform APIs, including app installation, simulated input, assertion and so forth. Airtest uses image recognition technology to locate UI elements, so that you can automate test on games without injecting any code. The test will generate an HTML report, which allows you to quickly locate failed test cases.

AirtestIDE is an out of the box GUI tool that helps to create and record test cases in a user-friendly way. AirtestIDE provides QA with a complete production workflow: record -> replay -> report

从官网开始上手吧

安装

这部分说明了如何安装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了。

文档

You can find the complete Airtest documentation on readthedocs.

例子

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

  1. 通过ADB连接一台安卓手机
  2. 安装应用APK
  3. 运行应用并截图
  4. 模拟用户输入(点击、滑动、按键)
  5. 卸载应用
from airtest.core.api import *

# connect an android phone with adb
connect_device("Android:///")
install("path/to/your/apk")
start_app("package_name_of_your_apk")
touch("image_of_a_button.png")
swipe("slide_start.png", "slide_end.png")
assert_exists("success.png")
keyevent("BACK")
home()
uninstall("package_name_of_your_apk")

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

基本使用方法

Airtest aims at providing platform independent APIs, so that you can write test once and run test on different devices.

  1. 使用 connect_device 来连接任意Android设备或者Windows窗口。
  2. 使用 模拟操作 的API来测试你的游戏或者App。
  3. 千万 不要 忘记 声明断言 来验证测试结果。

连接设备

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

connect_device("platform://host:port/uuid?param=value&param2=value2")

连接安卓设备

本地设备

  1. 通过usb将手机与电脑相连
  2. 命令行输入 adb devices 命令,确保手机连接状态是 device
  3. 在Airtest中连接设备
  4. 如果你连接了多个设备,或者有远程设备,那么使用参数来指定要连接的设备
# connect a local adb device using default params
connect_device("android:///")

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

连接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

声明断言

Airtest提供了以下断言方法:

  • assert_exists
  • assert_not_exists
  • assert_equal
  • assert_not_equal

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

用命令行运行 .air 脚本

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

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

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

运行测试

# run test test 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

生成报告

> 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": ...}

Import from other .air

You can write some common used function in one .air script and import it from other scripts. Airtest provide using API to manage the context change including sys.path and Template search path.

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

from common import common_function

common_function()