IOS 4中实现UI自动测试教程

移动开发 iOS
本文介绍的IOS 4中实现UI自动测试教程,主要介绍了IOS中UI的实现,我们来看内容。

IOS 4中实现UI自动测试教程是本文要介绍的内容,这篇文章的对象是 iOS 4 的初学者,我希望一个典型的iPhone 开发者能够通过这篇文章掌握自动设置UI测试的方法。

UI 自动测试iOS 4中重要的附加功能,它由名为“Automation”的新的工具对象支持,非常适合Producitivity风格应用的UI测试。

Automation工具从脚本工作(用JavaScript语言编写),它在应用上模仿/击发被请求的事件。测试脚本必须是可靠的可执行JavaScript文件,而且该文件可获取主机的工具。

什么是测试脚本?

测试脚本是有顺序的命令集合,每一个命令获取一个应用中的用户接口组件,然后在上面执行一个用户行为,或者使用与之相关的信息。

描述

我使用的应用例子有一个名为“登陆”的屏幕,包含两个文本域名:分别是“用户名”和“密码”,以及一个“登陆”按钮。

写测试脚本之前,在“Interface Builder”中标记所有的UI控制,在视图中将可存取性标签设置成一个唯一值(必须)。

在调试模式中编译你的应用

测试脚本:

正如我上面提到的,测试脚本基本上是有顺序的命令集合。换言之,它将文本测试的例子转换成可以被“Automation”工具自动执行的JavaScript

下面是测试脚本的例子

  1.  // Get the handle of applications main window  
  2. var window = UIATarget.localTarget().frontMostApp().mainWindow();   
  3.    
  4. // Get the handle of view  
  5. var view = window.elements()[0];   
  6.    
  7. var textfields = window.textFields();  
  8. var passwordfields = window.secureTextFields();  
  9. var buttons = window.buttons();  
  10. var textviews = window.textViews();  
  11. var statictexts = window.staticTexts();  
  12. var target = UIATarget.localTarget();   
  13.    
  14. // Check number of Text field(s)  
  15. if(textfields.length!=1)  
  16. {  
  17.    UIALogger.logFail("FAIL: Inavlid number of Text field(s)");  
  18. }  
  19. else  
  20. {  
  21.    UIALogger.logPass("PASS: Correct number of Text field(s)");  
  22. }  
  23. // Check number of Secure field(s)  
  24. if(passwordfields.length!=1)  
  25. {  
  26.    UIALogger.logFail("FAIL: Inavlid number of Secure field(s)");  
  27. }  
  28. else  
  29. {  
  30.    UIALogger.logPass("PASS: Correct number of Secure field(s)");  
  31. }   
  32.    
  33. // Check number of static field(s)  
  34. if(statictexts.length!=2)  
  35. {  
  36.    UIALogger.logFail("FAIL: Inavlid number of static field(s)");  
  37. }  
  38. else  
  39. {  
  40.    UIALogger.logPass("PASS: Correct number of static field(s)");  
  41. }   
  42. // Check number of buttons(s)  
  43. if(buttons.length!=1)  
  44. {  
  45.    UIALogger.logFail("FAIL: Inavlid number of button(s)");  
  46. }  
  47. else  
  48. {  
  49.    UIALogger.logPass("PASS: Correct number of button(s)");  
  50. }   
  51. //TESTCASE_001 : Test Log on Screen  
  52. //Check existence of desired TextField On UIScreen  
  53. if(textfields["username"]==null || textfields["username"].toString() == "[object UIAElementNil]")  
  54. {  
  55.    UIALogger.logFail("FAIL:Desired textfield not found.");  
  56. }  
  57. else  
  58. {  
  59.    UIALogger.logPass("PASS: Desired UITextField is available");  
  60. }   
  61.    
  62. //TESTCASE_1.2 :Check existence desired of PasswordField On UIScreen  
  63. if(passwordfields[0]==null || passwordfields[0].toString() == "[object UIAElementNil]")  
  64. {  
  65.    UIALogger.logFail("FAIL:Desired UISecureField not found.");  
  66. }  
  67. else  
  68. {  
  69.    UIALogger.logPass("PASS: Desired UISecureField is available");  
  70. }   
  71.    
  72. //TESTCASE_1.3 :Check For Existence of Buttons On UIScreen  
  73. if(buttons["logon"]==null || buttons["logon"].toString() == "[object UIAElementNil]")  
  74. {  
  75.    UIALogger.logFail("FAIL:Desired UIButton not found.");  
  76. }  
  77. else  
  78. {  
  79.    UIALogger.logPass("PASS: Desired UIButton is available");  
  80. }   
  81. //TESTCASE_001 : Missing User Name  
  82. ///////////////////////////////////////   
  83. textfields["username"].setValue("");  
  84. passwordfields[0].setValue("password");  
  85. buttons["logon"].tap();   
  86. //target.delay(2);   
  87. var errorVal=textviews["error"].value();  
  88. if(errorVal!="Invalid User Name or Password")  
  89. {  
  90.    UIALogger.logFail("Did Not Get Missing UserName Error : "+errorVal);  
  91. }  
  92. else  
  93. {  
  94.    UIALogger.logPass("Missing User Name");  
  95. }   
  96. //TESTCASE_002 : Missing Password  
  97. ////////////////////////////////////////////////   
  98. textfields["username"].setValue("username");  
  99. passwordfields[0].setValue("");  
  100. buttons["logon"].tap();  
  101. target.delay(2);   
  102.    
  103. var errorVal=textviews["error"].value();  
  104. if(errorVal!="Invalid User Name or Password")  
  105. {  
  106.    UIALogger.logFail("Did Not Get Missing Password Error : "+errorVal);  
  107. }  
  108. else  
  109. {  
  110.    UIALogger.logPass(" Missing Password");  
  111. }   
  112. //TESTCASE_003 : Successful Log On  
  113. textfields["username"].setValue("username");  
  114. passwordfields[0].setValue("password");  
  115. buttons["logon"].tap();  
  116. target.delay(2);  

