From b52d2cacfb24c67ebb04e16bd4aed97a1af3d026 Mon Sep 17 00:00:00 2001 From: Bartek Pacia Date: Tue, 7 Jun 2022 00:31:55 +0200 Subject: [PATCH] implement "--device" option for command "maestro drive" --- .../lib/src/commands/drive_command.dart | 19 ++++++++--- .../maestro_cli/lib/src/external/adb.dart | 24 +++++++++---- .../lib/src/external/flutter_driver.dart | 34 ++++++++++++++++--- 3 files changed, 62 insertions(+), 15 deletions(-) diff --git a/packages/maestro_cli/lib/src/commands/drive_command.dart b/packages/maestro_cli/lib/src/commands/drive_command.dart index cb58bdb8da..393f9ab79d 100644 --- a/packages/maestro_cli/lib/src/commands/drive_command.dart +++ b/packages/maestro_cli/lib/src/commands/drive_command.dart @@ -27,6 +27,10 @@ class DriveCommand extends Command { 'driver', abbr: 'd', help: 'Dart file which starts flutter_driver.', + ) + ..addOption( + 'device', + help: 'Serial number of ADB device to use.', ); } @@ -65,6 +69,9 @@ class DriveCommand extends Command { throw const FormatException('`driver` argument is not a string'); } + final device = argResults?['device'] as String?; + log.info('device: $device'); + final options = MaestroDriveOptions( host: host, port: int.parse(portStr), @@ -72,10 +79,14 @@ class DriveCommand extends Command { driver: driver, ); - await adb.installApps(); - await adb.forwardPorts(options.port); - await adb.runServer(); - await flutter_driver.runTestsWithOutput(options.driver, options.target); + await adb.installApps(device: device); + await adb.forwardPorts(options.port, device: device); + await adb.runServer(device: device); + await flutter_driver.runTestsWithOutput( + options.driver, + options.target, + device: device, + ); return 0; } diff --git a/packages/maestro_cli/lib/src/external/adb.dart b/packages/maestro_cli/lib/src/external/adb.dart index c5727108c1..517b270d20 100644 --- a/packages/maestro_cli/lib/src/external/adb.dart +++ b/packages/maestro_cli/lib/src/external/adb.dart @@ -4,10 +4,10 @@ import 'dart:io'; import 'package:maestro_cli/src/common/common.dart'; import 'package:path/path.dart' as path; -Future installApps() async { +Future installApps({String? device}) async { final progress1 = log.progress('Installing server'); try { - await _installApk(serverArtifactFile); + await _installApk(serverArtifactFile, device: device); } catch (err) { progress1.fail('Failed to install server'); rethrow; @@ -16,7 +16,7 @@ Future installApps() async { final progress2 = log.progress('Installing instrumentation'); try { - await _installApk(instrumentationArtifactFile); + await _installApk(instrumentationArtifactFile, device: device); } catch (err) { progress2.fail('Failed to install instrumentation'); rethrow; @@ -25,12 +25,16 @@ Future installApps() async { progress2.complete('Installed instrumentation'); } -Future forwardPorts(int port) async { +Future forwardPorts(int port, {String? device}) async { final progress = log.progress('Forwarding ports'); final result = await Process.run( 'adb', [ + if (device != null) ...[ + '-s', + device, + ], 'forward', 'tcp:$port', 'tcp:$port', @@ -46,7 +50,7 @@ Future forwardPorts(int port) async { progress.complete('Forwarded ports'); } -Future runServer() async { +Future runServer({String? device}) async { final progress = log.progress('Starting instrumentation server'); Process process; @@ -54,6 +58,10 @@ Future runServer() async { process = await Process.start( 'adb', [ + if (device != null) ...[ + '-s', + device, + ], 'shell', 'am', 'instrument', @@ -82,10 +90,14 @@ Future runServer() async { progress.complete('Started instrumentation server'); } -Future _installApk(String name) async { +Future _installApk(String name, {String? device}) async { final result = await Process.run( 'adb', [ + if (device != null) ...[ + '-s', + device, + ], 'install', path.join(artifactPath, name), ], diff --git a/packages/maestro_cli/lib/src/external/flutter_driver.dart b/packages/maestro_cli/lib/src/external/flutter_driver.dart index 7324116afb..733802a307 100644 --- a/packages/maestro_cli/lib/src/external/flutter_driver.dart +++ b/packages/maestro_cli/lib/src/external/flutter_driver.dart @@ -4,7 +4,7 @@ import 'package:maestro_cli/src/common/common.dart'; /// Runs flutter driver with the given [driver] and [target] and waits until the /// drive is done. -Future runTests(String driver, String target) async { +Future runTests(String driver, String target, {String? device}) async { log.info('Running tests...'); final res = await Process.run( @@ -15,6 +15,10 @@ Future runTests(String driver, String target) async { driver, '--target', target, + if (device != null) ...[ + '--device-id', + device, + ], ], runInShell: true, ); @@ -28,7 +32,11 @@ Future runTests(String driver, String target) async { /// drive is done. /// /// Prints standard output of "flutter drive". -Future runTestsWithOutput(String driver, String target) async { +Future runTestsWithOutput( + String driver, + String target, { + String? device, +}) async { log.info('Running tests with output...'); final res = await Process.start( @@ -39,11 +47,15 @@ Future runTestsWithOutput(String driver, String target) async { driver, '--target', target, + if (device != null) ...[ + '--device-id', + device, + ], ], runInShell: true, ); - final sub = res.stdout.listen((msg) { + final stdOutSub = res.stdout.listen((msg) { final text = 'driver: ${systemEncoding.decode(msg)}'; if (text.contains('I/flutter')) { log.info(text); @@ -52,6 +64,18 @@ Future runTestsWithOutput(String driver, String target) async { } }); - await res.exitCode; - await sub.cancel(); + final stdErrSub = res.stderr.listen((msg) { + final text = 'driver: ${systemEncoding.decode(msg)}'; + log.severe(text); + }); + + final exitCode = await res.exitCode; + await stdOutSub.cancel(); + await stdErrSub.cancel(); + + if (exitCode == 0) { + log.info('flutter_driver exited with code $exitCode'); + } else { + log.severe('flutter_driver exited with code $exitCode'); + } }