214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
|
# File 'lib/run_loop/core.rb', line 214
def self.run_with_options(options)
before = Time.now
self.prepare(options)
logger = options[:logger]
sim_control ||= options[:sim_control] || RunLoop::SimControl.new
xcode ||= options[:xcode] || sim_control.xcode
instruments = RunLoop::Instruments.new
instruments.kill_instruments(xcode)
device_target = options[:udid] || options[:device_target] || detect_connected_device || 'simulator'
if device_target && device_target.to_s.downcase == 'device'
device_target = detect_connected_device
end
log_file = options[:log_path]
timeout = options[:timeout] || 30
results_dir = options[:results_dir] || RunLoop::DotDir.make_results_dir
results_dir_trace = File.join(results_dir, 'trace')
FileUtils.mkdir_p(results_dir_trace)
dependencies = options[:dependencies] || []
dependencies << File.join(scripts_path, 'calabash_script_uia.js')
dependencies.each do |dep|
FileUtils.cp(dep, results_dir)
end
script = File.join(results_dir, '_run_loop.js')
code = UIAScriptTemplate.new(SCRIPTS_PATH, options[:script]).result
code = code.gsub(/\$PATH/, results_dir)
code = code.gsub(/\$READ_SCRIPT_PATH/, READ_SCRIPT_PATH)
code = code.gsub(/\$TIMEOUT_SCRIPT_PATH/, TIMEOUT_SCRIPT_PATH)
code = code.gsub(/\$MODE/, 'FLUSH') unless options[:no_flush]
repl_path = File.join(results_dir, 'repl-cmd.pipe')
FileUtils.rm_f(repl_path)
uia_strategy = options[:uia_strategy]
if uia_strategy == :host
create_uia_pipe(repl_path)
else
FileUtils.touch repl_path
end
RunLoop::HostCache.default.clear unless RunLoop::Environment.xtc?
cal_script = File.join(SCRIPTS_PATH, 'calabash_script_uia.js')
File.open(script, 'w') do |file|
if include_calabash_script?(options)
file.puts IO.read(cal_script)
end
file.puts code
end
udid = options[:udid]
bundle_dir_or_bundle_id = options[:bundle_dir_or_bundle_id]
if !(udid && bundle_dir_or_bundle_id)
udid, bundle_dir_or_bundle_id = self.udid_and_bundle_for_launcher(device_target, options, sim_control)
end
args = options.fetch(:args, [])
log_file ||= File.join(results_dir, 'run_loop.out')
discovered_options =
{
:udid => udid,
:results_dir_trace => results_dir_trace,
:bundle_dir_or_bundle_id => bundle_dir_or_bundle_id,
:results_dir => results_dir,
:script => script,
:log_file => log_file,
:args => args
}
merged_options = options.merge(discovered_options)
if self.simulator_target?(merged_options)
self.prepare_simulator(merged_options, sim_control)
end
self.log_run_loop_options(merged_options, xcode)
automation_template = automation_template(instruments)
RunLoop::Logging.(logger, "Starting on #{device_target} App: #{bundle_dir_or_bundle_id}")
pid = instruments.spawn(automation_template, merged_options, log_file)
File.open(File.join(results_dir, 'run_loop.pid'), 'w') do |f|
f.write pid
end
run_loop = {:pid => pid,
:index => 1,
:uia_strategy => uia_strategy,
:udid => udid,
:app => bundle_dir_or_bundle_id,
:repl_path => repl_path,
:log_file => log_file,
:results_dir => results_dir}
uia_timeout = options[:uia_timeout] || RunLoop::Environment.uia_timeout || 10
RunLoop::Logging.log_debug(logger, "Preparation took #{Time.now-before} seconds")
before_instruments_launch = Time.now
fifo_retry_on = [RunLoop::Fifo::NoReaderConfiguredError,
RunLoop::Fifo::WriteTimedOut]
begin
if options[:validate_channel]
options[:validate_channel].call(run_loop, 0, uia_timeout)
else
cmd = "UIALogger.logMessage('Listening for run loop commands')"
begin
fifo_timeout = options[:fifo_timeout] || 30
RunLoop::Fifo.write(repl_path, "0:#{cmd}", timeout: fifo_timeout)
rescue *fifo_retry_on => e
message = "Error while writing to fifo. #{e}"
RunLoop::Logging.log_debug(logger, message)
raise RunLoop::TimeoutError.new(message)
end
Timeout::timeout(timeout, RunLoop::TimeoutError) do
read_response(run_loop, 0, uia_timeout)
end
end
rescue RunLoop::TimeoutError => e
RunLoop::Logging.log_debug(logger, "Failed to launch. #{e}: #{e && e.message}")
message = %Q(
"Timed out waiting for UIAutomation run-loop #{e}.
Logfile: #{log_file}
#{File.read(log_file)}
)
raise RunLoop::TimeoutError, message
end
RunLoop::Logging.log_debug(logger, "Launching took #{Time.now-before_instruments_launch} seconds")
dylib_path = self.dylib_path_from_options(merged_options)
if dylib_path
RunLoop::LLDB.kill_lldb_processes
app = RunLoop::App.new(options[:app])
lldb = RunLoop::DylibInjector.new(app.executable_name, dylib_path)
lldb.retriable_inject_dylib
end
RunLoop.log_debug("It took #{Time.now - before} seconds to launch the app")
run_loop
end
|