drive.dart 13.7 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_null_migrated.dart' as globals;
24
import '../resident_runner.dart';
25
import '../runner/flutter_command.dart' show 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 69 70 71
    argParser
      ..addFlag('keep-app-running',
        defaultsTo: null,
        help: 'Will keep the Flutter application running when done testing.\n'
72
              'By default, "flutter drive" stops the application after tests are finished, '
73
              'and "--keep-app-running" overrides this. On the other hand, if "--use-existing-app" '
74
              'is specified, then "flutter drive" instead defaults to leaving the application '
75
              'running, and "--no-keep-app-running" overrides it.',
76 77
      )
      ..addOption('use-existing-app',
78 79
        help: 'Connect to an already running instance via the given observatory URL. '
              'If this option is given, the application will not be automatically started, '
80
              'and it will only be stopped if "--no-keep-app-running" is explicitly set.',
81 82 83
        valueHelp: 'url',
      )
      ..addOption('driver',
84 85 86 87 88 89
        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".',
90
        valueHelp: 'path',
91 92 93
      )
      ..addFlag('build',
        defaultsTo: true,
94 95
        help: '(deprecated) Build the app before running. To use an existing app, pass the "--use-application-binary" '
              'flag with an existing APK.',
96
      )
97 98 99 100
      ..addOption('screenshot',
        valueHelp: 'path/to/directory',
        help: 'Directory location to write screenshots on test failure.',
      )
101 102
      ..addOption('driver-port',
        defaultsTo: '4444',
103
        help: 'The port where Webdriver server is launched at.',
104 105 106 107
        valueHelp: '4444'
      )
      ..addFlag('headless',
        defaultsTo: true,
108
        help: 'Whether the driver browser is going to be launched in headless mode.',
109 110 111
      )
      ..addOption('browser-name',
        defaultsTo: 'chrome',
112
        help: 'Name of the browser where tests will be executed.',
113
        allowed: <String>[
114
          'android-chrome',
115 116 117 118 119
          'chrome',
          'edge',
          'firefox',
          'ios-safari',
          'safari',
120
        ],
121 122 123 124 125 126 127 128
        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).',
        },
129 130 131
      )
      ..addOption('browser-dimension',
        defaultsTo: '1600,1024',
132 133 134
        help: 'The dimension of the browser when running a Flutter Web test. '
              'This will affect screenshot and all offset-related actions.',
        valueHelp: 'width,height',
135 136 137
      )
      ..addFlag('android-emulator',
        defaultsTo: true,
138 139
        help: 'Whether to perform Flutter Driver testing using an Android Emulator. '
              'Works only if "browser-name" is set to "android-chrome".')
140
      ..addOption('chrome-binary',
141 142
        help: 'Location of the Chrome binary. '
              'Works only if "browser-name" is set to "chrome".')
143
      ..addOption('write-sksl-on-exit',
144 145
        help: 'Attempts to write an SkSL file when the drive process is finished '
              'to the provided file, overwriting it if necessary.')
146
      ..addMultiOption('test-arguments', help: 'Additional arguments to pass to the '
147 148 149 150
          '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
151 152
  }

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

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

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

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

  @override
176 177
  final List<String> aliases = <String>['driver'];

178
  String get userIdentifier => stringArg(FlutterOptions.kDeviceUser);
179

180 181
  String get screenshot => stringArg('screenshot');

182 183
  @override
  bool get startPausedDefault => true;
184

185 186 187
  @override
  bool get cachePubGet => false;

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

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

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

236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
    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,
        }
      );
    } else {
      final Uri uri = Uri.tryParse(stringArg('use-existing-app'));
      if (uri == null) {
        throwToolExit('Invalid VM Service URI: ${stringArg('use-existing-app')}');
257
      }
258 259 260 261 262 263 264
      await driverService.reuseApplication(
        uri,
        device,
        debuggingOptions,
        ipv6,
      );
    }
265

266 267 268 269
    final int testResult = await driverService.startTest(
      testFile,
      stringsArg('test-arguments'),
      <String, String>{},
270
      packageConfig,
271 272 273 274 275 276 277 278
      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'),
279
      profileMemory: stringArg('profile-memory'),
280
    );
281 282 283
    if (testResult != 0 && screenshot != null) {
      await takeScreenshot(device, screenshot, _fileSystem, _logger, _fsUtils);
    }
284

285 286
    if (boolArg('keep-app-running') ?? (argResults['use-existing-app'] != null)) {
      _logger.printStatus('Leaving the application running.');
287
    } else {
288 289 290 291
      final File skslFile = stringArg('write-sksl-on-exit') != null
        ? _fileSystem.file(stringArg('write-sksl-on-exit'))
        : null;
      await driverService.stop(userIdentifier: userIdentifier, writeSkslOnExit: skslFile);
yjbanov's avatar
yjbanov committed
292
    }
293
    if (testResult != 0) {
294
      throwToolExit(null);
295
    }
296
    return FlutterCommandResult.success();
yjbanov's avatar
yjbanov committed
297 298 299
  }

  String _getTestFile() {
300
    if (argResults['driver'] != null) {
301
      return stringArg('driver');
302
    }
303 304 305

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

308
    // This command extends `flutter run` and therefore CWD == package dir
309
    final String packageDir = _fileSystem.currentDirectory.path;
310 311 312

    // Make appFile path relative to package directory because we are looking
    // for the corresponding test file relative to it.
313 314 315
    if (!_fileSystem.path.isRelative(appFile)) {
      if (!_fileSystem.path.isWithin(packageDir, appFile)) {
        _logger.printError(
316 317 318 319 320
          'Application file $appFile is outside the package directory $packageDir'
        );
        return null;
      }

321
      appFile = _fileSystem.path.relative(appFile, from: packageDir);
322 323
    }

324
    final List<String> parts = _fileSystem.path.split(appFile);
325 326

    if (parts.length < 2) {
327
      _logger.printError(
328 329 330 331 332 333 334 335 336
        '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`.
337
    final String pathWithNoExtension = _fileSystem.path.withoutExtension(_fileSystem.path.joinAll(
338
      <String>[packageDir, 'test_driver', ...parts.skip(1)]));
339
    return '${pathWithNoExtension}_test${_fileSystem.path.extension(appFile)}';
340 341
  }
}
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364

@visibleForTesting
Future<void> takeScreenshot(
  Device device,
  String screenshotPath,
  FileSystem fileSystem,
  Logger logger,
  FileSystemUtils fileSystemUtils,
) async {
  try {
    final Directory outputDirectory = fileSystem.directory(screenshotPath);
    outputDirectory.createSync(recursive: true);
    final File outputFile = fileSystemUtils.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');
  }
}