iOS 测试 [Google EarlGrey] 0x00 安装及运行

hillchan31 · 2016年02月23日 · 最后由 梦梦GO 回复于 2017年04月13日 · 1761 次阅读
本帖已被设为精华帖!

看了恒捷老师的iOS 自动化测试框架 Google EarlGrey 尝鲜,想自己动手体验一下,把 step by step 的过程记录一下

Google EarlGrey 学习笔记:
[Google EarlGrey] 0x00 安装及运行
[Google EarlGrey] 0x01 第一个测试用例
[Google EarlGrey] 0x02 API 简介

EarlGrey 简介

EarlGrey 是一个面向 iOS apps 源码的用户界面测试框架,让使用者可以编写清楚且简洁的测试。Google 已经在在其少数 iOS 应用上使用此框架进行功能测试,例如 YouTube,日历,Photos,翻译,Play Music 等在内的 Google 应用。

EarlGrey 提供的主要特性如下:

  • 强大的内建同步机制:使用 EarlGrey 构建测试用例后,在 UI 交互前,程序会自动等待动画,网络请求等事件。这更有利于测试人员编写测试用例,测试人员无需将程序进行 sleep 或 wait 来等待特定的测试场景;同时也方便维护测试用例,测试人员只需用程序描述测试步骤即可。一般来说,你无需考虑同步性,因为 EarlGrey 会自动同步界面交互,网络请求,主 Dispatch Queue 以及主 NSOperationQueue。在测试中会有这样测试场景,当要进入下一个 UI 交互时你需要等待某些特定的事件发生,为了支持这样的场景,EarlGrey 提供了同步相关的 APIs 让用户可以控制 EarlGrey 的同步行为。测试人员可以通过使用此类 APIs 提高测试用例的稳定性

  • 可见性检测:所有用户可见元素上的交互。例如,尝试点击图片后面的按钮会导致测试立即失败。EarlGrey 使用屏幕快照差异对比的方法 (也叫做 “screenshots diffs”) 在与 UI 元素交互前确定其可见性。这样,你就可以确定对于 EarlGrey 与之交互的 UI,用户可以看到并且也能与之交互。注意:进程外 (比如系统产生的) 的弹层或者其他对话框遮盖住 UI 会对这个过程产生干扰

  • 灵活的设计:用户确定元素选中,交互,断言和同步的组件在设计上都是可扩展的。点击和滑动都是通过应用级的点击点击事件来实现的,并非使用元素级的事件处理器。在每一次 UI 交互前,EarlGrey 会判断交互的元素是否可见而不是仅仅只出现在视图中即可。EarlGrey 的 UI 交互模拟了真实用户与应用交互的方式,可以帮你找到和修复用户在使用应用时所遇到的同样的 bug。

安装和运行

先决条件

为了 EarlGrey 能够正常运行,确保被测应用满足以下要求:

最后确保测试目标已启用断言 (例如,不用设置 NS_BLOCK_ASSERTIONS)

安装 EarlGrey

你可以通过两种方式添加 EarlGrey 到你的 XCode 项目中:使用 CocoaPods,或者通过框架的形式。如果你想为 EarlGrey 贡献自己的代码,通过 Github 安装的 EarlGrey.xcodeproj 也集成了单元和功能测试。

CocoaPods 安装

官方推荐通过 CocoaPods 安装 EarlGrey

第一步:创建一个测试目标

1 EarlGrey 需要一个 Test Target。由于 EarlGrey 修改了 Test Target 的 Scheme 和 Build phases,建议创建一个独立的 Test Target 用来添加 EarlGrey 测试。如果还没有创建,那么可以在 Xcode Project Navigator 中选中项目,然后点击 Editor > Add Target

2 在 Add Target 对话框中,选中 iOS > Test > iOS Unit Testing Bundle:
iOS Unit Testing Bundle

3 因为 EarlGrey 会使用到 Schemes,所以 Test Target 必须有一个与之关联的 Scheme。如果该 Scheme 是共享的会更好。如果你的 Test Target 没有 Scheme,那么在 Manage Schemes 的对话框中,点击 + 按钮,在下拉框中选中目标。勾选 Shared 单选框,Container 设为需要测试的应用。
Edit Scheme
注意:如果之前创建过 Scheme,你需要执行一次才能让 pod install 命令选中。如果在执行 pod install 后你的 Test Target 的 Scheme 和 Build Phase 没有包含这些变更,请再次运行 pod install。

第二步:添加 EarlGrey 为依赖框架

1 添加完 Test Target 后 (如上图中 BullsEyeEarlGreyTests),你需要将 EarlGrey 添加为依赖框架。这样做,将 EarlGrey 作为测试依赖加入 Podfile。

2 因为 EarlGrey 必须嵌入被测应用中,我们需要对测试目标的 Build Phases 和 Scheme 进行一些修改。将configure_earlgrey_pods.rb文件放入执行 pod install 命令的目录下。你需要在 post_install 钩子中调用这个脚本,使用项目名称,测试目标名称和 xcscheme 文件名。

如果 CocoaPods 版本为 0.39.0,包含 EarlGrey 的 Podfile 如下:

