drive.dart 14.3 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
yjbanov's avatar
yjbanov committed
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
// @dart = 2.8

yjbanov's avatar
yjbanov committed
7
import 'dart:async';
8 9

import 'package:meta/meta.dart';
10
import 'package:package_config/package_config_types.dart';
yjbanov's avatar
yjbanov committed
11

12
import '../android/android_device.dart';
13
import '../application_package.dart';
14
import '../artifacts.dart';
Devon Carew's avatar
Devon Carew committed
15
import '../base/common.dart';
16
import '../base/file_system.dart';
17
import '../base/logger.dart';
18
import '../base/platform.dart';
19
import '../build_info.dart';
20
import '../dart/package_map.dart';
21
import '../device.dart';
22
import '../drive/drive_service.dart';
23
import '../globals.dart' as globals;
24
import '../resident_runner.dart';
25
import '../runner/flutter_command.dart' show FlutterCommandCategory, FlutterCommandResult, FlutterOptions;
26
import '../web/web_device.dart';
yjbanov's avatar
yjbanov committed
27 28 29 30 31 32 33 34 35 36 37
import 'run.dart';

/// Runs integration (a.k.a. end-to-end) tests.
///
/// An integration test is a program that runs in a separate process from your
/// Flutter application. It connects to the application and acts like a user,
/// performing taps, scrolls, reading out widget properties and verifying their
/// correctness.
///
/// This command takes a target Flutter application that you would like to test
/// as the `--target` option (defaults to `lib/main.dart`). It then looks for a
38 39
/// corresponding test file within the `test_driver` directory. The test file is
/// expected to have the same name but contain the `_test.dart` suffix. The
40
/// `_test.dart` file would generally be a Dart program that uses
41
/// `package:flutter_driver` and exercises your application. Most commonly it
yjbanov's avatar
yjbanov committed
42 43 44 45 46 47 48
/// is a test written using `package:test`, but you are free to use something
/// else.
///
/// The app and the test are launched simultaneously. Once the test completes
/// the application is stopped and the command exits. If all these steps are
/// successful the exit code will be `0`. Otherwise, you will see a non-zero
/// exit code.
49
class DriveCommand extends RunCommandBase {
50 51
  DriveCommand({
    bool verboseHelp = false,
52 53 54
    @visibleForTesting FlutterDriverFactory flutterDriverFactory,
    @required FileSystem fileSystem,
    @required Logger logger,
55
    @required Platform platform,
56 57
  }) : _flutterDriverFactory = flutterDriverFactory,
       _fileSystem = fileSystem,
58
       _logger = logger,
59
       _fsUtils = FileSystemUtils(fileSystem: fileSystem, platform: platform),
60
       super(verboseHelp: verboseHelp) {
61
    requiresPubspecYaml();
62
    addEnableExperimentation(hide: !verboseHelp);
63 64 65 66 67

    // By default, the drive app should not publish the VM service port over mDNS
    // to prevent a local network permission dialog on iOS 14+,
    // which cannot be accepted or dismissed in a CI environment.
    addPublishPort(enabledByDefault: false, verboseHelp: verboseHelp);
68
    addMultidexOption();
69 70 71 72
    argParser
      ..addFlag('keep-app-running',
        defaultsTo: null,
        help: 'Will keep the Flutter application running when done testing.\n'
73
              'By default, "flutter drive" stops the application after tests are finished, '
74
              'and "--keep-app-running" overrides this. On the other hand, if "--use-existing-app" '
75
              'is specified, then "flutter drive" instead defaults to leaving the application '
76
              'running, and "--no-keep-app-running" overrides it.',
77 78
      )
      ..addOption('use-existing-app',
79 80
        help: 'Connect to an already running instance via the given observatory URL. '
              'If this option is given, the application will not be automatically started, '
81
              'and it will only be stopped if "--no-keep-app-running" is explicitly set.',
82 83 84
        valueHelp: 'url',
      )
      ..addOption('driver',
85 86 87 88 89 90
        help: 'The test file to run on the host (as opposed to the target file to run on '
              'the device).\n'
              'By default, this file has the same base name as the target file, but in the '
              '"test_driver/" directory instead, and with "_test" inserted just before the '
              'extension, so e.g. if the target is "lib/main.dart", the driver will be '
              '"test_driver/main_test.dart".',
91
        valueHelp: 'path',
92 93 94
      )
      ..addFlag('build',
        defaultsTo: true,
95 96
        help: '(deprecated) Build the app before running. To use an existing app, pass the "--use-application-binary" '
              'flag with an existing APK.',
97
      )
98 99 100 101
      ..addOption('screenshot',
        valueHelp: 'path/to/directory',
        help: 'Directory location to write screenshots on test failure.',
      )
102 103
      ..addOption('driver-port',
        defaultsTo: '4444',
104
        help: 'The port where Webdriver server is launched at.',
105 106 107 108
        valueHelp: '4444'
      )
      ..addFlag('headless',
        defaultsTo: true,
109
        help: 'Whether the driver browser is going to be launched in headless mode.',
110 111 112
      )
      ..addOption('browser-name',
        defaultsTo: 'chrome',
113
        help: 'Name of the browser where tests will be executed.',
114
        allowed: <String>[
115
          'android-chrome',
116 117 118 119 120
          'chrome',
          'edge',
          'firefox',
          'ios-safari',
          'safari',
121
        ],
122 123 124 125 126 127 128 129
        allowedHelp: <String, String>{
          'android-chrome': 'Chrome on Android (see also "--android-emulator").',
          'chrome': 'Google Chrome on this computer (see also "--chrome-binary").',
          'edge': 'Microsoft Edge on this computer (Windows only).',
          'firefox': 'Mozilla Firefox on this computer.',
          'ios-safari': 'Apple Safari on an iOS device.',
          'safari': 'Apple Safari on this computer (macOS only).',
        },
130 131 132
      )
      ..addOption('browser-dimension',
        defaultsTo: '1600,1024',
133 134 135
        help: 'The dimension of the browser when running a Flutter Web test. '
              'This will affect screenshot and all offset-related actions.',
        valueHelp: 'width,height',
136 137 138
      )
      ..addFlag('android-emulator',
        defaultsTo: true,
139 140
        help: 'Whether to perform Flutter Driver testing using an Android Emulator. '
              'Works only if "browser-name" is set to "android-chrome".')
141
      ..addOption('chrome-binary',
142 143
        help: 'Location of the Chrome binary. '
              'Works only if "browser-name" is set to "chrome".')
144
      ..addOption('write-sksl-on-exit',
145 146
        help: 'Attempts to write an SkSL file when the drive process is finished '
              'to the provided file, overwriting it if necessary.')
147
      ..addMultiOption('test-arguments', help: 'Additional arguments to pass to the '
148 149 150 151
          'Dart VM running The test script.')
      ..addOption('profile-memory', help: 'Launch devtools and profile application memory, writing '
          'The output data to the file path provided to this argument as JSON.',
          valueHelp: 'profile_memory.json');
yjbanov's avatar
yjbanov committed
152 153
  }

154
  // `pub` must always be run due to the test script running from source,
155 156
  // even if an application binary is used. Default to true unless the user explicitly
  // specified not to.
157
  @override
158 159 160 161 162 163
  bool get shouldRunPub {
    if (argResults.wasParsed('pub') && !boolArg('pub')) {
      return false;
    }
    return true;
  }
164

165 166 167
  FlutterDriverFactory _flutterDriverFactory;
  final FileSystem _fileSystem;
  final Logger _logger;
168
  final FileSystemUtils _fsUtils;
169

170
  @override
171
  final String name = 'drive';
172 173

