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.
- // Get the handle of applications main window
- var window = UIATarget.localTarget().frontMostApp().mainWindow();
-
- // Get the handle of view
- var view = window.elements()[0];
-
- var textfields = window.textFields();
- var passwordfields = window.secureTextFields();
- var buttons = window.buttons();
- var textviews = window.textViews();
- var statictexts = window.staticTexts();
- var target = UIATarget.localTarget();
-
- // 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)");
- }
- // Check number of Secure field(s)
- if(passwordfields.length!=1)
- {
- UIALogger.logFail("FAIL: Inavlid number of Secure field(s)");
- }
- else
- {
- UIALogger.logPass("PASS: Correct number of Secure field(s)");
- }
-
- // Check number of static field(s)
- if(statictexts.length!=2)
- {
- UIALogger.logFail("FAIL: Inavlid number of static field(s)");
- }
- else
- {
- UIALogger.logPass("PASS: Correct number of static field(s)");
- }
- // Check number of buttons(s)
- if(buttons.length!=1)
- {
- UIALogger.logFail("FAIL: Inavlid number of button(s)");
- }
- else
- {
- UIALogger.logPass("PASS: Correct number of button(s)");
- }
- //TESTCASE_001 : Test Log on Screen
- //Check existence of desired TextField On UIScreen
- if(textfields["username"]==null || textfields["username"].toString() == "[object UIAElementNil]")
- {
- UIALogger.logFail("FAIL:Desired textfield not found.");
- }
- else
- {
- UIALogger.logPass("PASS: Desired UITextField is available");
- }
-
- //TESTCASE_1.2 :Check existence desired of PasswordField On UIScreen
- if(passwordfields[0]==null || passwordfields[0].toString() == "[object UIAElementNil]")
- {
- UIALogger.logFail("FAIL:Desired UISecureField not found.");
- }
- else
- {
- UIALogger.logPass("PASS: Desired UISecureField is available");
- }
-
- //TESTCASE_1.3 :Check For Existence of Buttons On UIScreen
- if(buttons["logon"]==null || buttons["logon"].toString() == "[object UIAElementNil]")
- {
- UIALogger.logFail("FAIL:Desired UIButton not found.");
- }
- else
- {
- UIALogger.logPass("PASS: Desired UIButton is available");
- }
- //TESTCASE_001 : Missing User Name
- ///////////////////////////////////////
- textfields["username"].setValue("");
- passwordfields[0].setValue("password");
- buttons["logon"].tap();
- //target.delay(2);
- var errorVal=textviews["error"].value();
- if(errorVal!="Invalid User Name or Password")
- {
- UIALogger.logFail("Did Not Get Missing UserName Error : "+errorVal);
- }
- else
- {
- UIALogger.logPass("Missing User Name");
- }
- //TESTCASE_002 : Missing Password
- ////////////////////////////////////////////////
- textfields["username"].setValue("username");
- passwordfields[0].setValue("");
- buttons["logon"].tap();
- target.delay(2);
-
- var errorVal=textviews["error"].value();
- if(errorVal!="Invalid User Name or Password")
- {
- UIALogger.logFail("Did Not Get Missing Password Error : "+errorVal);
- }
- else
- {
- UIALogger.logPass(" Missing Password");
- }
- //TESTCASE_003 : Successful Log On
- textfields["username"].setValue("username");
- passwordfields[0].setValue("password");
- buttons["logon"].tap();
- 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!