iphone/ipad 自动化测试 Demo(XCode4.5.1)

来源:互联网 发布:太极越狱mac版 编辑:程序博客网 时间:2024/06/11 18:09

最近,在进行iOS程序的开发,后期需要进行一点自动测试,就查找了一些相关的资料,note here,希望对大家有帮助~~

iOS自动化脚本采用的是JS来编写的,为了节省时间,我采用的是首先录制屏幕操作,然后,再根据程序逻辑来更改代码。

JS教程:http://www.w3schools.com/js/js_howto.asp

一个非常好的XCode Automation 教程: http://blog.manbolo.com/2012/04/08/ios-automated-tests-with-uiautomation#3.4

Automation对象的reference:http://developer.apple.com/library/ios/#documentation/DeveloperTools/Reference/UIAutomationRef/_index.html

一、 编写自己的测试脚本

建议把上面所有的资料看一边,在具体的进行自己自动化测试。 

下面是我自己进行修改的JS测试脚本,仅供参考:

[javascript] view plaincopy
  1. var target = UIATarget.localTarget();  
  2.   
  3. UIATarget.onAlert = function onAlert(alert) {  
  4.     var title = alert.name();  
  5.     UIALogger.logMessage("Alert View " + title + " encountered!");  
  6.     if(title == "请输入") {  
  7.         UIALogger.logMessage("请输入");  
  8.         target.frontMostApp().alert().textFields()[0].tap();  
  9.         target.frontMostApp().keyboard().typeString("300");  
  10.         return false;  
  11.     }  
  12.     return false;  
  13. }  
  14.   
  15. target.frontMostApp().mainWindow().textFields()[0].tap();  
  16. target.frontMostApp().keyboard().keys()["Delete"].tapWithOptions({tapCount:5});  
  17. target.frontMostApp().keyboard().typeString("xxxx");  
  18. target.frontMostApp().mainWindow().secureTextFields()[0].tap();  
  19. target.frontMostApp().keyboard().typeString("1234");  
  20. target.frontMostApp().navigationBar().rightButton().tap();  
  21. while(target.frontMostApp().navigationBar().name() == "登录") {  
  22.     UIALogger.logMessage("LoginView: " + target.frontMostApp().navigationBar().name());  
  23.     target.delay(1);  //此处参看http://stackoverflow.com/questions/3651316/handling-alert-with-uiautomation  
  24. }  
  25.   
  26. UIALogger.logMessage(target.frontMostApp().navigationBar().name());  
  27. if(target.frontMostApp().navigationBar().name() == "授权") {  
  28.     target.frontMostApp().mainWindow().textFields()[0].tap();  
  29.     target.frontMostApp().keyboard().typeString("092888");  
  30.     target.frontMostApp().navigationBar().rightButton().tap();  
  31. }  
  32. while(target.frontMostApp().navigationBar().name() == "授权") {  
  33.     UIALogger.logMessage("AuthoView: " + target.frontMostApp().navigationBar().name());  
  34.     target.delay(1);  
  35. }  
  36. var roundNum = 100;  
  37. UIALogger.logMessage(target.frontMostApp().navigationBar().name());  
  38. if(target.frontMostApp().navigationBar().name() == "主页") {  
  39.     while(roundNum-- > 0){  
  40.         target.frontMostApp().navigationBar().leftButton().tap();  
  41.         // Alert detected. Expressions for handling alerts should be moved into the UIATarget.onAlert function definition.  
  42.         target.delay(2);  
  43.         var boxNum = 30;  
  44.         if(target.frontMostApp().navigationBar().name() != "主页") {  
  45.             while(boxNum-- > 0) {  
  46.                 UIALogger.logMessage(target.frontMostApp().navigationBar().name());  
  47.                 target.frontMostApp().mainWindow().buttons().firstWithName("1").tap();  
  48.                 target.frontMostApp().mainWindow().buttons().firstWithName("0").tap();  
  49.                 target.frontMostApp().mainWindow().buttons()["x"].tap();  
  50.                 target.frontMostApp().mainWindow().buttons()["xx"].tap();  
  51.                 target.frontMostApp().mainWindow().buttons()["xx"].tap();  
  52.                 target.frontMostApp().mainWindow().buttons()["xx"].tap();  
  53.                 target.frontMostApp().mainWindow().buttons()["xx"].tap();  
  54.                 target.frontMostApp().mainWindow().buttons().firstWithName("8").tap();  
  55.                 target.frontMostApp().mainWindow().buttons()["xxx"].tap();  
  56.                 target.frontMostApp().mainWindow().buttons()["确定"].tap();  
  57.             }  
  58.             target.frontMostApp().navigationBar().segmentedControls()[0].buttons()["stop_normal.png"].tap();  
  59.             // Alert detected. Expressions for handling alerts should be moved into the UIATarget.onAlert function definition.  
  60.             target.delay(2);  
  61.         }  
  62.     }  
  63. }  

二、在命令行运行测试脚本

如果要使用命令行来运行测试脚本, 上面给出的XCode Automation 里面的命令是对的,但是对于Xcode4.5.1 还需要进行相应的修改。
因为自动化测试的模板和Xcode.app的位置都与之前的不同,需要分别进行确定:
      首先,定位Automation.tracetemplate, 使用命令:
           locate Automation.tracetemplate 或者 find / -type f -name "Automation.tracetemplate"
      然后,执行:
           sudo xcode-select -switch /Applications/Xcode.app (此处也可先找到Xcode.app的路径,方法同上)
最终,在真机上面测试的命令如下:
           instruments -w ****************** -t /Applications/Xcode.app/Contents/Applications/Instruments.app/Contents/PlugIns/AutomationInstrument.bundle/Contents/Resources/Automation.tracetemplate TestApp -e UIASCRIPT /gtliuData/Instruments/iOSAutoTestJS/SecondTest.js