导语


由于已经熟悉appium测试iOS流程,了解appium运行原理。
Appium使用WebDriver的json wire协议,封装了instruments内uiautomator库,启动appium服务以后,会在电脑端有一个监听端口,将主机内代码转化成移动端识别的命令,然后在移动端执行完以后,将结果反馈给主机,主机收集反馈的结果,形成一定形式的报告。
既然appium封装的是Uiautomation库,那么直接用Uiautomation进行测试呢?正好使用appium测试公司app过程中遇到了一个无法绕过的问题,决定使用Uiautomaton做UI自动化功能测试。


一、Instruments

Uiautomation是Instruments的一小部分功能。
Instruments Uiautomaion
Uiautomation脚本编辑可以通过录制和编辑两种方式,Uiautomation录制的脚本比较难以阅读和维护,我一直坚持自己写自动化测试脚本,这样更容易维护。

二、准备工作

  1. mac,Developer签名的应用程序,真机或者模拟器。
  2. 打开instrumens Uiautomation,首先录制一份简单的操作脚本,确定能否启动移动端的app。
    可能出现的问题:
    点击Uiautomation以后录制键以后,app启动不起来?
    解决方法:
    在手机端打开开发者模式,打开enable ui automation

三、开始编辑脚本

  1. 点击录制键启动app以后,到需要操作的app界面,然后输入

    1
    2
    3
    4
    var target = UIATarget.localTarget();
    var app = target.frontMostApp();
    var window = app.mainWindow();
    target.logElementTree();
  2. 在Trace log界面会打印出类似于xpath形式的,app界面的树形结构,根据元素层级定位的原则,定位页面的功能点。

  3. 最后我编辑的脚本类似于
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    var target = UIATarget.localTarget();
    target.delay(10);
    target.frontMostApp().tabBar().buttons()["发现"].tap();
    target.frontMostApp().tabBar().buttons()["智玩"].tap();
    target.frontMostApp().tabBar().buttons()["我的"].tap();
    target.frontMostApp().mainWindow().tableViews()[0]
    .buttons()[2].tap();
    target.frontMostApp().mainWindow().tableViews()[0]
    .groups()[0].scrollToVisible();
    target.frontMostApp().mainWindow().tap();
    target.frontMostApp().mainWindow().tableViews()[0]
    .groups()[2].tap();
    target.frontMostApp().tabBar().buttons()["发现"].tap();

四、使用Tuneup_Js框架

  1. Tunup_Js源代码地址为:https://github.com/alexvollmer/tuneup_js
  2. 需要熟悉Tuneup_Js的使用规则
    (1). 必须在文件中加入Tuneup_Js的import:
    #import 'tuneup/tuneup.js'
    (2). 测试方法需要使用以下的方式定义:

    1
    2
    3
    test('测试方法的简要说明',function(){
    //具体的测试行为加上结果判断的断言
    });
  3. 命令行执行方式

    1
    2
    3
    /ios_testing/tuneup_js/test_runner/run (bundle id)
    /ios_testing/tuneup_js/cases/core.js
    /ios_testing/tuneup_js/out/ -d (udid) -x test.xml
  4. 生成xml以后,测试报告不是很直观,用python解析并生成Html测试报告。
    Html结果报告

  5. 放入笔者用python封装生成测试报告的框架。Python封装Tuenup_Js框架

五、后续

思考:生成了Html报告,但是没有客户端性能方面的数据,如何获取客户端性能数据?
猜想:Uiautomation在录制脚本过程中,可以查看iOS内存泄露、网络连接和CPU内存等使用一系列数据。而且在执行Tuneup_Js结果OUT下有instruments.trace文件,应该可以解析出客户端性能方面的数据,需要研究并且实践。

To be continued…..