[xcode] automating ui testing
TRANSCRIPT
![Page 1: [XCode] Automating UI Testing](https://reader034.vdocuments.mx/reader034/viewer/2022042522/55a931011a28ab8d2a8b45fd/html5/thumbnails/1.jpg)
XCODE: AUTO TESTPhineas
![Page 2: [XCode] Automating UI Testing](https://reader034.vdocuments.mx/reader034/viewer/2022042522/55a931011a28ab8d2a8b45fd/html5/thumbnails/2.jpg)
ABOUT AUTO TEST
![Page 3: [XCode] Automating UI Testing](https://reader034.vdocuments.mx/reader034/viewer/2022042522/55a931011a28ab8d2a8b45fd/html5/thumbnails/3.jpg)
ABOUT AUTO-TEST
Why?
If we're testing internals we should probably test what user could see.
UI is the first thing users that see, so it is more important than how it does whatever.
![Page 4: [XCode] Automating UI Testing](https://reader034.vdocuments.mx/reader034/viewer/2022042522/55a931011a28ab8d2a8b45fd/html5/thumbnails/4.jpg)
Hard to forget to test something.
Much expensive, time, labor, money, boring, etc.
![Page 5: [XCode] Automating UI Testing](https://reader034.vdocuments.mx/reader034/viewer/2022042522/55a931011a28ab8d2a8b45fd/html5/thumbnails/5.jpg)
■ Logic tests. These tests check the correct functionality of a unit of code by itself (not in an app). With logic tests you can put together specific test cases to exercise a unit of code. You can also use logic tests to perform stress-testing of your code to ensure that it behaves correctly in extreme situations that are unlikely in a running app. These tests help you produce robust code that works correctly when used in ways that you did not anticipate.
■ Application tests. These tests check units of code in the context of your app. You can use application tests to ensure that the connections of your user-interface controls (outlets and actions) remain in place, and that your controls and controller objects work correctly with your object model as you work on your app. You can also use these tests to perform hardware testing, such as getting the location of the device on which your app is running.
![Page 6: [XCode] Automating UI Testing](https://reader034.vdocuments.mx/reader034/viewer/2022042522/55a931011a28ab8d2a8b45fd/html5/thumbnails/6.jpg)
UNIT TEST
![Page 7: [XCode] Automating UI Testing](https://reader034.vdocuments.mx/reader034/viewer/2022042522/55a931011a28ab8d2a8b45fd/html5/thumbnails/7.jpg)
TEST SCRIPT
![Page 8: [XCode] Automating UI Testing](https://reader034.vdocuments.mx/reader034/viewer/2022042522/55a931011a28ab8d2a8b45fd/html5/thumbnails/8.jpg)
INITIAL UNIT TEST
1. Static function will be executed once
2. General function always run at each test
![Page 9: [XCode] Automating UI Testing](https://reader034.vdocuments.mx/reader034/viewer/2022042522/55a931011a28ab8d2a8b45fd/html5/thumbnails/9.jpg)
XCTESTCASE
■ Fundamental Test XCTAssert(expression, format...)
■ Boolean Tests XCTAssertTrue(expression, format...) XCTAssertFalse(expression, format...)
![Page 10: [XCode] Automating UI Testing](https://reader034.vdocuments.mx/reader034/viewer/2022042522/55a931011a28ab8d2a8b45fd/html5/thumbnails/10.jpg)
■ Equality Tests XCTAssertEqual(expression1, expression2, format...) XCTAssertNotEqual(expression1, expression2, format...) XCTAssertEqualWithAccuracy(expression1, expression2, accuracy, format...)
XCTAssertNotEqualWithAccuracy(expression1, expression2, accuracy, format...)
■ Nil Tests XCTAssertNil(expression, format...) XCTAssertNotNil(expression, format...)
■ Unconditional Failure XCTFail(format...)
XCTESTCASE
![Page 11: [XCode] Automating UI Testing](https://reader034.vdocuments.mx/reader034/viewer/2022042522/55a931011a28ab8d2a8b45fd/html5/thumbnails/11.jpg)
PERFORMATION TESTING
■What are the absolute performance characteristics of
this code? Is the procedure bound by computation or
memory? What is the limiting behavior across different
sample sizes?
■What are the relative performance characteristics of this
code, as compared to its alternatives? Which is faster,
methodA or methodB?
![Page 12: [XCode] Automating UI Testing](https://reader034.vdocuments.mx/reader034/viewer/2022042522/55a931011a28ab8d2a8b45fd/html5/thumbnails/12.jpg)
CFTimeInterval startTime = CACurrentMediaTime(); {
. . . . . . . } CFTimeInterval endTime = CACurrentMediaTime(); NSLog(@"Total Runtime: %g s", endTime - startTime);
![Page 13: [XCode] Automating UI Testing](https://reader034.vdocuments.mx/reader034/viewer/2022042522/55a931011a28ab8d2a8b45fd/html5/thumbnails/13.jpg)
uint64_t t = dispatch_benchmark(iterations, ^{ @autoreleasepool { ……… } }); NSLog(@"[[NSMutableArray array] addObject:] Avg. Runtime: %llu ns", t);
![Page 14: [XCode] Automating UI Testing](https://reader034.vdocuments.mx/reader034/viewer/2022042522/55a931011a28ab8d2a8b45fd/html5/thumbnails/14.jpg)
measureBlock() { ……… }
![Page 15: [XCode] Automating UI Testing](https://reader034.vdocuments.mx/reader034/viewer/2022042522/55a931011a28ab8d2a8b45fd/html5/thumbnails/15.jpg)
■A performance test takes a block of code that you want to evaluate and runs it ten times, collecting the average execution time and the standard deviation for the runs. These statistics combine to create a baseline for comparison, a means to evaluate success or failure. To implement performance measuring tests, you write methods using new API from XCTest in Xcode 6 and later.
![Page 16: [XCode] Automating UI Testing](https://reader034.vdocuments.mx/reader034/viewer/2022042522/55a931011a28ab8d2a8b45fd/html5/thumbnails/16.jpg)
Test Suite 'Selected tests' started at 2014-12-11 01:21:27 +0000 Test Suite 'testerTests.xctest' started at 2014-12-11 01:21:27 +0000 Test Suite 'testerTests' started at 2014-12-11 01:21:27 +0000 Test Case '-[testerTests testPerformanceExample]' started. 2014-12-11 09:21:27.328 tester[724:13120] test start /Users/sunxiaoshan/Desktop/Report/XCodeTest/tester/tester/testerTests/testerTests.m:164: Test Case '-[testerTests testPerformanceExample]' measured [Time, seconds] average: 0.003, relative standard deviation: 3.742%, values: [0.002862, 0.002867, 0.002876, 0.003214, 0.003048, 0.002881, 0.002917, 0.002864, 0.002859, 0.002884], performanceMetricID:com.apple.XCTPerformanceMetric_WallClockTime, baselineName: "Local Baseline", baselineAverage: 1.810, maxPercentRegression: 10.000%, maxPercentRelativeStandardDeviation: 10.000%, maxRegression: 0.100, maxStandardDeviation: 0.100 2014-12-11 09:21:27.705 tester[724:13120] test end Test Case '-[testerTests testPerformanceExample]' passed (0.378 seconds). Test Suite 'testerTests' passed at 2014-12-11 01:21:27 +0000. Executed 1 test, with 0 failures (0 unexpected) in 0.378 (0.379) seconds Test Suite 'testerTests.xctest' passed at 2014-12-11 01:21:27 +0000. Executed 1 test, with 0 failures (0 unexpected) in 0.378 (0.379) seconds Test Suite 'Selected tests' passed at 2014-12-11 01:21:27 +0000. Executed 1 test, with 0 failures (0 unexpected) in 0.378 (0.381) seconds
![Page 17: [XCode] Automating UI Testing](https://reader034.vdocuments.mx/reader034/viewer/2022042522/55a931011a28ab8d2a8b45fd/html5/thumbnails/17.jpg)
XCTESTEXPECTATION
XCTestExpectation *documentOpenExpectation = [self expectationWithDescription:@"document open”];
[documentOpenExpectation fulfill];
[self waitForExpectationsWithTimeout:15 handler:^(NSError *error) { if (error != nil) { XCTFail(@"timeout error: %@", error); } }];
![Page 18: [XCode] Automating UI Testing](https://reader034.vdocuments.mx/reader034/viewer/2022042522/55a931011a28ab8d2a8b45fd/html5/thumbnails/18.jpg)
UI AUTOMATION TEST
![Page 19: [XCode] Automating UI Testing](https://reader034.vdocuments.mx/reader034/viewer/2022042522/55a931011a28ab8d2a8b45fd/html5/thumbnails/19.jpg)
How to do it
XCode tool: Automation
Script Language : JavaScript
![Page 20: [XCode] Automating UI Testing](https://reader034.vdocuments.mx/reader034/viewer/2022042522/55a931011a28ab8d2a8b45fd/html5/thumbnails/20.jpg)
How to get object
window.buttons()[0]
window.buttons()[“login”]
![Page 21: [XCode] Automating UI Testing](https://reader034.vdocuments.mx/reader034/viewer/2022042522/55a931011a28ab8d2a8b45fd/html5/thumbnails/21.jpg)
HOW TO START
![Page 22: [XCode] Automating UI Testing](https://reader034.vdocuments.mx/reader034/viewer/2022042522/55a931011a28ab8d2a8b45fd/html5/thumbnails/22.jpg)
HOW TO START (I)
Create a new trace document in Instruments using the Automation trace template.
![Page 23: [XCode] Automating UI Testing](https://reader034.vdocuments.mx/reader034/viewer/2022042522/55a931011a28ab8d2a8b45fd/html5/thumbnails/23.jpg)
HOW TO START (II)
Click Add > Create.
![Page 24: [XCode] Automating UI Testing](https://reader034.vdocuments.mx/reader034/viewer/2022042522/55a931011a28ab8d2a8b45fd/html5/thumbnails/24.jpg)
HOW TO START (III)
In the Detail pane Navigation bar, select Script to enter the code for your script.
![Page 25: [XCode] Automating UI Testing](https://reader034.vdocuments.mx/reader034/viewer/2022042522/55a931011a28ab8d2a8b45fd/html5/thumbnails/25.jpg)
VIEW STRUCTURE
![Page 26: [XCode] Automating UI Testing](https://reader034.vdocuments.mx/reader034/viewer/2022042522/55a931011a28ab8d2a8b45fd/html5/thumbnails/26.jpg)
ELEMENT HIERARCHY (I)
UIATarget.localTarget()
frontMostApp()
https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/InstrumentsUserGuide/UsingtheAutomationInstrument/UsingtheAutomationInstrument.html
![Page 27: [XCode] Automating UI Testing](https://reader034.vdocuments.mx/reader034/viewer/2022042522/55a931011a28ab8d2a8b45fd/html5/thumbnails/27.jpg)
ELEMENT HIERARCHY (II)
UIATarget.localTarget()
frontMostApp()
mainWindow()
https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/InstrumentsUserGuide/UsingtheAutomationInstrument/UsingtheAutomationInstrument.html
![Page 28: [XCode] Automating UI Testing](https://reader034.vdocuments.mx/reader034/viewer/2022042522/55a931011a28ab8d2a8b45fd/html5/thumbnails/28.jpg)
ELEMENT HIERARCHY (III)
UIATarget.localTarget()
frontMostApp()
mainWindow()
tableViews()[0]
https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/InstrumentsUserGuide/UsingtheAutomationInstrument/UsingtheAutomationInstrument.html
![Page 29: [XCode] Automating UI Testing](https://reader034.vdocuments.mx/reader034/viewer/2022042522/55a931011a28ab8d2a8b45fd/html5/thumbnails/29.jpg)
ELEMENT HIERARCHY (IV)
UIATarget.localTarget()
frontMostApp()
mainWindow()
tableViews()[0]
cells()[6]
https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/InstrumentsUserGuide/UsingtheAutomationInstrument/UsingtheAutomationInstrument.html
![Page 30: [XCode] Automating UI Testing](https://reader034.vdocuments.mx/reader034/viewer/2022042522/55a931011a28ab8d2a8b45fd/html5/thumbnails/30.jpg)
ELEMENT HIERARCHY (V)
UIATarget.localTarget()
frontMostApp()
mainWindow()
tableViews()[0]
cells()[6]
elements()["Name"]
https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/InstrumentsUserGuide/UsingtheAutomationInstrument/UsingtheAutomationInstrument.html
![Page 31: [XCode] Automating UI Testing](https://reader034.vdocuments.mx/reader034/viewer/2022042522/55a931011a28ab8d2a8b45fd/html5/thumbnails/31.jpg)
ELEMENT HIERARCHY (VI)
https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/InstrumentsUserGuide/UsingtheAutomationInstrument/UsingtheAutomationInstrument.html
![Page 32: [XCode] Automating UI Testing](https://reader034.vdocuments.mx/reader034/viewer/2022042522/55a931011a28ab8d2a8b45fd/html5/thumbnails/32.jpg)
INTERACTION
![Page 33: [XCode] Automating UI Testing](https://reader034.vdocuments.mx/reader034/viewer/2022042522/55a931011a28ab8d2a8b45fd/html5/thumbnails/33.jpg)
INTERACTION
element.tap()
touchAndHold()
doubleTap()
scrollToVisible()
captureScreenWithName()
![Page 34: [XCode] Automating UI Testing](https://reader034.vdocuments.mx/reader034/viewer/2022042522/55a931011a28ab8d2a8b45fd/html5/thumbnails/34.jpg)
JENKINS
![Page 35: [XCode] Automating UI Testing](https://reader034.vdocuments.mx/reader034/viewer/2022042522/55a931011a28ab8d2a8b45fd/html5/thumbnails/35.jpg)
It’s a tool that can manager projects easily and write the custom script quickly by yourself.
![Page 36: [XCode] Automating UI Testing](https://reader034.vdocuments.mx/reader034/viewer/2022042522/55a931011a28ab8d2a8b45fd/html5/thumbnails/36.jpg)
JENKINS PLUGIN
![Page 37: [XCode] Automating UI Testing](https://reader034.vdocuments.mx/reader034/viewer/2022042522/55a931011a28ab8d2a8b45fd/html5/thumbnails/37.jpg)
NEW PROJECT
![Page 38: [XCode] Automating UI Testing](https://reader034.vdocuments.mx/reader034/viewer/2022042522/55a931011a28ab8d2a8b45fd/html5/thumbnails/38.jpg)
XCTOOL COMMAND LINE
![Page 39: [XCode] Automating UI Testing](https://reader034.vdocuments.mx/reader034/viewer/2022042522/55a931011a28ab8d2a8b45fd/html5/thumbnails/39.jpg)
REFERENCE
![Page 40: [XCode] Automating UI Testing](https://reader034.vdocuments.mx/reader034/viewer/2022042522/55a931011a28ab8d2a8b45fd/html5/thumbnails/40.jpg)
REFERENCE
https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/InstrumentsUserGuide/UsingtheAutomationInstrument/UsingtheAutomationInstrument.html
https://developer.apple.com/library/ios/documentation/DeveloperTools/Reference/UIAutomationRef/_index.html#//apple_ref/doc/uid/TP40009771
http://blog.manbolo.com/2012/04/08/ios-automated-tests-with-uiautomation
http://justbm.blogspot.tw/2011/12/my-ios-ui-automation-testing.html
http://www.slideshare.net/DavidReidy/automated-ui-testing
![Page 41: [XCode] Automating UI Testing](https://reader034.vdocuments.mx/reader034/viewer/2022042522/55a931011a28ab8d2a8b45fd/html5/thumbnails/41.jpg)
http://nshipster.com/xctestcase/
https://developer.apple.com/Library/ios/documentation/DeveloperTools/Conceptual/testing_with_xcode/testing_3_writing_test_classes/testing_3_writing_test_classes.html
https://developer.apple.com/legacy/library/documentation/DeveloperTools/Conceptual/UnitTesting/01-Unit-Test_Overview/overview.html
https://developer.apple.com/library/IOs/documentation/DeveloperTools/Conceptual/testing_with_xcode/testing_4_running_tests/testing_4_running_tests.html