Module: RunLoop::Core
- Defined in:
- lib/run_loop/core.rb
Constant Summary collapse
- START_DELIMITER =
"OUTPUT_JSON:\n"- END_DELIMITER =
"\nEND_OUTPUT"- SCRIPTS =
{ :dismiss => 'run_dismiss_location.js', :run_loop_host => 'run_loop_host.js', :run_loop_fast_uia => 'run_loop_fast_uia.js', :run_loop_shared_element => 'run_loop_shared_element.js', :run_loop_basic => 'run_loop_basic.js' }
- SCRIPTS_PATH =
File.(File.join(File.dirname(__FILE__), '..', '..', 'scripts'))
- READ_SCRIPT_PATH =
File.join(SCRIPTS_PATH, 'read-cmd.sh')
- TIMEOUT_SCRIPT_PATH =
File.join(SCRIPTS_PATH, 'timeout3')
Class Method Summary collapse
- .automation_template(instruments, candidate = RunLoop::Environment.trace_template) ⇒ Object
- .create_uia_pipe(repl_path) ⇒ Object
-
.default_simulator(xcode = RunLoop::Xcode.new) ⇒ Object
Returns the a default simulator to target.
- .default_tracetemplate(instruments = RunLoop::Instruments.new) ⇒ Object
- .escape_host_command(cmd) ⇒ Object
- .instruments_running? ⇒ Boolean
- .jruby? ⇒ Boolean
- .log_instruments_error(msg) ⇒ Object
- .log_run_loop_options(options, xcode) ⇒ Object
-
.prepare_simulator(app, device, xcode, simctl, reset_options) ⇒ Object
Prepares the simulator for running.
- .read_response(run_loop, expected_index, empty_file_timeout = 10, search_for_property = 'index') ⇒ Object
- .script_for_key(key) ⇒ Object
- .validate_index_written(run_loop, index, logger) ⇒ Object
- .write_request(run_loop, cmd, logger = nil) ⇒ Object
Class Method Details
.automation_template(instruments, candidate = RunLoop::Environment.trace_template) ⇒ Object
501 502 503 504 505 506 |
# File 'lib/run_loop/core.rb', line 501 def self.automation_template(instruments, candidate=RunLoop::Environment.trace_template) unless candidate && File.exist?(candidate) candidate = default_tracetemplate(instruments) end candidate end |
.create_uia_pipe(repl_path) ⇒ Object
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/run_loop/core.rb', line 338 def self.create_uia_pipe(repl_path) begin Timeout::timeout(5, RunLoop::TimeoutError) do loop do begin FileUtils.rm_f(repl_path) return repl_path if system(%Q[mkfifo "#{repl_path}"]) rescue Errno::EINTR => _ sleep(0.1) end end end rescue RunLoop::TimeoutError => _ raise RunLoop::TimeoutError, 'Unable to create pipe (mkfifo failed)' end end |
.default_simulator(xcode = RunLoop::Xcode.new) ⇒ Object
Returns the a default simulator to target. This default needs to be one that installed by default in the current Xcode version.
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/run_loop/core.rb', line 321 def self.default_simulator(xcode=RunLoop::Xcode.new) version = xcode.version xcode_major = version.major xcode_minor = version.minor major = xcode_major + 2 minor = xcode_minor # Early major Xcode beta releases do not have new hardware model numbers if xcode.beta? && xcode_major == 10 model = xcode_major - 2 else model = xcode_major - 1 end "iPhone #{model} (#{major}.#{minor})" end |
.default_tracetemplate(instruments = RunLoop::Instruments.new) ⇒ Object
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 |
# File 'lib/run_loop/core.rb', line 508 def self.default_tracetemplate(instruments=RunLoop::Instruments.new) templates = instruments.templates if instruments.xcode.version_gte_8? raise(RuntimeError, %Q[ There is no Automation template for this #{instruments.xcode} version. ]) end # xcrun instruments -s templates # Xcode >= 6 will return known, Apple defined tracetemplates as names # e.g. Automation, Zombies, Allocations # # Xcode < 6 will return known, Apple defined tracetemplates as paths. # # Xcode 6 Beta versions also return paths, but revert to 'normal' # behavior when GM is released. # # Xcode 7 Beta versions appear to behavior like Xcode 6 Beta versions. template = templates.find { |name| name == 'Automation' } return template if template candidate = templates.find do |path| path =~ /\/Automation.tracetemplate/ and path =~ /Xcode/ end if !candidate.nil? return candidate.tr("\"", '').strip end raise(RuntimeError, %Q[ Expected instruments to report an Automation tracetemplate. Please report this as bug: https://github.com/calabash/run_loop/issues In the bug report, include the output of: $ xcrun xcodebuild -version $ xcrun instruments -s templates ]) end |
.escape_host_command(cmd) ⇒ Object
398 399 400 401 |
# File 'lib/run_loop/core.rb', line 398 def self.escape_host_command(cmd) backquote = "\\" cmd.gsub(backquote,backquote*4) end |
.instruments_running? ⇒ Boolean
553 554 555 |
# File 'lib/run_loop/core.rb', line 553 def self.instruments_running? RunLoop::Instruments.new.instruments_running? end |
.jruby? ⇒ Boolean
355 356 357 |
# File 'lib/run_loop/core.rb', line 355 def self.jruby? RUBY_PLATFORM == 'java' end |
.log_instruments_error(msg) ⇒ Object
403 404 405 406 |
# File 'lib/run_loop/core.rb', line 403 def self.log_instruments_error(msg) $stderr.puts "\033[31m\n\n*** #{msg} ***\n\n\033[0m" $stderr.flush end |
.log_run_loop_options(options, xcode) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/run_loop/core.rb', line 30 def self.(, xcode) return unless RunLoop::Environment.debug? # Ignore :sim_control b/c it is a ruby object; printing is not useful. ignored_keys = [:sim_control] = {} .each_pair do |key, value| next if ignored_keys.include?(key) [key] = value end # Objects that override '==' cannot be printed by awesome_print # https://github.com/michaeldv/awesome_print/issues/154 # RunLoop::Version overrides '==' [:xcode] = xcode.version.to_s [:xcode_path] = xcode.developer_dir = .ai({:sort_keys => true}) logger = [:logger] RunLoop::Logging.log_debug(logger, "\n" + ) end |
.prepare_simulator(app, device, xcode, simctl, reset_options) ⇒ Object
Prepares the simulator for running.
-
enabling accessibility and software keyboard
-
installing / uninstalling apps
TODO: move to CoreSimulator?
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 |
# File 'lib/run_loop/core.rb', line 678 def self.prepare_simulator(app, device, xcode, simctl, ) # Validate the architecture. self.expect_simulator_compatible_arch(device, app) RunLoop::CoreSimulator.quit_simulator core_sim = RunLoop::CoreSimulator.new(device, app, xcode: xcode) # Calabash 0.x can only reset the app sandbox (true/false). # Calabash 2.x has advanced reset options. if core_sim.reset_app_sandbox end # Launches the simulator if the app is not installed. core_sim.install # If CoreSimulator has already launched the simulator, it will not launch it again. core_sim.launch_simulator end |
.read_response(run_loop, expected_index, empty_file_timeout = 10, search_for_property = 'index') ⇒ Object
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 |
# File 'lib/run_loop/core.rb', line 408 def self.read_response(run_loop, expected_index, empty_file_timeout=10, search_for_property='index') debug_read = RunLoop::Environment.debug_read? log_file = run_loop[:log_file] initial_offset = run_loop[:initial_offset] || 0 offset = initial_offset result = nil loop do unless File.exist?(log_file) && File.size?(log_file) sleep(0.2) next end size = File.size(log_file) output = File.read(log_file, size-offset, offset) if /AXError: Could not auto-register for pid status change/.match(output) if /kAXErrorServerNotFound/.match(output) self.log_instruments_error('Accessibility is not enabled on device/simulator, please enable it.') end raise RunLoop::TimeoutError.new('AXError: Could not auto-register for pid status change') end if /Automation Instrument ran into an exception/.match(output) raise RunLoop::TimeoutError.new('Exception while running script') end if /FBSOpenApplicationErrorDomain error/.match(output) msg = "Instruments failed to launch app: 'FBSOpenApplicationErrorDomain error 8" if RunLoop::Environment.debug? self.log_instruments_error(msg) end raise RunLoop::TimeoutError.new(msg) end if /Error: Script threw an uncaught JavaScript error: unknown JavaScript exception/.match(output) msg = "Instruments failed to launch: because of an unknown JavaScript exception" if RunLoop::Environment.debug? self.log_instruments_error(msg) end raise RunLoop::TimeoutError.new(msg) end index_if_found = output.index(START_DELIMITER) if debug_read puts output.gsub('*', '') puts "Size #{size}" puts "offset #{offset}" puts "index_of #{START_DELIMITER}: #{index_if_found}" end if index_if_found offset = offset + index_if_found rest = output[index_if_found+START_DELIMITER.size..output.length] index_of_json = rest.index("}#{END_DELIMITER}") if index_of_json.nil? #Wait for rest of json sleep(0.1) next end json = rest[0..index_of_json] if debug_read puts "Index #{index_if_found}, Size: #{size} Offset #{offset}" puts ("parse #{json}") end offset = offset + json.size parsed_result = JSON.parse(json) if debug_read p parsed_result end json_index_if_present = parsed_result[search_for_property] if json_index_if_present && json_index_if_present == expected_index result = parsed_result break end else sleep(0.1) end end run_loop[:initial_offset] = offset RunLoop::Cache.default.write(run_loop) unless RunLoop::Environment.xtc? result end |
.script_for_key(key) ⇒ Object
49 50 51 52 53 54 |
# File 'lib/run_loop/core.rb', line 49 def self.script_for_key(key) if SCRIPTS[key].nil? return nil end SCRIPTS[key] end |
.validate_index_written(run_loop, index, logger) ⇒ Object
385 386 387 388 389 390 391 392 393 394 395 396 |
# File 'lib/run_loop/core.rb', line 385 def self.validate_index_written(run_loop, index, logger) begin Timeout::timeout(10, RunLoop::TimeoutError) do Core.read_response(run_loop, index, 10, 'last_index') end RunLoop::Logging.log_debug(logger, "validate index written for index #{index} ok") return true rescue RunLoop::TimeoutError => _ RunLoop::Logging.log_debug(logger, "validate index written for index #{index} failed. Retrying.") return false end end |
.write_request(run_loop, cmd, logger = nil) ⇒ Object
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 |
# File 'lib/run_loop/core.rb', line 359 def self.write_request(run_loop, cmd, logger=nil) repl_path = run_loop[:repl_path] index = run_loop[:index] cmd_str = "#{index}:#{escape_host_command(cmd)}" RunLoop::Logging.log_debug(logger, cmd_str) write_succeeded = false 2.times do |i| RunLoop::Logging.log_debug(logger, "Trying write of command #{cmd_str} at index #{index}") begin RunLoop::Fifo.write(repl_path, cmd_str) write_succeeded = validate_index_written(run_loop, index, logger) rescue RunLoop::Fifo::NoReaderConfiguredError, RunLoop::Fifo::WriteTimedOut => e RunLoop::Logging.log_debug(logger, "Error while writing command (retry count #{i}). #{e}") end break if write_succeeded end unless write_succeeded RunLoop::Logging.log_debug(logger, 'Failing...Raising RunLoop::WriteFailedError') raise RunLoop::WriteFailedError.new("Trying write of command #{cmd_str} at index #{index}") end run_loop[:index] = index + 1 RunLoop::Cache.default.write(run_loop) unless RunLoop::Environment.xtc? index end |