Xcode 使用Instrument(2)

UIAutomation

UIAutomation是随iOS4.0系统一起发布的一款旨在iPhone Device/Simulator上可执行的自动化测试框架。

使用UIAutomation准备

  • 被测试app源代码
  • 了解JavaScript

录制测试脚本

  • 录制测试脚本

    1. 使用Xcode打开被测试app工程
    2. command + i 打开Instrument
    3. 选择Automation template ->profile
    4. command + R 停止 profile
    5. 找到scripts 选择 add->create... 会看到一个新的脚本文件,下方有3个操作按钮
    6. 点击红色录制按钮,会启动APP,你在设备或是模拟器上进行操作都会生成操作代码 点击方形停止录制按钮录制完成,后Command + R停止
    7. 需要重新对录制脚本进行回放 可以点击三角按钮,或者直接C+R执行测试回放
  • 编写测试脚本:
    通过导入测试脚本和录制脚本,可以熟悉一些基本API
    接下来写一个测试脚本

    1. 打开被测试app工程

    2. Command + I 打开Instrument

    3. 选择Automation template ->profile

    4. Command + R 停止profile

    5. 在左边Scripts 选择add->create… 会看到一个新脚本文件生成 脚本可编辑区中进行脚本编辑如下图 4区域,在此区域进行测试用例的编写

    6. 测试用例demo 新建window上添加一个UITextField

    var target = UIATarget.localTarget();
    var app = target.frontMostApp();
    var window = app.mainWindow();
    var textFields = window.textFields();
    
    // Check number of Text field(s)
    if(textfields.length!=1)
    {
    	UIALogger.logFail("FAIL: Inavlid number of Text 		field(s)");
    }
    else
    {
    	UIALogger.logPass("PASS: Correct number of Text 		field(s)");
    }
    

Note

1. 在使用设备进行自动化测试有一点需要注意,在Release configuration 中选择使用Developer profile(不是Ad-Hoc Distrubution profile)。默认release 下profiling都已经完成。

2. 文档

UIAutomation JavaScript API 参考文档:
http://developer.apple.com/library/ios/#documentation/DeveloperTools/Reference/UIAutomationRef/_index.html

posted @ 2014-09-16 13:29  supermvn  阅读(1160)  评论(0编辑  收藏  举报