|
| 1 | +// Copyright 2022 The Chromium Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:convert'; |
| 6 | +import 'dart:io'; |
| 7 | + |
| 8 | +import 'package:collection/collection.dart'; |
| 9 | + |
| 10 | +import 'io_utils.dart'; |
| 11 | +import 'test_app_driver.dart'; |
| 12 | + |
| 13 | +bool _debugTestScript = false; |
| 14 | + |
| 15 | +Future<void> runFlutterIntegrationTest( |
| 16 | + List<String> args, { |
| 17 | + String testAppPath = 'test/test_infra/fixtures/flutter_app', |
| 18 | +}) async { |
| 19 | + final testRunnerArgs = TestArgs(args); |
| 20 | + |
| 21 | + TestFlutterApp? testApp; |
| 22 | + late String testAppUri; |
| 23 | + |
| 24 | + final bool shouldCreateTestApp = testRunnerArgs.testAppUri == null; |
| 25 | + if (shouldCreateTestApp) { |
| 26 | + // Create the test app and start it. |
| 27 | + // TODO(kenz): support running Dart CLI test apps from here too. |
| 28 | + testApp = TestFlutterApp(appPath: testAppPath); |
| 29 | + await testApp.start(); |
| 30 | + testAppUri = testApp.vmServiceUri.toString(); |
| 31 | + } else { |
| 32 | + testAppUri = testRunnerArgs.testAppUri!; |
| 33 | + } |
| 34 | + |
| 35 | + // TODO(kenz): do we need to start chromedriver in headless mode? |
| 36 | + // Start chrome driver before running the flutter integration test. |
| 37 | + final chromedriver = ChromeDriver(); |
| 38 | + await chromedriver.start(); |
| 39 | + |
| 40 | + // Run the flutter integration test. |
| 41 | + final testRunner = TestRunner(); |
| 42 | + await testRunner.run( |
| 43 | + testRunnerArgs.testTarget, |
| 44 | + enableExperiments: testRunnerArgs.enableExperiments, |
| 45 | + headless: testRunnerArgs.headless, |
| 46 | + testAppArguments: { |
| 47 | + 'service_uri': testAppUri, |
| 48 | + }, |
| 49 | + ); |
| 50 | + |
| 51 | + if (shouldCreateTestApp) { |
| 52 | + _debugLog('killing the test app'); |
| 53 | + await testApp?.killGracefully(); |
| 54 | + } |
| 55 | + |
| 56 | + _debugLog('cancelling stream subscriptions'); |
| 57 | + await testRunner.cancelAllStreamSubscriptions(); |
| 58 | + await chromedriver.cancelAllStreamSubscriptions(); |
| 59 | + |
| 60 | + _debugLog('killing the chromedriver process'); |
| 61 | + chromedriver.kill(); |
| 62 | +} |
| 63 | + |
| 64 | +class ChromeDriver with IOMixin { |
| 65 | + late final Process _process; |
| 66 | + |
| 67 | + // TODO(kenz): add error messaging if the chromedriver executable is not |
| 68 | + // found. We can also consider using web installers directly in this script: |
| 69 | + // https://github.com/flutter/flutter/wiki/Running-Flutter-Driver-tests-with-Web#web-installers-repo. |
| 70 | + Future<void> start() async { |
| 71 | + _debugLog('starting the chromedriver process'); |
| 72 | + _process = await Process.start( |
| 73 | + 'chromedriver', |
| 74 | + [ |
| 75 | + '--port=4444', |
| 76 | + ], |
| 77 | + ); |
| 78 | + listenToProcessOutput(_process); |
| 79 | + } |
| 80 | + |
| 81 | + void kill() { |
| 82 | + _process.kill(); |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +class TestRunner with IOMixin { |
| 87 | + Future<void> run( |
| 88 | + String testTarget, { |
| 89 | + bool headless = false, |
| 90 | + bool enableExperiments = false, |
| 91 | + Map<String, Object> testAppArguments = const <String, Object>{}, |
| 92 | + }) async { |
| 93 | + _debugLog('starting the flutter drive process'); |
| 94 | + final process = await Process.start( |
| 95 | + 'flutter', |
| 96 | + [ |
| 97 | + 'drive', |
| 98 | + '--profile', |
| 99 | + '--driver=test_driver/integration_test.dart', |
| 100 | + '--target=$testTarget', |
| 101 | + '-d', |
| 102 | + headless ? 'web-server' : 'chrome', |
| 103 | + if (testAppArguments.isNotEmpty) |
| 104 | + '--dart-define=test_args=${jsonEncode(testAppArguments)}', |
| 105 | + if (enableExperiments) '--dart-define=enable_experiments=true', |
| 106 | + ], |
| 107 | + ); |
| 108 | + listenToProcessOutput(process); |
| 109 | + |
| 110 | + await process.exitCode; |
| 111 | + process.kill(); |
| 112 | + _debugLog('flutter drive process has exited'); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +void _debugLog(String log) { |
| 117 | + if (_debugTestScript) { |
| 118 | + print(log); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +class TestArgs { |
| 123 | + TestArgs(List<String> args) { |
| 124 | + final argWithTestTarget = |
| 125 | + args.firstWhereOrNull((arg) => arg.startsWith(testTargetArg)); |
| 126 | + final target = argWithTestTarget?.substring(testTargetArg.length); |
| 127 | + assert( |
| 128 | + target != null, |
| 129 | + 'Please specify a test target (e.g. --target=path/to/test.dart', |
| 130 | + ); |
| 131 | + testTarget = target!; |
| 132 | + |
| 133 | + final argWithTestAppUri = |
| 134 | + args.firstWhereOrNull((arg) => arg.startsWith(testAppArg)); |
| 135 | + testAppUri = argWithTestAppUri?.substring(testAppArg.length); |
| 136 | + |
| 137 | + enableExperiments = args.contains(enableExperimentsArg); |
| 138 | + headless = args.contains(headlessArg); |
| 139 | + } |
| 140 | + |
| 141 | + static const testTargetArg = '--target='; |
| 142 | + static const testAppArg = '--test-app-uri='; |
| 143 | + static const enableExperimentsArg = '--enable-experiments'; |
| 144 | + static const headlessArg = '--headless'; |
| 145 | + |
| 146 | + late final String testTarget; |
| 147 | + late final String? testAppUri; |
| 148 | + late final bool enableExperiments; |
| 149 | + late final bool headless; |
| 150 | +} |
0 commit comments