diff --git a/dev/devicelab/lib/framework/utils.dart b/dev/devicelab/lib/framework/utils.dart index 6a46937354..1f96e8b626 100644 --- a/dev/devicelab/lib/framework/utils.dart +++ b/dev/devicelab/lib/framework/utils.dart @@ -443,11 +443,11 @@ List flutterCommandArgs(String command, List options) { Future flutter(String command, { List options = const [], bool canFail = false, // as in, whether failures are ok. False means that they are fatal. - Map environment, + Map environment = const {}, }) { final List args = flutterCommandArgs(command, options); return exec(path.join(flutterDirectory.path, 'bin', 'flutter'), args, - canFail: canFail, environment: environment); + canFail: canFail, environment: {...?environment, 'FLUTTER_IOS_SCREENSHOT_ON_CONNECTION_FAILURE': 'true'}); } /// Runs a `flutter` command and returns the standard output as a string. diff --git a/packages/flutter_tools/lib/src/ios/devices.dart b/packages/flutter_tools/lib/src/ios/devices.dart index 867b25bc51..e97046ee17 100644 --- a/packages/flutter_tools/lib/src/ios/devices.dart +++ b/packages/flutter_tools/lib/src/ios/devices.dart @@ -11,6 +11,7 @@ import 'package:vm_service/vm_service.dart' as vm_service; import '../application_package.dart'; import '../base/common.dart'; +import '../base/error_handling_io.dart'; import '../base/file_system.dart'; import '../base/io.dart'; import '../base/logger.dart'; @@ -439,6 +440,7 @@ class IOSDevice extends Device { installationResult = await iosDeployDebugger.launchAndAttach() ? 0 : 1; } if (installationResult != 0) { + await _screenshotOnFailure(); _logger.printError('Could not run ${bundle.path} on $id.'); _logger.printError('Try launching Xcode and selecting "Product > Run" to fix the problem:'); _logger.printError(' open ios/Runner.xcworkspace'); @@ -468,6 +470,7 @@ class IOSDevice extends Device { packageName: FlutterProject.current().manifest.appName, ); if (localUri == null) { + await _screenshotOnFailure(); return LaunchResult.failed(); } return LaunchResult.succeeded(observatoryUri: localUri); @@ -549,6 +552,23 @@ class IOSDevice extends Device { }); await _portForwarder?.dispose(); } + + Future _screenshotOnFailure() async { + final bool screenshotOnConnectionFailure = _platform + .environment['FLUTTER_IOS_SCREENSHOT_ON_CONNECTION_FAILURE'] == 'true'; + if (!screenshotOnConnectionFailure) { + return; + } + final File file = _fileSystem.file('test_screenshot.png'); + try { + await takeScreenshot(file); + _logger.printStatus('BASE64 SCREENSHOT:${base64.encode(file.readAsBytesSync())}'); + } on Exception { + _logger.printError('Failed to take screenshot'); + } finally { + ErrorHandlingFileSystem.deleteIfExists(file); + } + } } /// Decodes a vis-encoded syslog string to a UTF-8 representation. diff --git a/packages/flutter_tools/tool/screenshot_decoder.dart b/packages/flutter_tools/tool/screenshot_decoder.dart new file mode 100644 index 0000000000..9e09e80a7d --- /dev/null +++ b/packages/flutter_tools/tool/screenshot_decoder.dart @@ -0,0 +1,23 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'dart:convert'; +import 'dart:io'; + +/// decodes a base64 screenshot stored in the devicelab. +/// +/// Usage dart tool/screenshot_decoder.dart path/to/log_file +void main(List arguments) { + int screenshot = 0; + final String logFile = arguments.first; + for (final String line in File(logFile).readAsLinesSync()) { + if (!line.contains('BASE64 SCREENSHOT:')) { + continue; + } + final String message = line.split('BASE64 SCREENSHOT:')[1]; + final List bytes = base64.decode(message); + File('flutter_screenshot_$screenshot.png').writeAsBytesSync(bytes); + screenshot += 1; + } +}