IOS automated testing-UIAutomation Learning

Source: Internet
Author: User
Tags how to write test cases

I. Software Installation
Download and install the Xcode development tool from the appstore. The latest version is 4.5.1.
 
2. Use Xcode to compile a test script
Note: If you are running a test case on the IOS simulator, you must have the source code of the tested application to install the application in the simulator, in this example, a simple Iphone application compiled by yourself is used. You can also search for an Open Source Application on the Internet.
 
1. When you have the source code of an application, select the application to be tested in Xcode, and click "Product-Profile" in the menu bar. The Instruments tool is displayed, in the pop-up tool, select IOS Simulator-> Automation and click Profile.

2. Select the project to be tested in the pop-up Automation tool, Add a test script in Add-Create, and click Create. The test script compiling area appears in the intermediate area, add the following script in the middle
// Obtain the current window object
Var target = UIATarget. localTarget ();
Var app = target. frontMostApp ();
Var window = app. mainWindow ();
// Print the number of controls on the current interface
Target. logElementTree ();
 
For details about the API, refer to the official documentation.
Http://developer.apple.com/library/ios/#documentation/DeveloperTools/Reference/UIAutomationRef/_index.html



3. Click the Record button in the upper left corner to start running the test case. After the test is completed, it is in the middle of the tool. The log of the running result appears in the original code, we wrote target in the code just now. logElementTree (). This API prints the control information on the current page. You can view the control in the tree structure in the log, and click here to view some properties of the control, this API is also useful in coding.
If you want to switch to the code writing page, click the red box in the figure to switch.
Note: When you click Record to run the test case, the code will not Stop automatically after it is executed. Therefore, you need to manually click the Stop button in the upper left corner to Stop running.




4. Recording playback
If this is the first time you write an automatic test script, you may not know many APIs. At this time, you can use the recording and return visit function of UIAutomation to see how to write test cases, of course, you will also find that there will be a lot of repeated code through recording and return visit. When you are familiar with it, you can write scripts without recording and return visit.
First, you switch to the script writing interface. At this time, you will see a red button at the bottom of the middle. You click it to start recording. At this time, it will automatically help you start the tested application in the simulator, then, the operation steps you click on the simulator will be recorded.
The red area in the middle is the automatically generated code. You can click the arrow in the code to view different APIs, because different paths can be used to find an element, it will be helpful to understand your attributes.
After the recording is complete, click the block icon next to the red button to stop recording,
Click Record in the upper left corner to run the recording result again.

3. Writing Test Cases
Through the above, you should know how to use the UIAutomation tool and write simple test scripts. At that time, writing is not a test case, and the minimum assertion operation is none, this article describes how to compile a real test case.
1,Element Recognition
To write test cases, we first think about how to identify and find the control elements to be operated, in section 2, we briefly introduced that using the Script output control Log is a way to identify the control, and another way is to use the device's own Accessibility Inspector function.
On the simulator, you can also activate the Accessibility detector. Start the simulator, find "Settings> General> Accessibility Inspector", and set it to "open.
At this time, a layer will appear on the simulator. You enter the application to be tested and click the corresponding control. As shown in, some information is displayed. Label is the id attribute of the control, traits is the type of the Control. Frame is the position and size of the control {36,295}, {36,295}. The first position {} is the coordinate of the upper left corner of the control, {} is the width and height of the control. The two parameters can be used to calculate the coordinates of the control.

2,EditingWriteTestUse Cases
 
Var testName = "FirstTest ";
UIALogger. logStart (testName );
Var target = UIATarget. localTarget ();
Var app = target. frontMostApp ();
Var window = app. mainWindow ();
App. logElementTree ();
Window. tableViews () [0]. cells () [0]. tap ();
Target. delay (3 );
Var date = window. elements () ["date"];
UIALogger. logMessage (date );
If (date ){
UIALogger. logPass (testName );
}
Else {
UIALogger. logFail (testName );
}
 
Some APIs provided by UIAutomation are used to write an automatic test case, including element search and assertion operations. However, if you write multiple test cases, you will find some problems, for example, some codes are repeated, the use cases are not well organized, and the assertion operation is not convenient.
 

3. tuneup Introduction
The following describes an open-source JS library tuneup Based on UIAutomation extension. This js extension library is convenient for you to compile test cases.
Tuneup Open Source Address https://github.com/alexvollmer/tuneup_js
 
First, you can create a new test case directory on your computer. For example, the folder name is Demo. Next, you can create a new lib sub-folder to store the needed extension library, you can put the downloaded tuneup contents in the lib directory and create a js file for the test case in the demo folder, in the test code, you only need to reference tuneup with import. the test cases written through tuneup are as follows.
 
#import "lib/tuneup/tuneup.js"   
var target = UIATarget.localTarget(); 
var app = target.frontMostApp();  
/* Second is the comment of the test case. You can enter the purpose of the test case and the author and other information */
test("Second", function(target, app) { 
var window = app.mainWindow(); 
app.logElementTree(); 
window.tableViews()[0].cells()[0].tap(); 
var date = window.elements()["date"]; 
AssertNotNull (date, "the date details page is displayed. The date attribute is not found! ");});
 
 
4. Run test cases through command line
In order to run the test script automatically and periodically, we hope that the test cases can be started through the command line, next we will introduce how to use the command line to start the test cases we have compiled.
instruments -t /Applications/Xcode.app/Contents/Applications/Instruments.app/Contents/PlugIns/ AutomationInstrument.bundle/Contents/Resources/Automation.tracetemplate "/Users/ios/Library /Application Support/iPhone Simulator/6.0/Applications/D02EF837-94F7-457A-989A-A654FC034803 / DemoSBTableViewTest.app" -e UIASCRIPT /AutoTest/workspace/IosDemo/test.js -e UIARESULTSPATH /AutoTest/workspace/IosDemo/lib/
The above is the script to run the test case through the command line,
-T: The following parameter is the path of Automation. tracetemplate. You do not need to modify it. This is the path for Versions later than Xcode4.5. The path for versions earlier than Xcode4.5 will be different.
The "/User ***. app" parameter is the absolute path of the program to be tested. Applications installed in the simulator can be found on the local hard disk.
-E UIASCRIPT specifies the js script to be executed
-E UIARESULTSPATH: Specifies the path where output results are stored.
 
5. Improvement points for subsequent UIAutomation-based extension
1. The output use case results are stored in the given xml file. The xml file needs to be parsed to the testng format to facilitate integration with Jenkins and other continuous integration platforms.
2. Search for positioning elements, which can only be found at the control level and Level 1. it is inconvenient to use and needs to be encapsulated again.
3. Some peripheral control needs to be added to make the entire automated test run completely automated. In addition, some failed re-run mechanisms need to be added. There are usually some unstable factors in UI automation.

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.