5

如何在特定场景下更改挂钩中的代码以重置(iOS)应用程序?仅适用于标签提及为@reset 的场景

4

1 回答 1

10

https://github.com/cucumber/cucumber/wiki/Hooks#tagged-hooks

更新了 Calabash 0.17 和运行循环 2.0.2

该项目包含一个示例,说明如何使用 Cucumber 标签和 Before hooks 重新安装应用程序并清除模拟器和设备上的应用程序数据。

https://github.com/calabash/ios-smoke-test-app/

特别是,请参阅以下两个文件:

我不会在这里重现整个 01_launch.rb 示例,但这里是钩子:

Before("@no_relaunch") do
  @no_relaunch = true
end

Before('@reset_app_btw_scenarios') do
  if xamarin_test_cloud? || LaunchControl.target_is_simulator?
    ENV['RESET_BETWEEN_SCENARIOS'] = '1'
  else
    LaunchControl.install_on_physical_device
  end
end

Before('@reset_device_settings') do
  if xamarin_test_cloud?
    ENV['RESET_BETWEEN_SCENARIOS'] = '1'
  elsif LaunchControl.target_is_simulator?
    target = LaunchControl.target
    instruments = RunLoop::Instruments.new
    xcode = instruments.xcode
    device = instruments.simulators.find do |sim|
      sim.udid == target || sim.instruments_identifier(xcode) == target
    end

    RunLoop::CoreSimulator.erase(device)
  else
    LaunchControl.install_on_physical_device
  end
end

Before do |scenario|
  launcher = LaunchControl.launcher

  options = {
    #:uia_strategy => :host
    #:uia_strategy => :shared_element
    :uia_strategy => :preferences
  }

  relaunch = true

  if @no_relaunch
    begin
      launcher.ping_app
      attach_options = options.dup
      attach_options[:timeout] = 1
      launcher.attach(attach_options)
      relaunch = launcher.device == nil
    rescue => e
      RunLoop.log_info2("Tag says: don't relaunch, but cannot attach to the app.")
      RunLoop.log_info2("#{e.class}: #{e.message}")
      RunLoop.log_info2("The app probably needs to be launched!")
    end
  end

  if relaunch
    launcher.relaunch(options)
    launcher.calabash_notify(self)
  end

  ENV['RESET_BETWEEN_SCENARIOS'] = '0'

  # Re-installing the app on a device does not clear the Keychain settings,
  # so we must clear them manually.
  if scenario.source_tag_names.include?('@reset_device_settings')
    if xamarin_test_cloud? || LaunchControl.target_is_physical_device?
      keychain_clear
    end
  end
end

关键是要认识到在模拟器和设备上进行测试与在测试云和本地进行测试之间的区别。

您可以使用env 变量在每个场景之前重置应用程序设置和内容。RESET_BETWEEN_SCENARIOS

$ RESET_BETWEEN_SCENARIOS=1 cucumber

您还可以实现一个后门方法来将您的应用程序重置为一个众所周知的状态。这个例子有点古怪而且过时了,但我在过去使用它效果很好——它确实加快了测试速度——briar-ios-example 你可以用后门做各种各样的事情:登录/注销用户,重置数据库、擦除用户首选项、从沙箱中添加/删除文件。

于 2014-07-02T00:18:26.093 回答