51Testing软件测试论坛

标题: iOS 测试 [Google EarlGrey] 0x00 安装及运行 [打印本页]

作者: 八戒你干嘛    时间: 2017-8-8 10:31
标题: iOS 测试 [Google EarlGrey] 0x00 安装及运行
EarlGrey简介EarlGrey 是一个面向iOS apps源码的用户界面测试框架,让使用者可以编写清楚且简洁的测试。Google 已经在在其少数iOS应用上使用此框架进行功能测试,例如YouTube,日历,Photos,翻译,Play Music等在内的Google应用。
EarlGrey提供的主要特性如下:
安装和运行先决条件为了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:


3 因为EarlGrey会使用到Schemes,所以Test Target必须有一个与之关联的Scheme。如果该Scheme是共享的会更好。如果你的Test Target没有Scheme,那么在Manage Schemes的对话框中,点击+按钮,在下拉框中选中目标。勾选Shared单选框,Container设为需要测试的应用。



注意:如果之前创建过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如下:

  1. PROJECT_NAME='BullsEye'
  2. TEST_TARGET='BullsEyeEarlGreyTests'
  3. SCHEME_FILE='BullsEyeEarlGreyTests.xcscheme'

  4. xcodeproj PROJECT_NAME
  5. target TEST_TARGET, :exclusive => true do
  6.   pod 'EarlGrey'
  7. end

  8. post_install do | installer |
  9.   load('configure_earlgrey_pods.rb')
  10.   configure_for_earlgrey(installer, PORJECT_NAME, TEST_TARGET, SCHEME_FILE)
  11. end
复制代码
如果CocoaPods版本为 1.0.0,包含EarlGrey的Podfile如下:
  1. PROJECT_NAME='BullsEye'
  2. TEST_TARGET='BullsEyeEarlGreyTests'
  3. SCHEME_FILE='BullsEyeEarlGreyTests.xcscheme'

  4. target TEST_TARGET do
  5.   project PROJECT_NAME
  6.   inherit! :search_paths
  7.   pod 'EarlGrey'
  8. end

  9. post_install do | installer |
  10.   load('configure_earlgrey_pods.rb')
  11.   configure_for_earlgrey(installer, PORJECT_NAME, TEST_TARGET, SCHEME_FILE)
  12. end
复制代码

第三步:执行pod install命令如果pod install命令执行成功,通过点击项目目录下的workspace文件(例如BullsEye.xcworkspace)在xcode中打开项目。
执行pod install命令如下:
  1. pod install --verbose --no-repo-update
复制代码
--verbose 显示安装日志,--no-repo-update不做仓库更新
日志如下:
  1. Preparing
  2. [!] Unable to load a specification for the plugin `/Library/Ruby/Gems/2.0.0/gems/cocoapods-deintegrate-1.0.0.beta.1`

  3. Analyzing dependencies

  4. Inspecting targets to integrate
  5.   Using `ARCHS` setting to build architectures of target `Pods`: (``)
  6.   Using `ARCHS` setting to build architectures of target
  7.   `Pods-BullsEyeEarlGreyTests`: (``)

  8. Resolving dependencies of `Podfile`

  9. Comparing resolved specification to the sandbox manifest
  10.   A EarlGrey

  11. Downloading dependencies

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

  17. Generating Pods project
  18.   - Creating Pods project
  19.   - Adding source files to Pods project
  20.   - Adding frameworks to Pods project
  21.   - Adding libraries to Pods project
  22.   - Adding resources to Pods project
  23.   - Linking headers
  24.   - Installing targets
  25.     - Installing target `Pods-BullsEyeEarlGreyTests` iOS 8.4
  26.   - Running post install hooks
  27. Checking and Updating BullsEye for EarlGrey.
  28. Adding EarlGrey Framework Location as an Environment Variable
  29. EarlGrey setup complete. You can use the Test Target : BullsEyeEarlGreyTests for EarlGrey testing.
  30.     - Podfile
  31.   - Writing Xcode project file to `Pods/Pods.xcodeproj`
  32.     - Generating deterministic UUIDs
  33.   - Writing Lockfile in `Podfile.lock`
  34.   - Writing Manifest in `Pods/Manifest.lock`

  35. Integrating client project

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

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

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

  48. Sending stats
  49.       - EarlGrey, 1.0.0
  50.   Pod installation complete! There is 1 dependency from the Podfile and 1 total
  51.   pod installed.
复制代码
点击workspace文件打开项目,目录结构如下,说明安装成功







欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2