{
  "schemaVersion": "0.11.0",
  "canonical": "https://www.pystone.net/notes/adb-airtest-poco-tips-tricks/",
  "atlas": "https://www.pystone.net/?node=adb-airtest-poco-tips-tricks#knowledge-atlas",
  "markdown": "https://www.pystone.net/notes/adb-airtest-poco-tips-tricks.md",
  "context": "https://www.pystone.net/notes/adb-airtest-poco-tips-tricks.context.json",
  "knowledgeVersion": "224c990773de.5fa8af6e39fa",
  "build": {
    "siteCommit": "224c990773de166d23a886306577dd90379529ce",
    "notesCommit": "5fa8af6e39fa3891d1b9b4832bfa6c4e0ecaaf0a",
    "builtAt": "1970-01-01T00:00:00.000Z",
    "version": "224c990773de.5fa8af6e39fa"
  },
  "id": "note:adb-airtest-poco-tips-tricks",
  "slug": "adb-airtest-poco-tips-tricks",
  "title": "【自动化测试】经验与技巧[adb&Aritest&Poco]",
  "type": "note",
  "visibility": "public",
  "idStability": "rename-stable",
  "author": {
    "name": "Perrin Yong",
    "profile": "https://www.pystone.net/profile/"
  },
  "publisher": {
    "name": "Perrin Yong",
    "profile": "https://www.pystone.net/profile/"
  },
  "aliases": [],
  "summary": "【自动化测试】经验与技巧[adb&Aritest&Poco]",
  "contentRole": "unspecified",
  "isMoc": false,
  "mocRecognition": "none",
  "generated": false,
  "attribution": "unspecified",
  "domain": "10-计算机、信息技术与工程",
  "tags": [],
  "mocs": [],
  "contentHash": "d6eaf22168a0a644ca5766c40716239d4e03b9febd1bf8d3facb27b53b7ea2ce",
  "assets": [
    {
      "reference": "assets/Image.png",
      "url": "/media/02c20bcc2fbab5952654.png",
      "mediaType": "image/png",
      "contentHash": "02c20bcc2fbab5952654be61a9f25dfda2322b4a72ac97b67399d3b3f870fbbf",
      "byteLength": 23357,
      "width": 710,
      "height": 180
    },
    {
      "reference": "assets/Image_1.png",
      "url": "/media/4b0b20b5b8c9c8fe631c.png",
      "mediaType": "image/png",
      "contentHash": "4b0b20b5b8c9c8fe631ca7245662ec81af74250056d809c8ab62306ce54ac81f",
      "byteLength": 16295,
      "width": 594,
      "height": 172
    }
  ],
  "headings": [
    {
      "depth": 1,
      "text": "【自动化测试】经验与技巧[adb&Aritest&Poco]",
      "anchor": "自动化测试经验与技巧adbaritestpoco",
      "citation": "https://www.pystone.net/notes/adb-airtest-poco-tips-tricks/#%E8%87%AA%E5%8A%A8%E5%8C%96%E6%B5%8B%E8%AF%95%E7%BB%8F%E9%AA%8C%E4%B8%8E%E6%8A%80%E5%B7%A7adbaritestpoco"
    },
    {
      "depth": 2,
      "text": "ADB技巧",
      "anchor": "adb技巧",
      "citation": "https://www.pystone.net/notes/adb-airtest-poco-tips-tricks/#adb%E6%8A%80%E5%B7%A7"
    },
    {
      "depth": 3,
      "text": "连接安卓设备",
      "anchor": "连接安卓设备",
      "citation": "https://www.pystone.net/notes/adb-airtest-poco-tips-tricks/#%E8%BF%9E%E6%8E%A5%E5%AE%89%E5%8D%93%E8%AE%BE%E5%A4%87"
    },
    {
      "depth": 3,
      "text": "Profiler: Editor-to-Android connection",
      "anchor": "profiler-editor-to-android-connection",
      "citation": "https://www.pystone.net/notes/adb-airtest-poco-tips-tricks/#profiler-editor-to-android-connection"
    },
    {
      "depth": 3,
      "text": "log抓取",
      "anchor": "log抓取",
      "citation": "https://www.pystone.net/notes/adb-airtest-poco-tips-tricks/#log%E6%8A%93%E5%8F%96"
    },
    {
      "depth": 3,
      "text": "获取包名等",
      "anchor": "获取包名等",
      "citation": "https://www.pystone.net/notes/adb-airtest-poco-tips-tricks/#%E8%8E%B7%E5%8F%96%E5%8C%85%E5%90%8D%E7%AD%89"
    },
    {
      "depth": 3,
      "text": "启动游戏",
      "anchor": "启动游戏",
      "citation": "https://www.pystone.net/notes/adb-airtest-poco-tips-tricks/#%E5%90%AF%E5%8A%A8%E6%B8%B8%E6%88%8F"
    },
    {
      "depth": 2,
      "text": "Airtest",
      "anchor": "airtest",
      "citation": "https://www.pystone.net/notes/adb-airtest-poco-tips-tricks/#airtest"
    },
    {
      "depth": 3,
      "text": "相关概念",
      "anchor": "相关概念",
      "citation": "https://www.pystone.net/notes/adb-airtest-poco-tips-tricks/#%E7%9B%B8%E5%85%B3%E6%A6%82%E5%BF%B5"
    },
    {
      "depth": 4,
      "text": "proxy",
      "anchor": "proxy",
      "citation": "https://www.pystone.net/notes/adb-airtest-poco-tips-tricks/#proxy"
    },
    {
      "depth": 4,
      "text": "其他",
      "anchor": "其他",
      "citation": "https://www.pystone.net/notes/adb-airtest-poco-tips-tricks/#%E5%85%B6%E4%BB%96"
    },
    {
      "depth": 3,
      "text": "常用API",
      "anchor": "常用api",
      "citation": "https://www.pystone.net/notes/adb-airtest-poco-tips-tricks/#%E5%B8%B8%E7%94%A8api"
    },
    {
      "depth": 4,
      "text": "基本",
      "anchor": "基本",
      "citation": "https://www.pystone.net/notes/adb-airtest-poco-tips-tricks/#%E5%9F%BA%E6%9C%AC"
    },
    {
      "depth": 4,
      "text": "Connect",
      "anchor": "connect",
      "citation": "https://www.pystone.net/notes/adb-airtest-poco-tips-tricks/#connect"
    },
    {
      "depth": 4,
      "text": "Simulate Input",
      "anchor": "simulate-input",
      "citation": "https://www.pystone.net/notes/adb-airtest-poco-tips-tricks/#simulate-input"
    },
    {
      "depth": 4,
      "text": "Make Assertion",
      "anchor": "make-assertion",
      "citation": "https://www.pystone.net/notes/adb-airtest-poco-tips-tricks/#make-assertion"
    },
    {
      "depth": 3,
      "text": "Running .air from CLI",
      "anchor": "running-air-from-cli",
      "citation": "https://www.pystone.net/notes/adb-airtest-poco-tips-tricks/#running-air-from-cli"
    },
    {
      "depth": 4,
      "text": "run automated case",
      "anchor": "run-automated-case",
      "citation": "https://www.pystone.net/notes/adb-airtest-poco-tips-tricks/#run-automated-case"
    },
    {
      "depth": 4,
      "text": "generate html report",
      "anchor": "generate-html-report",
      "citation": "https://www.pystone.net/notes/adb-airtest-poco-tips-tricks/#generate-html-report"
    },
    {
      "depth": 4,
      "text": "get case info",
      "anchor": "get-case-info",
      "citation": "https://www.pystone.net/notes/adb-airtest-poco-tips-tricks/#get-case-info"
    },
    {
      "depth": 3,
      "text": "设备连接问题",
      "anchor": "设备连接问题",
      "citation": "https://www.pystone.net/notes/adb-airtest-poco-tips-tricks/#%E8%AE%BE%E5%A4%87%E8%BF%9E%E6%8E%A5%E9%97%AE%E9%A2%98"
    },
    {
      "depth": 4,
      "text": "Some special parameters",
      "anchor": "some-special-parameters",
      "citation": "https://www.pystone.net/notes/adb-airtest-poco-tips-tricks/#some-special-parameters"
    },
    {
      "depth": 3,
      "text": "相关网站",
      "anchor": "相关网站",
      "citation": "https://www.pystone.net/notes/adb-airtest-poco-tips-tricks/#%E7%9B%B8%E5%85%B3%E7%BD%91%E7%AB%99"
    },
    {
      "depth": 3,
      "text": "设备支持问题",
      "anchor": "设备支持问题",
      "citation": "https://www.pystone.net/notes/adb-airtest-poco-tips-tricks/#%E8%AE%BE%E5%A4%87%E6%94%AF%E6%8C%81%E9%97%AE%E9%A2%98"
    },
    {
      "depth": 2,
      "text": "Poco",
      "anchor": "poco",
      "citation": "https://www.pystone.net/notes/adb-airtest-poco-tips-tricks/#poco"
    },
    {
      "depth": 3,
      "text": "SDK集成",
      "anchor": "sdk集成",
      "citation": "https://www.pystone.net/notes/adb-airtest-poco-tips-tricks/#sdk%E9%9B%86%E6%88%90"
    },
    {
      "depth": 3,
      "text": "术语",
      "anchor": "术语",
      "citation": "https://www.pystone.net/notes/adb-airtest-poco-tips-tricks/#%E6%9C%AF%E8%AF%AD"
    },
    {
      "depth": 3,
      "text": "多设备连接与初始化",
      "anchor": "多设备连接与初始化",
      "citation": "https://www.pystone.net/notes/adb-airtest-poco-tips-tricks/#%E5%A4%9A%E8%AE%BE%E5%A4%87%E8%BF%9E%E6%8E%A5%E4%B8%8E%E5%88%9D%E5%A7%8B%E5%8C%96"
    },
    {
      "depth": 3,
      "text": "坐标系",
      "anchor": "坐标系",
      "citation": "https://www.pystone.net/notes/adb-airtest-poco-tips-tricks/#%E5%9D%90%E6%A0%87%E7%B3%BB"
    },
    {
      "depth": 3,
      "text": "常用API",
      "anchor": "常用api-1",
      "citation": "https://www.pystone.net/notes/adb-airtest-poco-tips-tricks/#%E5%B8%B8%E7%94%A8api-1"
    },
    {
      "depth": 4,
      "text": "操作UI对象(Object Proxy Related Operation)",
      "anchor": "操作ui对象object-proxy-related-operation",
      "citation": "https://www.pystone.net/notes/adb-airtest-poco-tips-tricks/#%E6%93%8D%E4%BD%9Cui%E5%AF%B9%E8%B1%A1object-proxy-related-operation"
    },
    {
      "depth": 4,
      "text": "全局操作(Global Operation)",
      "anchor": "全局操作global-operation",
      "citation": "https://www.pystone.net/notes/adb-airtest-poco-tips-tricks/#%E5%85%A8%E5%B1%80%E6%93%8D%E4%BD%9Cglobal-operation"
    },
    {
      "depth": 4,
      "text": "异常处理(Exceptions)",
      "anchor": "异常处理exceptions",
      "citation": "https://www.pystone.net/notes/adb-airtest-poco-tips-tricks/#%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86exceptions"
    },
    {
      "depth": 2,
      "text": "实践技巧（需求及处理方案汇总）",
      "anchor": "实践技巧需求及处理方案汇总",
      "citation": "https://www.pystone.net/notes/adb-airtest-poco-tips-tricks/#%E5%AE%9E%E8%B7%B5%E6%8A%80%E5%B7%A7%E9%9C%80%E6%B1%82%E5%8F%8A%E5%A4%84%E7%90%86%E6%96%B9%E6%A1%88%E6%B1%87%E6%80%BB"
    },
    {
      "depth": 2,
      "text": "遇到的坑",
      "anchor": "遇到的坑",
      "citation": "https://www.pystone.net/notes/adb-airtest-poco-tips-tricks/#%E9%81%87%E5%88%B0%E7%9A%84%E5%9D%91"
    },
    {
      "depth": 3,
      "text": "[Python]ModuleNotFoundError and ImportError",
      "anchor": "pythonmodulenotfounderror-and-importerror",
      "citation": "https://www.pystone.net/notes/adb-airtest-poco-tips-tricks/#pythonmodulenotfounderror-and-importerror"
    },
    {
      "depth": 2,
      "text": "Ref",
      "anchor": "ref",
      "citation": "https://www.pystone.net/notes/adb-airtest-poco-tips-tricks/#ref"
    }
  ],
  "claims": [],
  "outgoing": [],
  "incoming": [],
  "contentMarkdown": "# 【自动化测试】经验与技巧[adb&Aritest&Poco]\n\n﻿# 【自动化测试】经验与技巧[adb&Aritest&Poco]\n\n> 创建时间：2021/5/8 13:39\n\n* ADB技巧\n    * 连接安卓设备\n    * Profiler: Editor-to-Android connection\n    * log抓取\n    * 获取包名等\n    * 启动游戏\n  * Airtest\n    * 相关概念\n      * proxy\n      * 其他\n    * 常用API\n      * 基本\n      * Connect\n      * Simulate Input\n      * Make Assertion\n    * Running .air from CLI\n      * run automated case\n      * generate html report\n      * get case info\n    * 设备连接问题\n      * Some special parameters\n    * 相关网站\n    * 设备支持问题\n  * Poco\n    * SDK集成\n    * 术语\n    * 多设备连接与初始化\n    * 坐标系\n    * 常用API\n      * 操作UI对象(Object Proxy Related Operation)\n      * 全局操作(Global Operation)\n      * 异常处理(Exceptions)\n  * 实践技巧（需求及处理方案汇总）\n  * 遇到的坑\n    * [Python]ModuleNotFoundError and ImportError\n  * Ref\n\n## ADB技巧\n\n用法大全: https://github.com/mzlogin/awesome-adb\n\n### 连接安卓设备\n\nWIFI连接调试：\n\n```cpp\nstring s = String.Empty;\n\n```\n```cpp\nstring s = String.Empty;\n\n```\n```cpp\nstring s = String.Empty;\n\n```\n\n切换USB模式与WIFI模式\n\n```cpp\nstring s = String.Empty;\n\n```\n\n### Profiler: Editor-to-Android connection\n\nadb forward tcp:34999 localabstract:Unity-{insert bundle identifier here}\n\n### log抓取\n\nadb logcat -s Unity\nadb -s %DeviceName% logcat -s Unity\nadb logcat -s Unity ActivityManager PackageManager dalvikvm DEBUG #获取log，-s指定过滤器\nadb -s deviceName logcat -s Unity -f /mnt/sdcard/log.txt #-f 输出log到指定文件。只能输出到Android设备上。\nadb -s deviceName logcat -s Unity > c:\\unity_log.txt\n\n### 获取包名等\n\nshell命令：\n\n```cpp\nstring s = String.Empty;\n\n```\n\n### 启动游戏\n\nadb shell dumpsys package com.examle.xx\nadb shell am start -n 包名/包名.****\n\n## Airtest\n\n### 相关概念\n\n#### proxy\n\n代理服务器 (Proxy server) : In computer networking, a proxy server is a server application or appliance that **acts as an intermediary for requests from clients seeking resources from servers that provide those resources** 。\n\nProxy主要运作的行为在 OSI 七层协议的 **应用层** 部分。Proxy 能不能进行某些工作，与该服务的程序功能有关。\n\n#### 其他\n\n  * **网络主机（network host）** \\- 是已连接到一个计算机网络的一台电子计算机或其他设备。\n使用TCP/IP协议族参与网络的计算机也可称为IP主机。更具体来说，参与互联网计算机可称为互联网主机，有时还称作互联网节点。\n\n### 常用API\n\n#### 基本\n\n```cpp\nstring s = String.Empty;\n\n```\n\n#### Connect\n\n```cpp\nstring s = String.Empty;\n\n```\n\n![](assets/Image.png)\n\n![](assets/Image_1.png)\n\nconnect_device(\"Android://127.0.0.1:5037/10.254.60.1:5555\")\n\n  * adb host: adb server的地址（the IP of the host where adb Server is located） -\nBy default, this is localhostor 127.0.0.1\n\n  * adb port defaults to 5037\n\n  * devicehost:port: 移动设备的地址和端口号 （端口号：5555-5585）\n\n#### Simulate Input\n\ntouch\nswipe\ntext\nkeyevent\nsnapshot\nwait\n\n#### Make Assertion\n\nassert_exists\nassert_not_exists\nassert_equal\nassert_not_equal\n\n### Running .air from CLI\n\n#### run automated case\n\n> airtest run \"path to your .air dir\" \\--device Android:///\n>  airtest run \"path to your .air dir\" \\--device Android://adbhost:adbport/serialno\n>  airtest run \"path to your .air dir\" \\--device Windows:///?title_re=Unity.*\n>  airtest run \"path to your .air dir\" \\--device iOS:///\n\n```cpp\nstring s = String.Empty;\n\n```\n\n#### generate html report\n\n> airtest report \"path to your .air dir\"\n\n```cpp\nstring s = String.Empty;\n\n```\n\n#### get case info\n\n> python -m airtest info \"path to your .air dir\"\n\n```cpp\nstring s = String.Empty;\n\n```\n\n### 设备连接问题\n\n可以在调用airtest时连接设备，也可以在airtest脚本中进行设备的连接。\n\n> These two methods only need to choose one of them, basically can meet our needs to connect devices.\n\n调用时通过参数初始化连接:\n\n  * 在运行时的命令行中如果传入了类似`--device Android:///XXXX` 这样的设备参数，那么脚本在 **初始化时会自动连上对应的设备** ，不需要再另外写代码连接了。连接哪个设备，就会在哪个设备上执行脚本\n  * 如果不主动输入连接的设备，则会默认选择（随机性）一个设备来执行脚本。\n\nairtest脚本内通过API连接:\n\n  * 如果没有在初始化时连上设备，可以在脚本代码中使用`connect_device`接口来连接设备。\n  * Airtest支持在一个脚本里同时连接多个设备，使用`set_current`接口可以在多个设备中进行切换，`device()`接口可以获取到当前使用中的设备。\n\n虽然支持多线程多设备运行，但是一个airtest进程在同一时间只支持在一个设备上执行命令。可以调用时就设置连接的设备，也可以在脚本内进行连接或者断开，但`device()` 接口获取的当前设备只有一个。\n\n#### Some special parameters\n\nSome special devices may appear black screen when connected, such as some emulators, we can add an extra parameter cap_method=JAVACAP to force the screen capture mode to be JAVACAP :\n\n```cpp\nstring s = String.Empty;\n\n```\n\nIn addition, we have two other parameters, ori_method=ADBORI, which specifies the rotation mode of the device screen, and touch_method=ADBTOUCH, which specifies the click mode of the screen as ADB instruction.\n\n命令行传参数需要对一些字符进行转义。\nif any of the characters `^<>|&` appear on the command line, they may need to be escaped to take effect.\n\n```cpp\nstring s = String.Empty;\n\n```\n\n### 相关网站\n\n源码地址：https://github.com/AirtestProject/Airtest\n\nAndroid连接常见问题： https://airtest.doc.io.netease.com/IDEdocs/device_connection/2_android_faq/\n\n各个平台支持情况：https://airtest.readthedocs.io/zh_CN/latest/wiki/device/platforms.html\n\nAndroid平台的一些技巧： https://airtest.readthedocs.io/zh_CN/latest/wiki/device/android.html\n\n模拟器连接：https://airtest.doc.io.netease.com/en/IDEdocs/device_connection/3_emulator_connection/#2\n\nAirtest Project文档：https://airtest.doc.io.netease.com/\n\n### 设备支持问题\n\nhttps://airtest.doc.io.netease.com/IDEdocs/device_connection/6_android_support/\n\n## Poco\n\n### SDK集成\n\n场景中任意 GameObject 上 Add Component Unity3D/PocoManager.cs\n\n### 术语\n\nUI proxy: proxy objects within Poco framework, they represent zero (none), one or multiple in-game UI elements\nNode/UI element: UI element instances or nodes in app/game\n选择表达式 ：除非你需要扩展或自定义更底层的Poco的行为，一般无需关心此。选择表达式是一种框架和sdk交互的底层数据结构，用在 Selector 类中。\n\n### 多设备连接与初始化\n\nhttps://poco.readthedocs.io/zh_CN/latest/source/doc/drivers/unity3d.html\n\n### 坐标系\n\n归一化坐标系(Normalized Coordinate System)\nUI在poco中的宽和高其实就是相对于屏幕的百分比大小，好处就是不同分辨率设备之间，同一个UI的归一化坐标系下的位置和尺寸是一样的，有助于编写跨设备测试用例。屏幕正中央一定是(0.5, 0.5)\n\n局部坐标系(Local Coordinate System (local positioning))\n局部坐标系以UI包围盒左上角为原点，向右为x轴，向下为y轴，包围盒宽和高均为单位一。\n\n### 常用API\n\n```cpp\nstring s = String.Empty;\n\n```\n\nUI选择：\n\n  * 相对选择器(Relative Selector)\n  * 空间顺序选择器(Sequence Selector)\n\n#### 操作UI对象(Object Proxy Related Operation)\n\n点击(click)\n滑动(swipe)\n拖拽(drag) - darg 是从一个UI拖到另一个UI，而 swipe 是将一个UI朝某个方向拖动。\n\n局部定位(focus (local positioning)) - 所有UI相关的操作都默认以UI的 anchorPoint 为操作点，如果想自定义一个点那么可以使用 focus 方法。调用此方法将返回 新的 设置了默认 焦点 的UI\n\n```cpp\nstring s = String.Empty;\n\n```\n\n等待出现(wait)\nwait(5).exists()\nwait_for_appearance(timeout=120)\nwait_for_disappearance(timeout=120)\n\n#### 全局操作(Global Operation)\n\npoco.click([0.5, 0.5])\npoco.long_click([0.5, 0.5], duration=3)\npoco.swipe(point_a, center)\n\n截屏(snapshot)\n\n```cpp\nstring s = String.Empty;\n\n```\n\n#### 异常处理(Exceptions)\n\nPocoTargetTimeout\nPocoNoSuchNodeException\n\n## 实践技巧（需求及处理方案汇总）\n\nAndroid设备上的一些操作技巧可查看：\nhttps://airtest.readthedocs.io/zh_CN/latest/wiki/device/android.html\n\n涉及到的例子有：连续滑动、两个手指操作、录屏等\n\n代码功能示例：https://airtest.readthedocs.io/zh_CN/latest/wiki/code/code.html\n\n## 遇到的坑\n\n### [Python]ModuleNotFoundError and ImportError\n\nset PYTHONPATH=\"path to your .air dir\"\n\n## Ref\n\nhttps://www.zhihu.com/zvideo/1261234074455998464\n"
}
