Automated UI testing in IOS 4

Source: Internet
Author: User

IOS 4ImplementationAutomatic UI TestThe tutorial is the content to be introduced in this article. The object of this article isIOS 4For beginners, I hope a typical iPhone developer can use this article to master automatic settings.UITest method.

Automatic UI TestYesIOS 4It is supported by the new tool object named "Automation" and is very suitable for UI testing of Producitivity vity style applications.

The Automation tool is written in JavaScript from the script. It imitates/sends the requested event on the application. The test script must be a reliable and executable JavaScript file that can be used to obtain the host tool.

What is a test script?

A test script is a set of ordered commands. Each Command obtains a user interface component in an application, and then executes a user action on it, or uses relevant information.

Description

In my application example, a screen named "login" contains two text domain names: "User Name" and "password", and a "login" button.

Before writing the test script, mark all UI controls in "Interface Builder" and set the Accessability tag to a unique value in the view ).

Compile your application in debug mode

Test script:

As I mentioned above, test scripts are basically a set of ordered commands. In other words, it converts the text test example to JavaScript that can be automatically executed by the Automation tool.

The following is an example of a test script.

 
 
  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);  

All UI components in the application are passed to the script through an ordered object hierarchy, which is defined by the UIAElements class, including the UIATarget and UIALogger sub-classes.

For more information, see UI Automation Reference Collection on the Apple developer website.

After the script is completed, follow these steps.

Step 1

Open Instruments and you can find it in Spotlight.) in the sample selection window, select "Automation"

Step 2

The Trace window opens. Select the debug version of the application with the help of the "Choose Target" drop-down window.

Step 3

Use the "Script" drop-down window to select a test Script file, and then click "Run and Record.

It will automatically run the "iPhone Simulator" and start to execute the test script may take 4-5 seconds ).

Reusable test scripts

The API provides a # import command. You can use it to write smaller, reusable, and separated test scripts.

For example, if you are preparing to file TestUtilities. js defines common functions. You can add the command line # import "<path-to-library-folder>/TestUtilities to the script. js ", so that your test script can use these functions.

Hardware requirements for testing sample code

With Snow Leopard 10.6 or later) and Mac MiniIntel processor version of iOS SDK 4.0)

1. decompress the attachment “loginwindow_src.zip ", compile it in debug mode, and test it on the Simulator.

2) "User Name/Password" is a reliable proof; enter the value in the corresponding "User ID" and "Password" fields.

You encountered "Unexpected error ..." ?
"Unexpected error in-[UIATarget_0x5a20d20 frontMostApp],/SourceCache/UIAutomation_Sim/UIAutomation-37/Framework/UIATargetElements. m line 437 ″

If you encounter this error, copy com. apple. Accessibility. plist to 4.0.1 to solve this problem.

Copy com. apple. Accessibility. plist ~ /Library/Application Support/iPhone Simulator/4.0.1/Library/Preferences

Make sure that only two key values named "AccessibilityEnabled" and "ApplicationAccessibilityEnabled" are verified.

Summary:IOS 4ImplementationAutomatic UI TestI hope this article will help you with the introduction of this tutorial!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.