Xcode UI自動化測試

關于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];//延時執行下一步測試
    }
}

就先寫這么多,有問題的可以留言。代碼不足的地方還請各位多多指教,謝謝了。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,316評論 25 708
  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,933評論 18 139
  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,241評論 4 61
  • 轉:http://www.lxweimin.com/p/d5fca0185e83 Xcode測試 前言 總算在今天把...
    測試小螞蟻閱讀 3,009評論 0 20
  • 婚姻就像這幅圖,無論誰放手,受傷的都是孩子!多經典的話,多牛的配圖。經常有人聊起:要孩子是為了什么?傳宗接代還是養...
    夕柏玟閱讀 194評論 14 13