PROJECT_NAME='BullsEye'
TEST_TARGET='BullsEyeEarlGreyTests'
SCHEME_FILE='BullsEyeEarlGreyTests.xcscheme'

xcodeproj PROJECT_NAME
target TEST_TARGET, :exclusive => true do
  pod 'EarlGrey'
end

post_install do | installer | 
  load('configure_earlgrey_pods.rb')
  configure_for_earlgrey(installer, PORJECT_NAME, TEST_TARGET, SCHEME_FILE)
end

如果 CocoaPods 版本为 1.0.0,包含 EarlGrey 的 Podfile 如下:

PROJECT_NAME='BullsEye'
TEST_TARGET='BullsEyeEarlGreyTests'
SCHEME_FILE='BullsEyeEarlGreyTests.xcscheme'

target TEST_TARGET do
  project PROJECT_NAME
  inherit! :search_paths
  pod 'EarlGrey'
end

post_install do | installer | 
  load('configure_earlgrey_pods.rb')
  configure_for_earlgrey(installer, PORJECT_NAME, TEST_TARGET, SCHEME_FILE)
end
  • 如果有多个 Targets 和 Schemes,每个 Target/Scheme 都需要调用 configure_for_earlgrey 方法
  • :exclusive => true do 或 inherit! :search_paths 标记是为了防止多次链接到 Test Target 引起冲突

第三步:执行 pod install 命令

如果 pod install 命令执行成功,通过点击项目目录下的 workspace 文件 (例如 BullsEye.xcworkspace) 在 xcode 中打开项目。

执行 pod install 命令如下:

pod install --verbose --no-repo-update

--verbose 显示安装日志,--no-repo-update 不做仓库更新
日志如下:

  Preparing
[!] Unable to load a specification for the plugin `/Library/Ruby/Gems/2.0.0/gems/cocoapods-deintegrate-1.0.0.beta.1`

Analyzing dependencies

Inspecting targets to integrate
  Using `ARCHS` setting to build architectures of target `Pods`: (``)
  Using `ARCHS` setting to build architectures of target
  `Pods-BullsEyeEarlGreyTests`: (``)

Resolving dependencies of `Podfile`

Comparing resolved specification to the sandbox manifest
  A EarlGrey

Downloading dependencies

-> Installing EarlGrey (1.0.0)
  > Copying EarlGrey from
  `/Users/Hill/Library/Caches/CocoaPods/Pods/Release/EarlGrey/1.0.0-d2369` to
  `Pods/EarlGrey`
  - Running pre install hooks

Generating Pods project
  - Creating Pods project
  - Adding source files to Pods project
  - Adding frameworks to Pods project
  - Adding libraries to Pods project
  - Adding resources to Pods project
  - Linking headers
  - Installing targets
    - Installing target `Pods-BullsEyeEarlGreyTests` iOS 8.4
  - Running post install hooks
Checking and Updating BullsEye for EarlGrey.
Adding EarlGrey Framework Location as an Environment Variable 
EarlGrey setup complete. You can use the Test Target : BullsEyeEarlGreyTests for EarlGrey testing.
    - Podfile
  - Writing Xcode project file to `Pods/Pods.xcodeproj`
    - Generating deterministic UUIDs
  - Writing Lockfile in `Podfile.lock`
  - Writing Manifest in `Pods/Manifest.lock`

Integrating client project

[!] Please close any current Xcode sessions and use `BullsEye.xcworkspace` for this project from now on.

Integrating target `Pods-BullsEyeEarlGreyTests` (`BullsEye.xcodeproj` project)
  Adding Build Phase 'Embed Pods Frameworks' to project.
  Adding Build Phase 'Copy Pods Resources' to project.
  Adding Build Phase 'Check Pods Manifest.lock' to project.
  - Running post install hooks
    - cocoapods-stats from
    `/Library/Ruby/Gems/2.0.0/gems/cocoapods-stats-0.6.2/lib/cocoapods_plugin.rb`

Sending stats
      - EarlGrey, 1.0.0
    - cocoapods-stats from
    `/Library/Ruby/Gems/2.0.0/gems/cocoapods-stats-1.0.0.beta.3/lib/cocoapods_plugin.rb`

Sending stats
      - EarlGrey, 1.0.0
  Pod installation complete! There is 1 dependency from the Podfile and 1 total
  pod installed.

点击 workspace 文件打开项目,目录结构如下,说明安装成功
project menu

参考资料

EarlGrey Github 官网
EarlGrey 特性
EarlGrey 安装及运行
Google 推出 iOS 功能性 UI 测试框架 EarlGrey
iOS 自动化测试框架 Google EarlGrey 尝鲜

下一篇: [Google EarlGrey] 0x01 第一个测试用例

共收到 2 条回复 时间 点赞

亲,我想问一下,这个文件是在哪里呀
configure_earlgrey_pods.rb

hillchan31 [Google EarlGrey] 0x01 第一个测试用例 中提及了此贴 04月13日 21:00
hillchan31 [Google EarlGrey] 0x02 API 简介 中提及了此贴 04月14日 15:38
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册