Class: Snapshot::Runner
- Inherits:
-
Object
- Object
- Snapshot::Runner
- Defined in:
- lib/snapshot/runner.rb
Constant Summary collapse
- TRACE_DIR =
'/tmp/snapshot_traces'
Instance Method Summary collapse
- #clean_old_traces ⇒ Object
- #com(cmd) ⇒ Object
- #copy_screenshots(language) ⇒ Object
- #determine_app_path ⇒ Object
- #generate_test_command(device, language) ⇒ Object
- #parse_test_line(line) ⇒ Object
- #prepare_simulator(device, language) ⇒ Object
- #reinstall_app(device, language) ⇒ Object
- #run_tests(device, language) ⇒ Object
- #teardown_simulator(device, language) ⇒ Object
-
#udid_for_simulator(name) ⇒ Object
fetches the UDID of the simulator type.
- #work(clean: true, build: true) ⇒ Object
Instance Method Details
#clean_old_traces ⇒ Object
52 53 54 55 |
# File 'lib/snapshot/runner.rb', line 52 def clean_old_traces FileUtils.rm_rf(TRACE_DIR) FileUtils.mkdir_p(TRACE_DIR) end |
#com(cmd) ⇒ Object
80 81 82 83 84 |
# File 'lib/snapshot/runner.rb', line 80 def com(cmd) puts cmd.magenta result = `#{cmd}` puts result if result.to_s.length > 0 end |
#copy_screenshots(language) ⇒ Object
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/snapshot/runner.rb', line 186 def copy_screenshots(language) resulting_path = File.join(SnapshotConfig.shared_instance.screenshots_path, language) FileUtils.mkdir_p resulting_path unless SnapshotConfig.shared_instance.skip_alpha_removal ScreenshotFlatten.new.run(TRACE_DIR) end ScreenshotRotate.new.run(TRACE_DIR) Dir.glob("#{TRACE_DIR}/**/*.png") do |file| FileUtils.cp_r(file, resulting_path + '/') end return Dir.glob("#{TRACE_DIR}/**/*.png").count end |
#determine_app_path ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/snapshot/runner.rb', line 154 def determine_app_path # Determine the path to the actual app and not the WatchKit app build_dir = SnapshotConfig.shared_instance.build_dir || '/tmp/snapshot' Dir.glob("#{build_dir}/**/*.app/*.plist").each do |path| watchkit_enabled = `/usr/libexec/PlistBuddy -c 'Print WKWatchKitApp' '#{path}'`.strip next if watchkit_enabled == 'true' # we don't care about WatchKit Apps app_identifier = `/usr/libexec/PlistBuddy -c 'Print CFBundleIdentifier' '#{path}'`.strip if app_identifier and app_identifier.length > 0 # This seems to be the valid Info.plist @app_identifier = app_identifier return File.("..", path) # the app end end end |
#generate_test_command(device, language) ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/snapshot/runner.rb', line 203 def generate_test_command(device, language) is_ipad = (device.downcase.include?'ipad') script_path = SnapshotConfig.shared_instance.js_file(is_ipad) [ "instruments", "-w '#{device}'", "-D '#{TRACE_DIR}/trace'", "-t 'Automation'", "#{@app_path.shellescape}", "-e UIARESULTSPATH '#{TRACE_DIR}'", "-e UIASCRIPT '#{script_path}'", "-AppleLanguages '(#{language})'", "-AppleLocale '#{language}'" ].join(' ') end |
#parse_test_line(line) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/snapshot/runner.rb', line 170 def parse_test_line(line) if line =~ /.*Target failed to run.*/ return :retry elsif line.include?"Screenshot captured" return :screenshot elsif line.include? "Instruments wants permission to analyze other processes" return :need_permission elsif line =~ /.*Error: (.*)/ raise "UIAutomation Error: #{$1}" elsif line =~ /Instruments Usage Error :(.*)/ raise "Instruments Usage Error: #{$1}" elsif line.include?"__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object" raise "Looks like something is wrong with the used app. Make sure the build was successful." end end |
#prepare_simulator(device, language) ⇒ Object
57 58 59 60 |
# File 'lib/snapshot/runner.rb', line 57 def prepare_simulator(device, language) SnapshotConfig.shared_instance.blocks[:setup_for_device_change].call(device, udid_for_simulator(device), language) # Callback SnapshotConfig.shared_instance.blocks[:setup_for_language_change].call(language, device) # deprecated end |
#reinstall_app(device, language) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/snapshot/runner.rb', line 75 def reinstall_app(device, language) app_identifier = ENV["SNAPSHOT_APP_IDENTIFIER"] app_identifier ||= @app_identifier def com(cmd) puts cmd.magenta result = `#{cmd}` puts result if result.to_s.length > 0 end udid = udid_for_simulator(device) com("killall 'iOS Simulator'") sleep 3 com("xcrun simctl boot '#{udid}'") com("xcrun simctl uninstall booted '#{app_identifier}'") sleep 3 com("xcrun simctl install booted '#{@app_path.shellescape}'") com("xcrun simctl shutdown booted") end |
#run_tests(device, language) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/snapshot/runner.rb', line 98 def run_tests(device, language) Helper.log.info "Running tests on #{device} in language #{language}".green clean_old_traces ENV['SNAPSHOT_LANGUAGE'] = language command = generate_test_command(device, language) Helper.log.debug command.yellow retry_run = false lines = [] errors = [] PTY.spawn(command) do |stdout, stdin, pid| # Waits for process so that we can see if anything has failed begin stdout.sync stdout.each do |line| lines << line begin puts line.strip if $verbose result = parse_test_line(line) case result when :retry retry_run = true when :screenshot Helper.log.info "Successfully took screenshot 📱" when :need_permission raise "Looks like you may need to grant permission for Instruments to analyze other processes.\nPlease Ctrc + C and run this command: \"#{command}\"" end rescue Exception => ex Helper.log.error lines.join('') Helper.log.error ex.to_s.red errors << ex.to_s end end rescue Errno::EIO => e # We could maybe do something like this ensure ::Process.wait pid end end if retry_run Helper.log.error "Instruments tool failed again. Re-trying..." sleep 2 # We need enough sleep... that's an instruments bug errors = run_tests(device, language) end return errors end |
#teardown_simulator(device, language) ⇒ Object
62 63 64 65 |
# File 'lib/snapshot/runner.rb', line 62 def teardown_simulator(device, language) SnapshotConfig.shared_instance.blocks[:teardown_language].call(language, device) # Callback SnapshotConfig.shared_instance.blocks[:teardown_device].call(device, language) # deprecated end |
#udid_for_simulator(name) ⇒ Object
fetches the UDID of the simulator type
67 68 69 70 71 72 73 |
# File 'lib/snapshot/runner.rb', line 67 def udid_for_simulator(name) # fetches the UDID of the simulator type all = `instruments -s`.split("\n") all.each do |current| return current.match(/\[(.*)\]/)[1] if current.include?name end raise "Could not find simulator '#{name}' to install the app on." end |
#work(clean: true, build: true) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/snapshot/runner.rb', line 8 def work(clean: true, build: true) SnapshotConfig.shared_instance.js_file # to verify the file can be found earlier Builder.new.build_app(clean: clean) if build @app_path = determine_app_path counter = 0 errors = [] FileUtils.rm_rf SnapshotConfig.shared_instance.screenshots_path if SnapshotConfig.shared_instance.clear_previous_screenshots SnapshotConfig.shared_instance.devices.each do |device| SnapshotConfig.shared_instance.languages.each do |language| reinstall_app(device, language) unless ENV["SNAPSHOT_SKIP_UNINSTALL"] prepare_simulator(device, language) begin errors.concat(run_tests(device, language)) counter += copy_screenshots(language) rescue => ex Helper.log.error(ex) end teardown_simulator(device, language) end end `killall "iOS Simulator"` # close the simulator after the script is finished ReportsGenerator.new.generate if errors.count > 0 Helper.log.error "-----------------------------------------------------------" Helper.log.error errors.join(' - ').red Helper.log.error "-----------------------------------------------------------" raise "Finished generating #{counter} screenshots with #{errors.count} errors.".red else Helper.log.info "Successfully finished generating #{counter} screenshots.".green end Helper.log.info "Check it out here: #{SnapshotConfig.shared_instance.screenshots_path}".green end |