Xcode使用Instrument(1)

Xcode使用Instrument(1)

UIAutomation

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

使用UIAutomation准备

  1. 被测试app源代码
  2. 了解JavaScript

开始使用

  • 测试脚本的导入

    1. 使用Xcode打开被测试app工程
    2. command + i 打开Instrument
    3. 选择Automation template ->profile
    4. command + R 停止 profile
    5. 找到scripts 选择 add->import…
    6. 选择写好的测试脚本后会看到脚本list
    7. 在脚本list右边script可以看到脚本内容
    8. command + R执行脚本
    9. 可以看到脚本测试log

      附上一个显示遍历UI元素树的脚本,保存为本地的showUIAElementTree.js

      1
      DEMO_DELAY=2;
      var target = UIATarget.localTarget();
      var app = target.frontMostApp();
      
      //test functions
      
      function showCurrentViewInfo(){
      	UIALogger.logStart("show CurrentViewInfoTree");
      		try{
      			//show the target logElementTree
      			target.logElementTree();
      			UIALogger.logPass();
      		} catch(error){
      			UIALogger.logFail(error);
      		}
      	}
      
      //calling test functions
      showCurrentViewInfo();

  • 录制测试脚本
    录制测试脚本,是录制用户操作而生成的测试脚本,是没有断言操作的
    1. 打开被测试app工程
    2. command + i 打开Instrument
    3. 选择Automation template->profile
    4. command + R 停止profile
    5. 在scripts 选择add->create… 会看到一个新脚本文件 看到脚本代码的下法有3个操作按钮
    6. 点击红色录制按钮,会启动app,你在设备或是模拟器上进行操作都会生成代码,点击停止录制按钮录制完成,后command

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