Xcode UI自动化测试

作者: ZYiDa | 来源:发表于2017-08-16 15:01 被阅读133次

    关于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];//延时执行下一步测试
        }
    }
    

    就先写这么多,有问题的可以留言。代码不足的地方还请各位多多指教,谢谢了。

    相关文章

      网友评论

      本文标题:Xcode UI自动化测试

      本文链接:https://www.haomeiwen.com/subject/fzsvrxtx.html