iOS Development: UI Tests Exploration notes

Source: Internet
Author: User
Tags new features

What is the UI tests?

UI tests is an testing component that automatically tests UI and interaction

What's the use of UI tests?

It can be done by writing code, or recording the process of the developer and code, to automatically click on a button, view, or automatically enter text and other functions.

The importance of UI tests

In the actual development process, as the project becomes larger and more functional, it is very difficult to overwrite all test cases manually, especially when the new features are added, and the old ones are tested again, which results in a very large amount of time for the test to be tested. This creates a lot of repetitive work, and some of these repetitive tasks can be done automatically, and UI tests can help solve the problem.

How to use

First step: Add UI Tests

If it is a new project, you can check the options directly when you create the project, as shown below

If you have an existing project, you can add a UI Tests by adding target, click on the Xcode menu, and find the target bar

In the test option, select Cocoa Touch UI testing Bundle

At this point the test component is added successfully and its position in the project is shown in the following illustration

Step Two: Create the test code

To create a test code manually

Open the test file and add the test code in the Testexample () method

If you don't know how to write your test code, you can refer to the Auto-generated code style

Automatically generate test steps

After selecting the test file, click the Record button

This is where you start the operation, and it records your steps and generates the test code

The following diagram is the test code that is automatically generated after some operations

This is the time to analyze the syntax of your test code so that you can manually modify it or write your own test code

Start testing

Click the Play button next to the Testexample method and it starts to test automatically, and you will see the app in the automatic

Here's a description of the syntax of the test element

Xcuiapplication:

Inheriting xcuielement, which governs the lifecycle of the application, contains two main methods

Launch ():

Start Program

Terminate ():

Terminate program

Xcuielement:

Inherit NSObject, implement protocol xcuielementattributes, Xcuielementtypequeryprovider

Can represent various UI elements of the system

Exist

Allows you to determine whether the current UI element exists, and if an element that does not exist will cause the test component to throw an exception and break the test

Descendantsmatchingtype (Type:xcuielementtype)->xcuielementquery:

Takes a type of element and a collection of its subclasses

Childrenmatchingtype (Type:xcuielementtype)->xcuielementquery:

Takes a collection of elements of a type that does not contain its subclasses

The difference between these two methods is that when you use only the UIButton of the system, you can use the Childrenmatchingtype, and if you want to query your own definition of the button, you need to use Descendantsmatchingtype

Other UI elements have some interaction methods

Tap (): click

Doubletap (): Double-click

Pressforduration (Duration:nstimeinterval): For a long time, when you need to delay operation, this will be useful.

Swipeup (): This does not respond to pan gestures, temporarily did not find where it can be used, but also may be a beta version of the bug, do not explain

TypeText (text:string): Used for TextField and TextView input text, make sure the text box gets the input focus before using it, you can use the tap () function to get the focus

Xcuielementattributes protocol

Contains some of the attributes in the uiaccessibility.

The following figure

You can easily view the characteristics of the current element, where the identifier property can be used to read the element directly, but the attribute has a bug in the Uitextfield and is temporarily unclear why

Xcuielementtypequeryprovider protocol

It contains the types of most UI controls in the system, and you can get some kind of UI collection by reading the properties

Some of the properties screenshot below

Create Demo

Create a login page first

Click the login button for login verification, and clicking the Clear button clears the text

Login successful can go to the Personal information page

The Personal Information page is as follows

Click the Modify button to modify your personal information and click the Message button to view your personal messages

Finally, the message interface

Test for login Page

Enter a wrong account

Validation results

Close Warning window

Clear input record

Enter a correct account

Validation results

Go to Personal information page

The test code is as follows:

Func Testloginview () {

Let app = Xcuiapplication ()

Because the ID of the Uitextfield is problematic, you can only traverse the element to read it by means of a label

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

}

To read the corresponding button by UIButton's preset ID

Let loginbtn = app.buttons["Login"]

If Self.canoperateelement (loginbtn) {

Loginbtn.tap ()

}

Start a period of delay, because the real login is the network request, so can not directly obtain results, demo through the way of delay to simulate the network request

let window = App.windows.elementAtIndex (0)

If self.canoperateelement (window) {

3 seconds delay, 3 seconds, if the login is successful, then automatically enter the information page, if the login failed, then pop-up warning window

Window.pressforduration (3)

}

Alert ID and Labe are not used, estimated or bug, so can only be judged by quantity

If App.alerts.count > 0 {

Login 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) {

3 seconds delay, 3 seconds, if the login is successful, then automatically enter the information page, if the login failed, then pop-up warning window

Window.pressforduration (3)

}

Self.loginsuccess ()

}

} else {

Login successful

Self.loginsuccess ()

}

}

Here are a few points to pay special attention to:

1. When your element does not exist, it may still return an element object, but it cannot be manipulated at this time

2. When the element you want to click is obscured by a keyboard or uialertview, the tap method throws an exception

Detailed implementation can be referenced Demo:https://github.com/sungd/demo/tree/master/ios9/uitestdemo

Personal Information page test

Modify gender

Revise 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) {

2 seconds delay, 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 on the blank area

Let clearbtn = app.buttons["Clearbtn"]

Clearbtn.tap ()

Save data

Modifybtn.tap ()

Window.pressforduration (2)

Let messagebtn = app.buttons["message"]

Messagebtn.tap ();

1 seconds delay, push view takes time

Window.pressforduration (1)

Self.testmessage ()

}

The following two points need special attention:

1. TextView where focus cannot be selected when the focus is obtained

2. The trigger position of the tap event is the center of the view, so when the view's center is blocked, consider using a different view instead

Personal Message Interface test

Click of cell

The test code is as follows:

Func TestMessage () {

Let app = Xcuiapplication ()

let window = App.windows.elementAtIndex (0)

If self.canoperateelement (window) {

2 seconds delay, time to load data

Window.pressforduration (2)

}

Let table = App.tables

Table.childrenmatchingtype (. Cell). Elementatindex (8). Tap ()

Table.childrenmatchingtype (. Cell). Elementatindex (1). Tap ()

}

One thing to note here:

1. Unable to get the element pointer to TableView temporarily

Summarize

In general, UI tests can only be used for some basic functionality testing to verify that the app's functionality is working properly and that there is a crash problem. But it also has a lot of deficiencies, the process of writing test cases is very cumbersome, automatically generated code is almost impossible to run, a single function, many use cases can not cover, and a lot of bugs, greatly restricting the UI tests in the actual development of the application. Hopefully the official version will be able to fix these problems and open up more features.

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.