写点什么

EarlGrey - iOS UI 自动化测试框架

作者:qife
  • 2025-07-01
    福建
  • 本文字数:1855 字

    阅读完需:约 6 分钟

项目标题与描述

EarlGrey 是由 Google 开发的 iOS 原生 UI 自动化测试框架,支持 Objective-C 和 Swift 语言。它是一个白盒测试解决方案,深度集成 XCTest 框架,可直接在 Xcode 的 Test Navigator 中运行测试。


核心价值:


  • 自动同步 UI 状态、网络请求和各种队列

  • 提供丰富的交互 API 和断言功能

  • 支持编写清晰简洁的测试代码

  • 与 Xcode 完美集成,可直接从 IDE 运行测试


注意:EarlGrey 1.0 已停止维护,推荐使用集成 XCUITest 的EarlGrey 2.0

功能特性

核心功能

  • 智能同步机制:自动等待 UI 进入稳定状态

  • 丰富的交互 API:点击、滑动、长按等手势操作

  • 元素定位器:多种方式定位 UI 元素(Accessibility ID、文本等)

  • 断言验证:丰富的断言方法验证 UI 状态

  • WebView 支持:可测试嵌入的 Web 内容

  • 多手势支持:支持多指滑动手势

独特价值

  • 稳定性保障:内置同步机制减少测试 flakes

  • 精确控制:可自定义手势参数(持续时间、方向等)

  • 调试友好:失败时自动记录视图层次结构

  • 扩展性强:支持自定义匹配器和动作

安装指南

CocoaPods 安装

  1. 安装 EarlGrey gem:


   gem install earlgrey
复制代码


  1. 在项目目录下运行:


   pod install
复制代码


  1. 打开生成的 xcworkspace 文件

Carthage 安装

  1. 安装依赖工具:


   brew install carthage   xcode-select --install   gem install earlgrey
复制代码


  1. 初始化 EarlGrey:


   earlgrey install --carthage --swift-version=3.0 --target=EarlGreyExampleSwiftTests   carthage update EarlGrey --platform ios
复制代码

系统要求

  • Xcode 10.0+

  • iOS 9.0+

  • CocoaPods 1.0.0+

使用说明

基础测试示例

// 选择并点击按钮[[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"ClickMe")]    performAction:grey_tap()];
// 验证标签文本[[EarlGrey selectElementWithMatcher:grey_accessibilityLabel(@"textLabel")] assertWithMatcher:grey_text(@"Expected Text")];
复制代码

滑动操作

// 向下滑动50点[[EarlGrey selectElementWithMatcher:grey_kindOfClass([UIScrollView class])]    performAction:grey_scrollInDirection(kGREYDirectionDown, 50)];
复制代码

高级功能

// 自定义长按手势(持续2秒)[[EarlGrey selectElementWithMatcher:grey_accessibilityID(@"LongPressArea")]    performAction:grey_longPressWithDuration(2.0)];
// 多指滑动手势(3指同时滑动)GREYMultiFingerSwipeAction *swipe = [[GREYMultiFingerSwipeAction alloc] initWithDirection:kGREYDirectionUp duration:1.0 numberOfFingers:3];[[EarlGrey selectElementWithMatcher:grey_anyOf(grey_kindOfClass([UIView class]), nil)] performAction:swipe];
复制代码

核心代码

基础交互实现

// GREYTapAction.m 点击动作实现- (BOOL)perform:(id)element error:(__strong NSError **)errorOrNil {    if (![self satisfiesConstraintsForElement:element error:errorOrNil]) {        return NO;    }        UIView *viewToTap = [element isKindOfClass:[UIView class]] ?         element : [element grey_viewContainingSelf];    UIWindow *window = [viewToTap isKindOfClass:[UIWindow class]] ?         (UIWindow *)viewToTap : viewToTap.window;        return [GREYTapper tapOnWindow:window                     numberOfTaps:_numberOfTaps                        location:[self grey_tapPointForElement:element]                           error:errorOrNil];}
复制代码

同步机制实现

// GREYUIThreadExecutor.m 主线程同步- (void)executeSync:(GREYExecutionBlock)block {    if ([NSThread isMainThread]) {        block();    } else {        dispatch_semaphore_t waitForBlock = dispatch_semaphore_create(0);                dispatch_async(dispatch_get_main_queue(), ^{            block();            dispatch_semaphore_signal(waitForBlock);        });                dispatch_semaphore_wait(waitForBlock, DISPATCH_TIME_FOREVER);    }}
复制代码

元素定位实现

// GREYElementMatcher.m 元素匹配逻辑- (BOOL)matches:(id)item {    if (![self checkClassAndVisibility:item]) {        return NO;    }        for (id<GREYMatcher> matcher in _matchers) {        if (![matcher matches:item]) {            return NO;        }    }        return YES;}
复制代码


更多精彩内容 请关注我的个人公众号 公众号(办公 AI 智能小助手)公众号二维码


办公AI智能小助手


用户头像

qife

关注

还未添加个人签名 2021-05-19 加入

还未添加个人简介

评论

发布
暂无评论
EarlGrey - iOS UI自动化测试框架_ios_qife_InfoQ写作社区