lib/screengrab/runner.rb in screengrab-0.5.1 vs lib/screengrab/runner.rb in screengrab-0.5.2

- old
+ new

@@ -75,15 +75,19 @@ UI.success "Captured #{number_of_screenshots} screenshots! 📷✨" end def select_device - devices = @executor.execute(command: "adb devices -l", print_all: true, print_command: true).split("\n") + devices = run_adb_command("adb devices -l", print_all: true, print_command: true).split("\n") # the first output by adb devices is "List of devices attached" so remove that and any adb startup output - # devices.reject! { |d| d.include?("List of devices attached") || d.include?("* daemon") || d.include?("unauthorized") || d.include?("offline") } devices.reject! do |device| - ['unauthorized', 'offline', '* daemon', 'List of devices attached'].any? { |status| device.include? status } + [ + 'unauthorized', # The device has not yet accepted ADB control + 'offline', # The device is offline, skip it + '* daemon', # Messages printed when the daemon is starting up + 'List of devices attached' # Header of table for data we want + ].any? { |status| device.include? status } end UI.user_error! 'There are no connected and authorized devices or emulators' if devices.empty? devices.select! { |d| d.include?(@config[:specific_device]) } if @config[:specific_device] @@ -128,13 +132,13 @@ def screenshot_file_names_in(output_directory, device_type) Dir.glob(File.join(output_directory, '**', device_type, '*.png'), File::FNM_CASEFOLD) end def determine_external_screenshots_path(device_serial) - device_ext_storage = @executor.execute(command: "adb -s #{device_serial} shell echo \\$EXTERNAL_STORAGE", - print_all: true, - print_command: true) + device_ext_storage = run_adb_command("adb -s #{device_serial} shell echo \\$EXTERNAL_STORAGE", + print_all: true, + print_command: true) File.join(device_ext_storage, @config[:app_package_name], 'screengrab') end def determine_internal_screenshots_path "/data/data/#{@config[:app_package_name]}/app_screengrab" @@ -143,13 +147,13 @@ def clear_device_previous_screenshots(device_serial, device_screenshots_paths) UI.message 'Cleaning screenshots on device' device_screenshots_paths.each do |device_path| if_device_path_exists(device_serial, device_path) do |path| - @executor.execute(command: "adb -s #{device_serial} shell rm -rf #{path}", - print_all: true, - print_command: true) + run_adb_command("adb -s #{device_serial} shell rm -rf #{path}", + print_all: true, + print_command: true) end end end def validate_apk(app_apk_path) @@ -170,40 +174,40 @@ end end def install_apks(device_serial, app_apk_path, tests_apk_path) UI.message 'Installing app APK' - apk_install_output = @executor.execute(command: "adb -s #{device_serial} install -r #{app_apk_path}", - print_all: true, - print_command: true) + apk_install_output = run_adb_command("adb -s #{device_serial} install -r #{app_apk_path}", + print_all: true, + print_command: true) UI.user_error! "App APK could not be installed" if apk_install_output.include?("Failure [") UI.message 'Installing tests APK' - apk_install_output = @executor.execute(command: "adb -s #{device_serial} install -r #{tests_apk_path}", - print_all: true, - print_command: true) + apk_install_output = run_adb_command("adb -s #{device_serial} install -r #{tests_apk_path}", + print_all: true, + print_command: true) UI.user_error! "Tests APK could not be installed" if apk_install_output.include?("Failure [") end def grant_permissions(device_serial) UI.message 'Granting the permission necessary to change locales on the device' - @executor.execute(command: "adb -s #{device_serial} shell pm grant #{@config[:app_package_name]} android.permission.CHANGE_CONFIGURATION", - print_all: true, - print_command: true) + run_adb_command("adb -s #{device_serial} shell pm grant #{@config[:app_package_name]} android.permission.CHANGE_CONFIGURATION", + print_all: true, + print_command: true) - device_api_version = @executor.execute(command: "adb -s #{device_serial} shell getprop ro.build.version.sdk", - print_all: true, - print_command: true).to_i + device_api_version = run_adb_command("adb -s #{device_serial} shell getprop ro.build.version.sdk", + print_all: true, + print_command: true).to_i if device_api_version >= 23 UI.message 'Granting the permissions necessary to access device external storage' - @executor.execute(command: "adb -s #{device_serial} shell pm grant #{@config[:app_package_name]} android.permission.WRITE_EXTERNAL_STORAGE", - print_all: true, - print_command: true) - @executor.execute(command: "adb -s #{device_serial} shell pm grant #{@config[:app_package_name]} android.permission.READ_EXTERNAL_STORAGE", - print_all: true, - print_command: true) + run_adb_command("adb -s #{device_serial} shell pm grant #{@config[:app_package_name]} android.permission.WRITE_EXTERNAL_STORAGE", + print_all: true, + print_command: true) + run_adb_command("adb -s #{device_serial} shell pm grant #{@config[:app_package_name]} android.permission.READ_EXTERNAL_STORAGE", + print_all: true, + print_command: true) end end def run_tests(device_serial, test_classes_to_use, test_packages_to_use) @config[:locales].each do |locale| @@ -214,13 +218,13 @@ "-e endingLocale #{@config[:ending_locale].tr('-', '_')}"] instrument_command << "-e class #{test_classes_to_use.join(',')}" if test_classes_to_use instrument_command << "-e package #{test_packages_to_use.join(',')}" if test_packages_to_use instrument_command << "#{@config[:tests_package_name]}/#{@config[:test_instrumentation_runner]}" - test_output = @executor.execute(command: instrument_command.join(" \\\n"), - print_all: true, - print_command: true) + test_output = run_adb_command(instrument_command.join(" \\\n"), + print_all: true, + print_command: true) UI.user_error!("Tests failed", show_github_issues: false) if test_output.include?("FAILURES!!!") end end @@ -233,13 +237,13 @@ # Make a temp directory into which to pull the screenshots before they are moved to their final location. # This makes directory cleanup easier, as the temp directory will be removed when the block completes. Dir.mktmpdir do |tempdir| device_screenshots_paths.each do |device_path| if_device_path_exists(device_serial, device_path) do |path| - @executor.execute(command: "adb -s #{device_serial} pull #{path} #{tempdir}", - print_all: false, - print_command: true) + run_adb_command("adb -s #{device_serial} pull #{path} #{tempdir}", + print_all: false, + print_command: true) end end # The SDK can't 100% determine what kind of device it is running on relative to the categories that # supply and Google Play care about (phone, 7" tablet, TV, etc.). @@ -296,13 +300,13 @@ end # Some device commands fail if executed against a device path that does not exist, so this helper method # provides a way to conditionally execute a block only if the provided path exists on the device. def if_device_path_exists(device_serial, device_path) - return if @executor.execute(command: "adb -s #{device_serial} shell ls #{device_path}", - print_all: false, - print_command: false).include?('No such file') + return if run_adb_command("adb -s #{device_serial} shell ls #{device_path}", + print_all: false, + print_command: false).include?('No such file') yield device_path rescue # Some versions of ADB will have a non-zero exit status for this, which will cause the executor to raise. # We can safely ignore that and treat it as if it returned 'No such file' @@ -310,12 +314,22 @@ def open_screenshots_summary(device_type_dir_name) unless @config[:skip_open_summary] UI.message "Opening screenshots summary" # MCF: this isn't OK on any platform except Mac - @executor.execute(command: "open #{@config[:output_directory]}/*/images/#{device_type_dir_name}/*.png", - print_all: false, - print_command: true) + run_adb_command("open #{@config[:output_directory]}/*/images/#{device_type_dir_name}/*.png", + print_all: false, + print_command: true) end + end + + def run_adb_command(command, print_all: false, print_command: false) + output = @executor.execute(command: command, + print_all: print_all, + print_command: print_command) || '' + output.lines.reject do |line| + # Debug/Warning output from ADB} + line.start_with?('adb: ') + end.join('') # Lines retain their newline chars end end end