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,966评论 25 709
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,099评论 19 139
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,267评论 4 61
  • 转:http://www.jianshu.com/p/d5fca0185e83 Xcode测试 前言 总算在今天把...
    测试小蚂蚁阅读 3,058评论 0 20
  • 婚姻就像这幅图,无论谁放手,受伤的都是孩子!多经典的话,多牛的配图。经常有人聊起:要孩子是为了什么?传宗接代还是养...
    夕柏玟阅读 195评论 14 13