  @override
174
  final String description = 'Run integration tests for the project on an attached device or emulator.';
175

176 177 178
  @override
  String get category => FlutterCommandCategory.project;

179
  @override
180 181
  final List<String> aliases = <String>['driver'];

182
  String get userIdentifier => stringArg(FlutterOptions.kDeviceUser);
183

184 185
  String get screenshot => stringArg('screenshot');

186 187
  @override
  bool get startPausedDefault => true;
188

189 190 191
  @override
  bool get cachePubGet => false;

192 193 194
  @override
  Future<void> validateCommand() async {
    if (userIdentifier != null) {
195
      final Device device = await findTargetDevice();
196 197 198 199 200 201 202
      if (device is! AndroidDevice) {
        throwToolExit('--${FlutterOptions.kDeviceUser} is only supported for Android');
      }
    }
    return super.validateCommand();
  }

203
  @override
204
  Future<FlutterCommandResult> runCommand() async {
205
    final String testFile = _getTestFile();
206
    if (testFile == null) {
207
      throwToolExit(null);
208
    }
209 210 211 212
    if (await _fileSystem.type(testFile) != FileSystemEntityType.file) {
      throwToolExit('Test file not found: $testFile');
    }
    final Device device = await findTargetDevice(includeUnsupportedDevices: stringArg('use-application-binary') == null);
213
    if (device == null) {
214
      throwToolExit(null);
215
    }
216
    if (screenshot != null && !device.supportsScreenshot) {
217
      _logger.printError('Screenshot not supported for ${device.name}.');
218
    }
219

220 221 222 223 224
    final bool web = device is WebServerDevice || device is ChromiumDevice;
    _flutterDriverFactory ??= FlutterDriverFactory(
      applicationPackageFactory: ApplicationPackageFactory.instance,
      logger: _logger,
      processUtils: globals.processUtils,
225
      dartSdkPath: globals.artifacts.getHostArtifact(HostArtifact.engineDartBinary).path,
226
      devtoolsLauncher: DevtoolsLauncher.instance,
227 228
    );
    final PackageConfig packageConfig = await loadPackageConfigWithLogging(
229
      _fileSystem.file('.packages'),
230 231 232
      logger: _logger,
      throwOnError: false,
    ) ?? PackageConfig.empty;
233
    final DriverService driverService = _flutterDriverFactory.createDriverService(web);
234
    final BuildInfo buildInfo = await getBuildInfo();
235
    final DebuggingOptions debuggingOptions = await createDebuggingOptions(web);
236 237
    final File applicationBinary = stringArg('use-application-binary') == null
      ? null
238
      : _fileSystem.file(stringArg('use-application-binary'));
239

240
    bool screenshotTaken = false;
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
    try {
      if (stringArg('use-existing-app') == null) {
        await driverService.start(
          buildInfo,
          device,
          debuggingOptions,
          ipv6,
          applicationBinary: applicationBinary,
          route: route,
          userIdentifier: userIdentifier,
          mainPath: targetFile,
          platformArgs: <String, Object>{
            if (traceStartup)
              'trace-startup': traceStartup,
            if (web)
              '--no-launch-chrome': true,
            if (boolArg('multidex'))
              'multidex': true,
          }
        );
      } else {
        final Uri uri = Uri.tryParse(stringArg('use-existing-app'));
        if (uri == null) {
          throwToolExit('Invalid VM Service URI: ${stringArg('use-existing-app')}');
265
        }
266 267 268 269 270 271
        await driverService.reuseApplication(
          uri,
          device,
          debuggingOptions,
          ipv6,
        );
272
      }
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287

      final int testResult = await driverService.startTest(
        testFile,
        stringsArg('test-arguments'),
        <String, String>{},
        packageConfig,
        chromeBinary: stringArg('chrome-binary'),
        headless: boolArg('headless'),
        browserDimension: stringArg('browser-dimension').split(','),
        browserName: stringArg('browser-name'),
        driverPort: stringArg('driver-port') != null
          ? int.tryParse(stringArg('driver-port'))
          : null,
        androidEmulator: boolArg('android-emulator'),
        profileMemory: stringArg('profile-memory'),
288
      );
289 290 291 292 293
      if (testResult != 0 && screenshot != null) {
        // Take a screenshot while the app is still running.
        await _takeScreenshot(device);
        screenshotTaken = true;
      }
294

295 296 297 298 299 300 301 302 303 304 305 306
      if (boolArg('keep-app-running') ?? (argResults['use-existing-app'] != null)) {
        _logger.printStatus('Leaving the application running.');
      } else {
        final File skslFile = stringArg('write-sksl-on-exit') != null
          ? _fileSystem.file(stringArg('write-sksl-on-exit'))
          : null;
        await driverService.stop(userIdentifier: userIdentifier, writeSkslOnExit: skslFile);
      }
      if (testResult != 0) {
        throwToolExit(null);
      }
    } on Exception catch(_) {
307 308 309
      // On exceptions, including ToolExit, take a screenshot on the device
      // unless a screenshot was already taken on test failure.
      if (!screenshotTaken && screenshot != null) {
310 311 312
        await _takeScreenshot(device);
      }
      rethrow;
313
    }
314

315
    return FlutterCommandResult.success();
yjbanov's avatar
yjbanov committed
316 317 318
  }

