5

How to change code in hooks to reset (iOS) app at specific scenario ? means only to those scenario where tags mention as @reset

jmoody
  • 2,480
  • 1
  • 16
  • 22
Mesh
  • 193
  • 1
  • 7

1 Answers1

10

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

UPDATED for Calabash 0.17 and run-loop 2.0.2

This project contains an example of how to use Cucumber tags and Before hooks to re-install apps and clear application data on simulators and devices.

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

In particular, see these two files:

I won't reproduce the entire 01_launch.rb example here, but here are the hooks:

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

The key is recognize the difference between testing on simulators and devices and testing on the Test Cloud and locally.

You can reset the application settings and content before every Scenario using the RESET_BETWEEN_SCENARIOS env variable.

$ RESET_BETWEEN_SCENARIOS=1 cucumber

You can also implement a backdoor method to reset your app to a good known state. This example is a bit wonky and out of date, but I have used it to great effect in the past - it really speeds up tests - briar-ios-example You can do all kinds of things with backdoors: log users in/out, reset databases, wipe user preferences, add/remove files from the sandbox.

jmoody
  • 2,480
  • 1
  • 16
  • 22
  • That is up to you. If you just want to reset the simulator content and settings, try: reset_simulator_content_and_settings. No promises though - I can't recall if this is function is exposed publicly. You might need to require some files in your 01_launch.rb file. – jmoody Jul 02 '14 at 15:09
  • Updated my answer based on your comment and some questions on the calabash-ios google group and calabash-ios issues. – jmoody Jul 02 '14 at 23:58
  • In the 0.10.0 release of calabash there will be some changes based on this pull request: https://github.com/calabash/calabash-ios/pull/440 – jmoody Jul 21 '14 at 20:55
  • 1
    On the Xamarin Test Cloud, which uses real devices, setting RESET_BETWEEN_SCENARIOS=1 will reset the app sandbox before each Scenario. Locally, there is no known way to reset an app's sandbox on a physical device. And can you please accept my answer as correct? – jmoody Jul 25 '14 at 13:18
  • Updated the examples for 0.10.0.pre2 and above. – jmoody Jul 25 '14 at 13:31
  • thanks for quick reply . When can we expect app reset feature for real devices in iOS using calabash ? ? . Is there any work-around for same ?. – Mesh Aug 01 '14 at 07:03
  • re: When can we expect reset feature for real devices - This feature is not on our roadmap. – jmoody Sep 04 '14 at 16:18
  • Updated for 0.17.0 and run-loop 2.0.2 – jmoody Dec 18 '15 at 13:05