如何在特定情况下更改钩子中的代码以重置(iOS)应用程序? 仅表示标签提及为@reset
的情况答案 0 :(得分:10)
https://github.com/cucumber/cucumber/wiki/Hooks#tagged-hooks
更新为Calabash 0.17和run-loop 2.0.2
此项目包含一个示例,说明如何使用Cucumber标签和Before钩子重新安装应用程序并清除模拟器和设备上的应用程序数据。
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
关键是要认识到在模拟器和设备上进行测试以及在测试云和本地测试之间的区别。
您可以使用RESET_BETWEEN_SCENARIOS
env变量在每个场景之前重置应用程序设置和内容。
$ RESET_BETWEEN_SCENARIOS=1 cucumber
您还可以实施后门方法,将您的应用重置为知名状态。这个例子有点不稳定且已经过时了,但我过去曾经使用过它 - 它确实加快了测试速度 - briar-ios-example你可以用后门做各种事情:登录/退出用户,重置数据库,擦除用户首选项,从沙箱中添加/删除文件。