  String _getTestFile() {
319
    if (argResults['driver'] != null) {
320
      return stringArg('driver');
321
    }
322 323 324

    // If the --driver argument wasn't provided, then derive the value from
    // the target file.
325
    String appFile = _fileSystem.path.normalize(targetFile);
326

327
    // This command extends `flutter run` and therefore CWD == package dir
328
    final String packageDir = _fileSystem.currentDirectory.path;
329 330 331

    // Make appFile path relative to package directory because we are looking
    // for the corresponding test file relative to it.
332 333 334
    if (!_fileSystem.path.isRelative(appFile)) {
      if (!_fileSystem.path.isWithin(packageDir, appFile)) {
        _logger.printError(
335 336 337 338 339
          'Application file $appFile is outside the package directory $packageDir'
        );
        return null;
      }

340
      appFile = _fileSystem.path.relative(appFile, from: packageDir);
341 342
    }

343
    final List<String> parts = _fileSystem.path.split(appFile);
344 345

    if (parts.length < 2) {
346
      _logger.printError(
347 348 349 350 351 352 353 354 355
        'Application file $appFile must reside in one of the sub-directories '
        'of the package structure, not in the root directory.'
      );
      return null;
    }

    // Look for the test file inside `test_driver/` matching the sub-path, e.g.
    // if the application is `lib/foo/bar.dart`, the test file is expected to
    // be `test_driver/foo/bar_test.dart`.
356
    final String pathWithNoExtension = _fileSystem.path.withoutExtension(_fileSystem.path.joinAll(
357
      <String>[packageDir, 'test_driver', ...parts.skip(1)]));
358
    return '${pathWithNoExtension}_test${_fileSystem.path.extension(appFile)}';
359
  }
360

361
  Future<void> _takeScreenshot(Device device) async {
362 363 364
    if (!device.supportsScreenshot) {
      return;
    }
365 366 367 368 369 370 371 372 373 374 375 376 377
    try {
      final Directory outputDirectory = _fileSystem.directory(screenshot)
        ..createSync(recursive: true);
      final File outputFile = _fsUtils.getUniqueFile(
        outputDirectory,
        'drive',
        'png',
      );
      await device.takeScreenshot(outputFile);
      _logger.printStatus('Screenshot written to ${outputFile.path}');
    } on Exception catch (error) {
      _logger.printError('Error taking screenshot: $error');
    }
378 379
  }
}