UIAutomation---Tools for automated testing of iOS

Source: Internet
Author: User

The Instuments tools available in Xcode can be used for automated testing of apps, as well as for performance analysis of memory leaks, file read and write operations, and more.

The first part: familiar with Instruments's uiautomation.

First, select the Xcode->open Developer tool->instruments to open the instruments tool, and then in the upper left corner you can select the device and the app under test (for example):


Here I chose an emulator for the iphone 5s, and a simple-to-know daily app written before.

When you start contacting UIAutomation, it is recommended that you familiarize yourself with the use of the tool by choosing how to record the script. For example, the bottom three buttons are played, recorded, and stopped respectively.


Click the red button in the middle to start recording, during the recording process, the Code box will update the automation script in real-time, and after clicking Stop, the recording has successfully recorded an automated test script. You can then click on the left to see the results of the iphone 5s simulator, which is consistent with the recorded action.

</pre><pre name= "Code" class= "javascript" >var target = Uiatarget.localtarget (); var app = Target.frontmostapp (); var window = App.mainwindow ();
As you can see, the iOS app is automatically tested using UIAutomation, using the JavaScript language.

Above target, the app builds a specific execution environment and then gets the app's UIWindow via App.mainwindow ().

You can also get the app's navigation bar Navigationbar by using var NavBar = App.navigationbar ().

Use Target.logelementtree () to create a hierarchical tree of apps (similar to the Gethierarchyview () method in Android Automation). A simple structure is as follows:



Part II: Control acquisition and manipulation

Getting the UI control's methods is also straightforward:

var tableviews = window.tableviews ()//can get all the TableView composed of an array. var cells = Tableviews[0].cells ()// Gets all the cell elements in the first tableview. var textfields = Window.textfields () textfields[0].value ()//Gets the value in the TextField
Other controls are obtained in a similar way, such as buttons (), images (), Webviews ().

The operation for the control is as follows:

Window.textfields () [0].setvalue ("name"); Populate the TextField with Value.cells[0].tap ()//TAP operations on a cell. Target.tap ({x:100, y:200}); Execute tap operation via coordinates Target.doubletap ({x:100, y:200}); Target.twofingertap ({x:100, y:200});// Zoom Target.pinchopenfromtoforduration ({x:20, y:200}, {x:300, y:200}, 2); Target.pinchclosefromtoforduration ({x:20, y : ($), {x:300, y:200}, 2);//drag-and-drop: Target.dragfromtoforduration ({x:160, y:200}, {x:160, y:400}, 1); Target.flickfromto ({ x:160, y:200}, {x:160, y:400});
The navigation bar Navigationbar and Tabbar are obtained and operated as follows:

var NavBar = App.navigationbar (); Navbar.leftbutton (). Tap () var TabBar = App.tabbar (); var selectedtabname = Tabbar.selectedbutton (). Name (), if (selectedtabname! = "First") {    tabbar.buttons () ["First"].tap ();}
The way to print debug log is as follows:


Part III: Customizing Automation scripts

Once you are familiar with the basic uiautomation-related rules, we can write custom automation scripts.

var target = Uiatarget.localtarget (); var app = Target.frontmostapp (); var window = App.mainwindow (); var navBar = App.naviga Tionbar (); Target.logelementtree (); Uialogger.logstart ("Maintest"); Uialogger.logmessage ("Select the Cells"); var cells = Window.tableviews () [0].cells (); for (var index in cells) {//    win Dow.tableviews () [0].cells () [0].tap ();    Uialogger.logmessage (Cells[index]);    Cells[index].tap ();    Navbar.leftbutton (). Tap ()}
Here, I simply take out all the cells on the TableView and click on the cell, then jump to the detailed interface of each cell, and finally return.

The log column shows all the execution results and is very convenient to analyze.


How to use it is not quite simple. However, here, just briefly summed up the basic usage of the uiautomation in instruments, the real essence of things to be slowly pondering.

All the technology is easy to learn hard, everyone refueling.








UIAutomation---Tools for automated testing of iOS

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.