關于Xcode UI自動化測試的介紹,網上有很多,那我就直接來說一下使用步驟。
一、創建工程,選擇Include UI Tests
粘貼圖片.png
二、工程創建完畢后,會多出一個UITestDemoTests
文件目錄,如下:
屏幕快照 2017-08-15 下午5.27.42.png
三、如果在創建工程時沒有勾選Include UI Tests
,還可以通過下面的方式來添加UI Tests
部分,如下,
粘貼圖片1.png
粘貼圖片2.png
四、進入UITestDemoUITests.m
開始測試操作,如下圖,
粘貼圖片3.png
點擊上圖中的錄制按鈕,然后等工程運行好,就可以按照自己想要測試的內容來一步一步操作,同時會把你的操作以代碼的形式錄制下來。
下面是我錄制好的代碼:
#import <XCTest/XCTest.h>
@interface UITestDemoUITests : XCTestCase
@end
@implementation UITestDemoUITests
- (void)setUp {
[super setUp];
self.continueAfterFailure = NO;
[[[XCUIApplication alloc] init] launch];
}
- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
}
- (void)testMyDemo
{
XCUIApplication *app = [[XCUIApplication alloc] init];
[app.buttons[@"\u8ba1\u7b97"] tap];
[app pressForDuration:1.0f];//延時執行下一步操作
[app.navigationBars[@"SecondView"].buttons[@"Back"] tap];
}
點擊command+u
進行測試,你的模擬器或真機就會自動運行項目,按照之前錄制好的操作自動進行測試。
如下面的效果圖
2017081615028666995993ed0b2c970.gif
順便貼一下我項目中的部分測試代碼:
#pragma mark 測試
- (void)testFaceRecognize
{
for (int i = 0; i < TEST_COUNT; i ++)
{
XCUIApplication *app = [[XCUIApplication alloc] init];
XCTAssert(app != nil,@"獲取當前應用失敗!");
[app pressForDuration:DEALY_TIME];
XCTAssert(app.collectionViews.cells != nil,@"獲取Item失敗!");
[[app.collectionViews.cells containingType:XCUIElementTypeImage identifier:@"main_item01"].element tap];
XCUIElementQuery *collectionViewsQuery = app.images[@"main_back_image.png"].collectionViews;
[collectionViewsQuery.images[@"camera_image_meitu_1"] tap];
XCUIElement *button = app.sheets.buttons[@"\u672c\u5730\u6d4b\u8bd5\u56fe\u7247"];
XCTAssert(button != nil ,@"獲取Button失敗!");
[button tap];
[[[[collectionViewsQuery childrenMatchingType:XCUIElementTypeCell] elementBoundByIndex:0].otherElements containingType:XCUIElementTypeImage identifier:@"camera_image_meitu_2"].element tap];
[button tap];
[[[[collectionViewsQuery childrenMatchingType:XCUIElementTypeCell] elementBoundByIndex:1].otherElements containingType:XCUIElementTypeImage identifier:@"camera_image_meitu_2"].element tap];
[button tap];
[[[[collectionViewsQuery childrenMatchingType:XCUIElementTypeCell] elementBoundByIndex:2].otherElements containingType:XCUIElementTypeImage identifier:@"camera_image_meitu_2"].element tap];
[button tap];
[[[[collectionViewsQuery childrenMatchingType:XCUIElementTypeCell] elementBoundByIndex:3].otherElements containingType:XCUIElementTypeImage identifier:@"camera_image_meitu_2"].element tap];
[button tap];
[[[[collectionViewsQuery childrenMatchingType:XCUIElementTypeCell] elementBoundByIndex:4].otherElements containingType:XCUIElementTypeImage identifier:@"camera_image_meitu_2"].element tap];
[button tap];
[[collectionViewsQuery.cells.otherElements containingType:XCUIElementTypeImage identifier:@"camera_image_meitu_2"].element tap];
[button tap];
[app.images[@"main_back_image.png"].buttons[@"\u4eba\u8138\u8bc6\u522b"] tap];
[app pressForDuration:DEALY_TIME];//延時執行下一步測試
}
}
就先寫這么多,有問題的可以留言。代碼不足的地方還請各位多多指教,謝謝了。