How to use IOS automation testing tool uiautomation


The instruments tool in Xcode can be used for automatic test of app, performance analysis of memory leak, file read and write operation, etc

Part one: familiar with uiautomation of instruments

First, select Xcode > Open developer tool > instruments to open the instruments tool, and then select the device and tested app in the upper left corner (as shown in the figure below)

Here, I choose a simulator of iPhone 5S and a simple Zhihu daily app written before

When you first contact UI automation, it is recommended to choose the way of recording script to familiarize yourself with the use of the tool. As shown in the figure below, the three buttons at the bottom are play, record and stop

Click the red button in the middle to start recording. During the recording process, the automatic script will be updated in the code box in real time. After clicking stop, an automatic test script will be recorded successfully. Then you can click execute on the left to see the execution result in the iPhone 5S simulator, which is consistent with the recorded action

</pre><pre name="code">var target = ();
var app = ();
var window = ();

It can be seen that using UI automation to test IOS app automatically, using JavaScript language

The above target, app is to establish a specific execution environment, and then get the UIWindow of the app through ()

You can also get the navigation bar of the app through VAR navbar = ()

Use () to build the hierarchy tree of app (similar to gethierarchyview () method in Android Automation)   A simple structure is as follows:

The second part: access and operation of control

The method to get UI controls is also very simple

Var tableviews = () // you can get an array of all tableviews
Var cells = tableviews [0]. Cells() // all cell elements in the first tableview are obtained
var textFields = ()
Textfields [0]. Value() // get the value in the textfield

Other controls are obtained by similar methods, such as buttons(), images(), webviews()

The operation of the control is as follows:

()[0].setValue("name"); //  Fill the textfield with value
Cells [0]. Tap() // tap a cell
({x: 100, y: 200}); //  Tap operation through coordinates
({x: 100, y: 200});
({x: 100, y: 200});
({x:20, y:200}, {x:300, y:200}, 2);
({x:20, y:200}, {x:300, y:200}, 2);
//Drag and slide:
({x:160, y:200}, {x:160, y:400}, 1);
({x:160, y:200}, {x:160, y:400});

The navigation bar and tabbar are obtained and operated as follows:

var navBar = ();
var tabBar = ();
var selectedTabName = ().name();
if (selectedTabName != "First") {

Print debug log as follows:

( "Select the cells" );

Part 3: custom automation script

Familiar with the basic UI automation related rules, we can write custom automation scripts

var target = ();
var app = ();
var window = ();
var navBar = ();
( "Select the cells" );
var cells = ()[0].cells();
for (var index in cells) {
//    ()[0].cells()[0].tap();

Here, I simply take out all the cells on the tableview, click the cell in turn, then jump to the detailed interface of each cell, and finally return

Log column, will present all the execution results, analysis is also very convenient

How to use it? Is it very simple to use it? However, here, I just summarize the basic usage of UI automation in instruments. I have to ponder over the real essence by myself

The above is how to use the IOS automation testing tool uiautomation in detail. For more information about IOS automation testing tool uiautomation, please pay attention to other related articles of developer!