Getting Started with instruments_automation

Source: Internet
Author: User

Instruments is a utility used by applications to dynamically track and analyze Mac OS X and IOS code. This is a flexible and powerful tool that allows you to track one or more processes and examine the collected data. This way, instruments can help you better understand the behavior of your application and your operating system.

Using the Instruments app, you can use a special tool (the Instruments tool) to track the behavior of different aspects of the same process. You can also use the actions that should be used to record a series of user interfaces and respond to them, and you can also use one or more instruments tools to collect data.

The Instruments app includes the following features:

    • Analyze the behavior of one or more processes

    • Record the actions of a series of users and respond to them, reliably reproduce these events and collect data that runs multiple times

    • Create your own custom DTrace instruments to analyze system and application behavior

    • Save user interface record and Instruments configuration as template and access from Xcode inside using Instruments, you can:
      1) Tracing problems that are difficult to reproduce in the code
      2) Perform a performance analysis of your program
      3) Automated testing of your code
      4) Stress Test your program
      5) Perform general system-level fault diagnosis
      6) Get a deeper understanding of how your code works
      Instruments is available in Xcode 3.0 and Mac OS X 10.5 and beyond.

  

UI Auto-testing is an important additional feature in iOS, supported by a new tool object called "Automation". The script for the Automation tool is written in JavaScript, primarily for analyzing application performance and user behavior, simulating/firing the requested event, and using it to perform simple UI testing and related functional testing of the application under test.

  A. Simple recording script

Open Xcode and select the project you want to test for automation. You can open the instruments tool as shown, or you can click Product-profile in the menu bar to open the instruments tool.

Select the Automation tool

View the test log by selecting Editor log

Two. Positioning the interface elements

Write a test case where you can find the element that identifies the control. Here are 2 ways of doing this

1. You can write Target.logelementtree () in the script to print the control information for the current page in the log.

In script location, write scripts, Target.logelementtree ();

After writing the script, click the red button in the upper left corner to start the program in the emulator.

2. Use the Accessibility inspector function that comes with it.

Open the emulator and find "Settings > General > Accessibility > Accessibility Inspector" and turn it on.

Three. Manually write the test code we need.

The recorded code is poorly maintainable and robust, and lacks the necessary checkpoints, so the actual effect is very poor. At this point, you need to manually write test code, the use of custom-tailored way to achieve automation.

First of all, be sure to remember this address:

https://developer.apple.com/library/prerelease/ios/documentation/DeveloperTools/Reference/UIAutomationRef/

This is Apple's official reference document, listing all the classes in the UI Automation JavaScript Library, and all of the app's operations are ultimately done through these APIs.

Common steps in automation are usually three steps: positioning, operation, checking

1) Positioning elements

UIAutomation is targeted to a specific element by means of hierarchical access. Apple provides a logelementtree () method to print the control tree. In the new test script, add the following line of code:

Target.logelementtree ();

Execute this code (click on the red button in the upper left corner) and we can see what we have entered (the tree structure on the left, which is the view structure in the right-hand emulator)

On the left is the UI hierarchy of the current app view, and when you need to navigate to an element, you can follow it down one level at a time until the target element is reached. For example, you need to locate the login button.

var target= UIATarget.localTarget();

var app =target.frontMostApp();

var window= app.mainWindow();

//获取登录按钮

var loginButton = window. buttons () ["Login"];

which

The Uiatarget object represents the highest-level UI for the environment in which the application is being tested, and here Localtarget () indicates that the iphone device running the app

The Uiaapplication object represents the app-level UI, and the object that is obtained through the Frontmostapp () method is the running AV iphone app.

The Uiawindow object represents the window-level UI in the app, the object that is obtained by the MainWindow () method, which refers to the main form in the current app, and the current interface of an app usually has only one main form.

In real-world projects, the difference between different elements starts at the window level and is the same at the window level.

2) Operating elements

The above one is already able to do the element positioning, now need to operate on this element, the most common is the tap

loginButton.tap();

For different elements, there will be differences in the specific methods of operation provided, please refer to the official documentation for details. After entering the login screen, you need to enter your nickname, email, password, and the same steps:

//定位输入框

var nicknamefield = window . statictexts () ["nickname"]

var emailField = window.staticTexts()["邮箱"]

var pwField = window.staticTexts()["密码"]

//操作

Nicknamefield.setvalue ("Hello");

emailField.setValue("[email protected]");

pwField.setValue("ppp111");

//点击执行登陆

loginButton.tap();

3) Check the results

After the operation is complete, you need to check that the results are as expected.

if(window.scrollViews()[0].staticTexts()[0].name() == "请叫我雷锋"){

      UIALogger.logPass("测试通过");

}else{

      UIALogger.logMessage(window.scrollViews()[0].staticTexts()[0].name());

      UIALogger.logFail("测试失败");

}

These steps above, even if the uiautomation of the completion of the Hello World.

Three items to note:

1) delay

Above this code, in the actual run, basically will always test failed, because from the beginning to login, to display user information, need a certain time, click the login button operation, immediately to determine the user information will certainly fail. In addition, the network fluctuations, or the app/test machine may be the lag, is one of the reasons for the frequent failure of the test script, so we need some operations, the artificial increase of some necessary delay, waiting for the completion of the operation, thereby enhancing the stability of the code. Apple provides a method delay (number TimeInterval) at the target layer, which is used to delay the execution of the script, and we can add the method where necessary.

target.delay(3);

2) Bullet Frame

The box alert is a special type in iOS, it doesn't belong to the app hierarchy, and it doesn't operate as an ordinary element, and Apple provides a special way to handle it, Uiatarget.onalert, and the test engine automatically calls this method to handle the frame when the box appears.

"A Perfect Stay"

3) Log

Uialogger is primarily used to output various types of logs. including Logstart,logpass,logfail,logmessage,logdebug,logwarning, LogError

The first three are usually used to differentiate a test case, Logstart represents the beginning of a test until Logpass or Logfail. The latter four are used to enter different levels of logging during the test.

This article refers to the article:

1.Instruments Guide ("Old Wolf")

2.iOS Instruments UI Automation easy to use (master bypass)

3. Instruments-automation-based iOS App UI Automation Test Guide

Getting Started with instruments_automation

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.