Class: Snapshot::SimulatorLauncherXcode8

Inherits:
SimulatorLauncherBase show all
Defined in:
snapshot/lib/snapshot/simulator_launchers/simulator_launcher_xcode_8.rb

Instance Attribute Summary

Attributes inherited from SimulatorLauncherBase

#collected_errors, #current_number_of_retries_due_to_failing_simulator, #launcher_config

Instance Method Summary collapse

Methods inherited from SimulatorLauncherBase

#add_media, #clear_status_bar, #copy_simulator_logs, #disable_slide_to_type, #erase_simulator, #initialize, #interface_style, #localize_simulator, #override_status_bar, #prepare_directories_for_launch, #prepare_for_launch, #prepare_simulators_for_launch, #uninstall_app

Constructor Details

This class inherits a constructor from Snapshot::SimulatorLauncherBase

Instance Method Details

#execute(command: nil, language: nil, locale: nil, device_type: nil, launch_args: nil) ⇒ Object


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'snapshot/lib/snapshot/simulator_launchers/simulator_launcher_xcode_8.rb', line 76

def execute(command: nil, language: nil, locale: nil, device_type: nil, launch_args: nil)
  prefix_hash = [
    {
      prefix: "Running Tests: ",
      block: proc do |value|
        value.include?("Touching")
      end
    }
  ]
  FastlaneCore::CommandExecutor.execute(command: command,
                                      print_all: true,
                                  print_command: true,
                                         prefix: prefix_hash,
                                        loading: "Loading...",
                                          error: proc do |output, return_code|
                                            ErrorHandler.handle_test_error(output, return_code)

                                            # no exception raised... that means we need to retry
                                            UI.error("Caught error... #{return_code}")

                                            self.current_number_of_retries_due_to_failing_simulator += 1
                                            if self.current_number_of_retries_due_to_failing_simulator < 20
                                              launch_one_at_a_time(language, locale, device_type, launch_arguments)
                                            else
                                              # It's important to raise an error, as we don't want to collect the screenshots
                                              UI.crash!("Too many errors... no more retries...")
                                            end
                                          end)
end

#launch_one_at_a_time(language, locale, device_type, launch_arguments) ⇒ Object

Returns true if it succeeded


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'snapshot/lib/snapshot/simulator_launchers/simulator_launcher_xcode_8.rb', line 51

def launch_one_at_a_time(language, locale, device_type, launch_arguments)
  prepare_for_launch([device_type], language, locale, launch_arguments)

  add_media([device_type], :photo, launcher_config.add_photos) if launcher_config.add_photos
  add_media([device_type], :video, launcher_config.add_videos) if launcher_config.add_videos

  open_simulator_for_device(device_type)

  command = TestCommandGeneratorXcode8.generate(device_type: device_type, language: language, locale: locale)

  if locale
    UI.header("#{device_type} - #{language} (#{locale})")
  else
    UI.header("#{device_type} - #{language}")
  end

  execute(command: command, language: language, locale: locale, device_type: device_type, launch_args: launch_arguments)

  raw_output = File.read(TestCommandGeneratorXcode8.xcodebuild_log_path(device_type: device_type, language: language, locale: locale))

  dir_name = locale || language

  return Collector.fetch_screenshots(raw_output, dir_name, device_type, launch_arguments.first)
end

#open_simulator_for_device(device_name) ⇒ Object


106
107
108
109
110
111
# File 'snapshot/lib/snapshot/simulator_launchers/simulator_launcher_xcode_8.rb', line 106

def open_simulator_for_device(device_name)
  return unless FastlaneCore::Env.truthy?('FASTLANE_EXPLICIT_OPEN_SIMULATOR')

  device = TestCommandGeneratorBase.find_device(device_name)
  FastlaneCore::Simulator.launch(device) if device
end

#run_for_device_and_language(language, locale, device, launch_arguments, retries = 0) ⇒ Object

This is its own method so that it can re-try if the tests fail randomly

Returns:

  • true/false depending on if the tests succeeded


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'snapshot/lib/snapshot/simulator_launchers/simulator_launcher_xcode_8.rb', line 34

def run_for_device_and_language(language, locale, device, launch_arguments, retries = 0)
  return launch_one_at_a_time(language, locale, device, launch_arguments)
rescue => ex
  UI.error(ex.to_s) # show the reason for failure to the user, but still maybe retry

  if retries < launcher_config.number_of_retries
    UI.important("Tests failed, re-trying #{retries + 1} out of #{launcher_config.number_of_retries + 1} times")
    run_for_device_and_language(language, locale, device, launch_arguments, retries + 1)
  else
    UI.error("Backtrace:\n\t#{ex.backtrace.join("\n\t")}") if FastlaneCore::Globals.verbose?
    self.collected_errors << ex
    raise ex if launcher_config.stop_after_first_error
    return false # for the results
  end
end

#take_screenshots_one_simulator_at_a_timeObject


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'snapshot/lib/snapshot/simulator_launchers/simulator_launcher_xcode_8.rb', line 8

def take_screenshots_one_simulator_at_a_time
  results = {} # collect all the results for a nice table
  launcher_config.devices.each_with_index do |device, device_index|
    # launch_args_set always has at at least 1 item (could be "")
    launcher_config.launch_args_set.each do |launch_args|
      launcher_config.languages.each_with_index do |language, language_index|
        locale = nil
        if language.kind_of?(Array)
          locale = language[1]
          language = language[0]
        end
        results[device] ||= {}

        current_run = device_index * launcher_config.languages.count + language_index + 1
        number_of_runs = launcher_config.languages.count * launcher_config.devices.count
        UI.message("snapshot run #{current_run} of #{number_of_runs}")
        results[device][language] = run_for_device_and_language(language, locale, device, launch_args)
        copy_simulator_logs([device], language, locale, launch_args)
      end
    end
  end
  results
end