IOS development: UI Tests exploration notes
What is UI Tests?
UI Tests is a test component that automatically Tests the UI and interacts with each other.
What is the use of UI Tests?
It can automatically click a button, view, or enter text by writing code, recording the developer's operation process and coding.
Importance of UI Tests
In the actual development process, as the project grows and the number of functions increases, it is very difficult to cover all test cases by manual operations, especially after the new functions are added, the old functions need to be re-tested, which leads to a lot of time to perform regression testing. A lot of repetitive work is generated here, some of the repetitive work can be completed automatically. At this time, the UI Tests can help solve this problem.
Usage
Step 1: add the UI Tests
If the project is a new project, you can directly select the option when creating the project, as shown in figure
If you have an existing project, you can add a UI Tests by adding a target. Click the xcode menu and find the target column.
Select Cocoa Touch UI Testing Bundle in the Test option
At this time, the test component is successfully added, as shown in figure
Step 2: Create a test code
Manually create test code
Open the test file and add the test code in the testExample () method.
If you do not know how to write the test code, you can refer to the automatically generated code style
Automatically generate test steps
Select the test file and click the recording button.
When you start the operation, it will record your operation steps and generate the test code
Is the test code automatically generated after some operations
At this time, you can analyze the syntax of the test code so that you can manually modify or manually test the code.
Start testing
Click the play button next to the testExample method to start the automatic test. At this time, you will see that the app is performing automatic operations.
The following describes the syntax of the test element.
XCUIApplication:
Inherits XCUIElement. This class is in charge of the application lifecycle and contains two main methods.
Launch ():
Start the program
Terminate ():
Terminate the program
XCUIElement:
Inherits NSObject and implements XCUIElementAttributes and XCUIElementTypeQueryProvider.
It can represent various UI elements of the system.
Exist:
This allows you to determine whether the current UI element exists. If you operate on a nonexistent element, the test component throws an exception and interrupts the test.
DescendantsMatchingType (type: XCUIElementType)-> XCUIElementQuery:
Take a certain type of element and its subclass set
ChildrenMatchingType (type: XCUIElementType)-> XCUIElementQuery:
Obtains a certain type of Element Set, excluding its subclass.
The difference between the two methods is that you can use childrenMatchingType only when using the system UIButton. If you want to query your own defined sub-buttons, you must use descendantsMatchingType.
In addition, there are some interaction methods for UI elements.
Tap (): Click
DoubleTap (): Double-click
PressForDuration (duration: NSTimeInterval)
SwipeUp ()
TypeText (text: String): used for textField and textView text input. before using the text box, make sure that the text box gets the input focus. You can use the tap () function to get the focus.
XCUIElementAttributes Protocol
It contains some attributes in UIAccessibility.
For example
You can easily view the features of the current element. The identifier attribute can be used to directly read the element. However, this attribute has a bug in UITextField, so it is unclear why
XCUIElementTypeQueryProvider Protocol
Contains the types of most UI controls in the system. You can obtain some types of UI sets by reading attributes.
Some attributes are as follows:
Create Demo
First create a login page
Click the login button for Logon verification. Click the clear button to clear the text.
After successful logon, you can go to the personal information page
The personal information page is as follows:
Click the modify button to modify personal information. Click the Message button to view personal messages.
Finally, the message interface
Test the logon page
Enter an incorrect account
Verification Result
Close warning window
Clear input records
Enter a correct account
Verification Result
Go to the personal information page
The test code is as follows:
Func testLoginView (){
Let app = XCUIApplication ()
// The UITextField id is incorrect. Therefore, you can only use the label method to traverse the elements and read them.
Let nameField = self. getFieldWithLbl ("nameField ")
If self. canOperateElement (nameField ){
NameField !. Tap ()
NameField !. TypeText ("xiaoming ")
}
Let psdField = self. getFieldWithLbl ("psdField ")
If self. canOperateElement (psdField ){
PsdField !. Tap ()
PsdField !. Typetext( "1234321 ")
}
// Read the corresponding button through the preset id of UIButton
Let loginBtn = app. buttons ["Login"]
If self. canOperateElement (loginBtn ){
LoginBtn. tap ()
}
// Start with a latency. Because the real login request is a network connection request, the result cannot be obtained directly. The demo simulates the network connection request through latency.
Let window = app. windows. elementAtIndex (0)
If self. canOperateElement (window ){
// The delay is 3 seconds. If the logon succeeds after 3 seconds, the information page is automatically displayed. If the logon fails, a warning window is displayed.
Window. pressForDuration (3)
}
// The alert id and labe cannot be used, and the estimation is still a bug. Therefore, it can only be determined by quantity.
If app. alerts. count> 0 {
// Logon Failed
App. alerts. collectionViews. buttons ["OK"]. tap ()
Let clear = app. buttons ["Clear"]
If self. canOperateElement (clear ){
Clear. tap ()
If self. canOperateElement (nameField ){
NameField !. Tap ()
NameField !. TypeText ("sun ")
}
If self. canOperateElement (psdField ){
PsdField !. Tap ()
PsdField !. Typetext( "111111 ")
}
If self. canOperateElement (loginBtn ){
LoginBtn. tap ()
}
If self. canOperateElement (window ){
// The delay is 3 seconds. If the logon succeeds after 3 seconds, the information page is automatically displayed. If the logon fails, a warning window is displayed.
Window. pressForDuration (3)
}
Self. loginSuccess ()
}
} Else {
// Login successful
Self. loginSuccess ()
}
}
Here are a few notes for special attention:
1. When your element does not exist, it may still return an element object, but it cannot be operated on at this time.
2. When the element you want to click is blocked by the keyboard or UIAlertView, the execution of the tap method throws an exception.
Detailed implementation can be referred to demo: https://github.com/sunGd/demo/tree/master/iOS9/UITestDemo
Personal Information Page Test
Modify gender
Modify age
Modify mood
Save changes
The test code is as follows:
Func testInfo (){
Let app = XCUIApplication ()
Let window = app. windows. elementAtIndex (0)
If self. canOperateElement (window ){
// The latency is 2 seconds. It takes time to load data.
Window. pressForDuration (2)
}
Let modifyBtn = app. buttons ["modify"];
ModifyBtn. tap ()
Let sexSwitch = app. switches ["sex"]
SexSwitch. tap ()
Let incrementButton = app. buttons ["Increment"]
IncrementButton. tap ()
IncrementButton. tap ()
IncrementButton. tap ()
App. buttons ["Decrement"]. tap ()
Let textView = app. textViews ["feeling"]
TextView. tap ()
App. keys ["Delete"]. tap ()
App. keys ["Delete"]. tap ()
TextView. typeText ("abc ")
// Click the blank area
Let clearBtn = app. buttons ["clearBtn"]
ClearBtn. tap ()
// Save data
ModifyBtn. tap ()
Window. pressForDuration (2)
Let messageBtn = app. buttons ["message"]
MessageBtn. tap ();
// Delay: 1 second. It takes time to push the view.
Window. pressForDuration (1)
Self. testMessage ()
}
Pay special attention to the following two points:
1. When textview obtains the focus, it cannot select the focus position.
2. The trigger position of the tap event is the center of the view. When the center of the view is blocked, use other views instead.
Personal Message Interface Test
Cell click
The test code is as follows:
Func testMessage (){
Let app = XCUIApplication ()
Let window = app. windows. elementAtIndex (0)
If self. canOperateElement (window ){
// The latency is 2 seconds. It takes time to load data.
Window. pressForDuration (2)
}
Let table = app. tables
Table. childrenMatchingType (. Cell). elementAtIndex (8). tap ()
Table. childrenMatchingType (. Cell). elementAtIndex (1). tap ()
}
Note the following:
1. The element pointer of tableView cannot be obtained temporarily.
Summary
In general, the UI Tests can only be used for testing some basic functions to verify whether the app functions can be used normally and whether there is a crash. However, it also has many shortcomings. The process of writing test cases is very cumbersome, The automatically generated code is almost impossible to run, the function is single, and many use cases cannot be covered, and there are many bugs, this greatly limits the application of the UI Tests in actual development. We hope that the official version can fix these problems and provide more functions.