应用中的所有UI组件通过有次序的对象层级结构传达给脚本,该层次结构通过UIAElements类定义,包括UIATarget、UIALogger等子类。

请参照苹果开发者网站的UI Automation Reference Collection获取更多信息。

完成脚本之后按下列步骤执行。

第一步

打开Instruments(你可以在Spotlight中找到它),在样本选择窗口中选择“Automation”

第二步

Trace窗口会打开,在“Choose Target”下拉窗口的帮助下选择应用的调试版本。

第三步

使用“Script”下拉窗口选择测试脚本文件,然后点击“Run and Record”按钮。

之后会自动运行“iPhone Simulator”,并开始执行测试脚本(可能花费4-5秒时间)。

可重用的测试脚本

API提供一个#import指令,你可以用它编写更小的、可重用的、分离的测试脚本。

例如:如果你准备在文件 TestUtilities.js中定义常用函数,你可以在脚本中加入命令行 #import”<path-to-library-folder>/TestUtilities.js”,从而使你的测试脚本能够使用这些函数。

测试样本代码的硬件要求

带有 Snow Leopard 10.6(或者更高版本)和iOS SDK 4.0 的Mac Mini(Intel处理器版本)

(1)解压附件“LoginWindow_src.zip”,在调试模式下编译它,并在模拟器Simulator上进行测试。

(2)“用户名/密码”是可靠证明;在相应的“User ID”和“Password”字段中填写值。

你遇到“Unexpected error…”了?
“Unexpected error in -[UIATarget_0x5a20d20 frontMostApp], /SourceCache/UIAutomation_Sim/UIAutomation-37/Framework/UIATargetElements.m line 437″

如果你遇到了这个错误,复制一个 com.apple.Accessibility.plist 到4.0.1就可以解决这个问题。

复制com.apple.Accessibility.plist 到~/Library/Application Support/iPhone Simulator/4.0.1/Library/Preferences

确保这个文件中仅有两个分别名为“AccessibilityEnabled”和“ApplicationAccessibilityEnabled”的键值,并且都经过效验。

小结:IOS 4中实现UI自动测试教程的内容介绍完了,希望本文对你有所帮助!

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-08-03 11:17:50

iOS程序 测试

2009-08-27 14:09:09

布线测试跳线测试串扰测试

2023-07-17 13:57:05

2023-03-17 16:32:51

测试软件开发

2019-02-15 15:07:39

AndroidiOS移动系统

2009-07-06 10:22:26

Web网站压力测试

2023-06-05 07:49:13

​左移测试敏捷

2011-08-30 11:09:26

MySQL ProxyLua

2019-11-26 17:44:16

AI 数据人工智能

2014-04-02 10:29:12

iOS 7模糊效果

2010-01-28 09:07:50

Visual Stud

2017-03-28 12:25:36

2023-03-30 16:50:18

2023-06-27 17:50:22

2021-06-26 07:40:21

前端自动化测试Jest

2016-12-08 08:25:39

QA自动测试Angular 2

2020-08-03 15:40:57

Web自动化工具测试

2009-12-23 16:33:34

WPF UI自动化测试

2021-10-18 12:01:17

iOS自动化测试Trip

2015-08-03 15:47:31

iosIOSUI优化
点赞
收藏

51CTO技术栈公众号