【质量平台】浅尝UI自动化之Airtest实践

{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"一、背景","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#494949","name":"user"}}],"text":"由于很多公司都采用敏捷开发的模式,测试也要跟着进行敏捷测试。而每个迭代的周期非常短,经常要对原有功能进行回归测试,这样就增加了大量重复人力成本。引入UI自动化测试可以用来快速回归测试app原有功能,测试人员只需要关注新功能的测试。其次,移动端App的测试用例大部分是功能验证相关的用例,通过UI操作即可验证,这就为UI自动化提供了便利条件。因此,可以将测试重复度高,执行效率低的用例,通过UI自动化快速重复执行,达到提高测试效率的目的。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"二、Airtest简介","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"目前主流的UI自动话测试工具主要有:Selenium, Appium和Airtest。其中Selenium是一款开源的Web应用自动化测试工具,它可以直接运行在多种浏览器平台,其支持的浏览器几乎涵盖了所有主流的浏览器,但是由于得物是APP,不需要web端的测试,故这个工具被残忍舍弃。而Appium和AirTest都是针对APP的自动化测试工具,都可以进行自动话测试脚本的录制和回放。但是之所以选择了AirTest最主要的原因是他很容易生成测试脚本,即使测试人员不会编程,不懂脚本,也可以通过正常用户的点击拖拽等操作,自动完成脚本的录制,从而大幅度降低自动化维护成本。本人经过亲身实践,从UI自动化小白到第一个自动化测试脚本成功运行,用了半天时间,感兴趣的同学可以尝试一下。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"   AirtestIDE 是一个跨平台的UI自动化测试编辑器,适用于游戏和App。它的特点如下:","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"自动化脚本录制、一键回放、报告查看,轻而易举实现自动化测试流程支持","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"基于图像识别的 Airtest 框架,适用于所有Android和Windows游戏支持","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"基于UI控件搜索的 Poco 框架,适用于Unity3d,Cocos2d与Android App","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"能够运行在Windows和MacOS上。","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"架构图","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/f4/f491f1c7b8568925e3e8312976b4a4b3.jpeg","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可以看到,底层的主要测试框架是AirTest和Poco,二者区别在于:","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"AirTest:基于Python的、跨平台的UI自动化测试框架,基于图像识别原理,适用于游戏和APP。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Poco:基于UI控件搜索的自动化测试框架,其核心优势是除了对Android 和IOS之外,对游戏也是支持的,同时也支持微信小程序、微信小游戏和H5应用。","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"三、Airtset安装和连接设备","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"    目前AirTestIDE 提供了Windows和Mac两个版本的客户端,请从","attrs":{}},{"type":"link","attrs":{"href":"http://airtest.netease.com/changelog.html","title":null,"type":null},"content":[{"type":"text","marks":[{"type":"underline","attrs":{}}],"text":"官网","attrs":{}}]},{"type":"text","text":"下载,解压即用。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"    无论是Android/IOS手机,还是Windows窗口,在AirTest中都将它视为一个设备。下边重点介绍一下Android设备的连接。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"连接Android手机","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"通过ADB连接电脑和Android手机,ADB是Google官方提供的Android调试工具,AirTestIDE依赖","attrs":{}},{"type":"link","attrs":{"href":"https://developer.android.com/studio/command-line/adb.html?hl=zh-cn","title":null,"type":null},"content":[{"type":"text","marks":[{"type":"underline","attrs":{}}],"text":"ADB","attrs":{}}]},{"type":"text","text":"与安卓设备进行通信。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"打开AirTestIDE,按照以下步骤进行连接:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"1.打开手机设置-开发者选项-USB调试开关,参考","attrs":{}},{"type":"link","attrs":{"href":"https://developer.android.com/studio/debug/dev-options.html#debugging","title":null,"type":null},"content":[{"type":"text","marks":[{"type":"underline","attrs":{}}],"text":"安卓官方文档","attrs":{}}]},{"type":"text","marks":[{"type":"color","attrs":{"color":"#393939","name":"user"}}],"text":";","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#393939","name":"user"}}],"text":"2.在AirTestIDE设备面板中点击 refresh ADB 按钮,查看连接上的设备;","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#393939","name":"user"}}],"text":"3.如果没有显示出设备,试试restart ADB,如果还不行,参考","attrs":{}},{"type":"link","attrs":{"href":"https://airtest.doc.io.netease.com/","title":null,"type":null},"content":[{"type":"text","marks":[{"type":"underline","attrs":{}}],"text":"FAQ文档","attrs":{}}]},{"type":"text","marks":[{"type":"color","attrs":{"color":"#393939","name":"user"}}],"text":"进行问题排查。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#393939","name":"user"}}],"text":"4.能够成功看到设备后,点击对应设备的connect 按钮,进行初始化。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#393939","name":"user"}}],"text":"连接成功后,即可在AirTestIDE中看到手机屏幕的镜像显示,并进行实时操作,如下图所示:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/32/32f0494946a16e2c64b1c8126517fc28.png","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"连接IOS手机","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"要连接一台IOS手机,你需要准备好一台安装了Xcode的MAC电脑,连接方式参考","attrs":{}},{"type":"link","attrs":{"href":"https://airtest.doc.io.netease.com/","title":null,"type":null},"content":[{"type":"text","marks":[{"type":"underline","attrs":{}}],"text":"文档","attrs":{}}]},{"type":"text","marks":[{"type":"color","attrs":{"color":"#393939","name":"user"}}],"text":"。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"四、录制自动化脚本","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在连接上设备以后,就可以开始录制自动化测试脚本了,在接下来的内容中,我将会在一台Android手机上,给大家演示如何录制脚本。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"模拟输入","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"AirTest支持通过图像识别的方式,找到你想要的位置并进行操作,这是基于AirTest框架实现的。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我们可以先看看如何自动录制脚本:点击AirTestIDE左侧的AirTest辅助窗上的“录制”按钮,然后随着你在设备窗口上操作手机,代码会自动生成在代码窗口中。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"录制完成后,点击“运行”,就可以运行你的第一个自动化脚本了。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/1a/1afa537f1ba02d0df622a7a60fa0d0c1.jpeg","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你觉得自动录制生成的图标不够准确,还可以点击AirTest辅助窗上的 touch 按钮,然后再设备窗口上框选精确的图标,也可以制动生成一条touch语句。如下:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"https://v.qq.com/txp/iframe/player.html?vid=e3252k79dir","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"类似的模拟输入操作还有滑动:点击swipe按钮,在设备窗口上框选精确的图标最为滑动起点,然后点击滑动终点位置,即会自动生成一个swipe语句。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其他的模拟输入的API包括:","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Text:文字输入","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"KeyEvent:按键输入,包括(HOME/BACK/MENU等)","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Sleep:等待","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Snapshot:截屏","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"断言","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"到这里,我们已经有各种模拟输入方法,配合逻辑控制语句让手机动起来。自动化测试中还有很重要的一个步骤:结果验证。我们就可以来看看怎样声明断言。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"(1)验证UI界面","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"录制方法与模拟输入类似。","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"assert_exists:断言图片存在","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"assert_not_exists:断言图片不存在","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"(2)验证数值","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"通过Poco获取属性值,手写代码进行断言。","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"assert_equal:断言箱单","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"assert_not_equal:断言不等","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"例如","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/e7/e782b9b71f248099e86806a5ad97cb25.jpeg","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"查看测试报告","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"脚本运行完毕后,点击“查看报告”那妞,会使用默认浏览器打开结果报告页面,报告中将展示每一个步骤的内容和实际执行过程的截图、运行结果,方便查看步骤是否执行成功。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/cc/cce496110e0cf1e82ad229c72ec01a32.png","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/e9/e9981d03d96b577e5bc6e3c86192cfeb.jpeg","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"五、AirTest脚本介绍","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"AirTest是一款基于Python的,跨平台的UI自动化测试框架,基于图像识别原理,适用于游戏和APP。虽然可以借助IDE提供的录制功能快速的生成脚本,但是通常来说,熟练掌握Python语法能够帮助我们写出应用更广泛、更不容易出错的脚本。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"一个简单的.air脚本解析","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在下载解压Airtest脚本的专属IDE——AirtestIDE后,点击“新建脚本”按钮,默认即可创建一个后缀名为.air的脚本文件,.air这是Airtest脚本的专属后缀。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"让我们打开刚才新建脚本的文件夹,可以看到实际上.air脚本文件是一个普通的文件夹,里面附带了一个同名的.py文件,AirtestIDE在执行脚本时,实际上执行的是里面的.py文件。也就是说,Airtest脚本本质上仍是Python脚本,遵循的是Python语法,我们可以根据实际需要自由地import其它Python第三方库。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"值得注意的是,.air文件夹中必须要有同名的.py文件,否则在命令行执行airtest run test.air这样的运行指令时会导致失败。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"AirTest脚本示例","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/e9/e901d86635b46b8efc1b4825bd4cf4c4.jpeg","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"初始化环境","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先,就像一个普通的Python脚本一样,我们需要在代码文件的最开头部分,写上from airtest.core.api import \\*,将AirTest 的主要API都import进来,以便在后续脚本中使用这些API。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"auto_setup是一用来初始化环境的接口,接口文档在","attrs":{}},{"type":"link","attrs":{"href":"https://airtest.readthedocs.io/zh_CN/latest/all_module/airtest.core.api.html#airtest.core.api.auto_setup","title":null,"type":null},"content":[{"type":"text","marks":[{"type":"underline","attrs":{}}],"text":"这里","attrs":{}}]},{"type":"text","marks":[{"type":"color","attrs":{"color":"#393939","name":"user"}}],"text":",它接受4个参数,我们可以设置当前脚本所在的路径、指定运行脚本的设备、设置默认的log路径和设置脚本父路径。","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#393939","name":"user"}}],"text":"如果auto_setup不传入任何参数,AirTest将会读取运行时命令行中传入的各项参数,来对环境进行初始化。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#393939","name":"user"}}],"text":"在AirTestIDE创建脚本时,默认生成的代码里是最简单的初始化代码auto_setup(__file__),意思是将脚本文件作为脚本路径传入,其他参数内容将默认读取运行命令行传入的参数。","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#393939","name":"user"}}],"text":"脚本运行命令行有两种形式,命令行中的参数包含device,log等:","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#393939","name":"user"}}],"text":"命令行运行AirTest脚本的示例:>airtest run untitled.air --device Android:///手机设备号 --log log。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#393939","name":"user"}}],"text":"在使用AirTestIDE运行脚本时,会在\"Log 查看窗\"中自动生成一个可用的命令行,可以供大家作为参考。","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"模拟点击","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Airtest作为自动化测试框架,模拟的是人的操作,常见的接口主要有:","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Touch点击某个位置,可以设定被点击的位置、次数、按住时长参数","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Swipe从一个位置华东到另一个位置","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Text调用输入法输入指定内容","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"KeyEvent输入某个按键响应,礼盒回车键、删除键","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Wait等待某个指定的图片元素出现","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Snapshot对当前画面截一张图","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其他","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"核心API请参见","attrs":{}},{"type":"link","attrs":{"href":"https://airtest.readthedocs.io/zh_CN/latest/all_module/airtest.core.api.html","title":null,"type":null},"content":[{"type":"text","marks":[{"type":"underline","attrs":{}}],"text":"文档","attrs":{}}]},{"type":"text","marks":[{"type":"color","attrs":{"color":"#393939","name":"user"}}],"text":",在这个文档页里出现的API都是","attrs":{}},{"type":"text","text":"跨平台API,由于我们在代码的第一行里将airtest.core.api里的接口全部import进来了,因此这些API可以在代码里直接进行调用,像这样:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/c3/c3be1348a112222406471f8a0dc40ab1.jpeg","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在很多接口中,支持传入Template图片对象作为参数,在运行时将会去点击图片在画面中的所在位置,类似这样:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/b8/b87fba9bc81d264b2415dddd9c90ac53.jpeg","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其中,Template对象是一个图片类,AirTest会先尝试在当前画面中找到匹配这张图片的位置,如果找到了,将对这个座标进行点击操作,如果找不到,就抛出识别异常。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"断言语句","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"断言在单元测试代码中非常重要,因此建议在我们的脚本里使用断言语句来判定被测应用当前的涨停是否是我们预期中的状态。Airtest提供了assert_exists和assert_not_exists两个接口,来断言一张图片存在或者不存在于当前画面中。同时,还提供了assert_equal和assert_not_equal两个语句,来断言传入的两个值相等或者不相等。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"如何在Python脚本中使用AirTest","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"AirTestIDE在创建新脚本时,也能够直接创建一个.py脚本文件,但是在创建之前会弹出一个设置窗口,要求填写一些指定的参数。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在我们了解过auto_setup接口后就会知道,这些参数就是为了传给它,然后初始化AirTest运行环境使用的。因此,一个纯.py脚本的初始化代码可以是这样的:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/97/9767a03c0d4689235d1bf75df77d5cfb.jpeg","alt":null,"title":"","style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上边这段代码的意思是说,当使用python xxx.py来运行本文件,且不带任何命令行参数是,则自动使用auto_setup这个接口来对AirTest相关的参数进行初始化。这样只需要在写py脚本时,填好指定的参数就能直接用python xxx.py指令来运行脚本。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"同时,原先传统的airtest run xxx.air --devices Android: /// 命令行方式也不受影响,只要脚本检测到传入了命令行参数(即代码中的 if not cli_setup()判断),就依然优先使用命令行参数来初始化AirTest环境。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"当然,熟练掌握API的各位,也可以根据实际需求在自己的Python脚本中调用AirTest API,与使用正常的pyhton第三方库方法相同。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"六、总结","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"本文只是简单讲解了如何使用AirTest进行UI自动化脚本的录制和回放,以及对自动化脚本进行了介绍。如果要进行UI自动化的持续集成,还需要进一步的了解和学习。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"希望有时间的同学可以进行一些实践,了解一下UI自动话相关的知识。在以后的工作中,我们就","attrs":{}},{"type":"text","marks":[{"type":"color","attrs":{"color":"#494949","name":"user"}}],"text":"可以将测试重复度高,执行效率低的用例,通过UI自动化快速重复执行,以达到提高测试效率的目的。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"color","attrs":{"color":"#393939","name":"user"}}],"text":"此外,本文只是介绍了通过AirTest的框架进行图像识别,编写测试脚本,但是在某些特殊情况下,例如对于游戏或者APP里的动态元素,通过图像识别比较困难。AirTest还提供了另外一种基于UI控件搜索的自动化框架Poco,同学们可以在下边的文章中,进行学习和实践。","attrs":{}},{"type":"text","text":"详情请查看:","attrs":{}},{"type":"link","attrs":{"href":"https://edu.uwa4d.com/lesson-detail/124/470/0?isPreview=0","title":null,"type":null},"content":[{"type":"text","marks":[{"type":"underline","attrs":{}}],"text":"https://edu.uwa4d.com/lesson-detail/124/470/0?isPreview=0","attrs":{}}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"文|Lynne","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"关注得物技术,携手走向技术的云端","attrs":{}}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章