web_driver.dart 13.1 KB
Newer Older
1 2 3 4 5 6
// 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';
7
import 'package:file/file.dart';
8 9
import 'package:matcher/matcher.dart';
import 'package:meta/meta.dart';
10 11

import 'package:path/path.dart' as path;
12
import 'package:vm_service/vm_service.dart' as vms;
13
import 'package:webdriver/async_io.dart' as async_io;
14 15 16 17
import 'package:webdriver/support/async.dart';

import '../common/error.dart';
import '../common/message.dart';
18 19

import 'common.dart';
20 21 22 23 24 25
import 'driver.dart';
import 'timeline.dart';

/// An implementation of the Flutter Driver using the WebDriver.
///
/// Example of how to test WebFlutterDriver:
26 27
///   1. Launch WebDriver binary: ./chromedriver --port=4444
///   2. Run test script: flutter drive --target=test_driver/scroll_perf_web.dart -d web-server --release
28 29
class WebFlutterDriver extends FlutterDriver {
  /// Creates a driver that uses a connection provided by the given
30
  /// [_connection].
31 32 33 34 35 36 37
  WebFlutterDriver.connectedTo(
    this._connection, {
    bool printCommunication = false,
    bool logCommunicationToFile = true,
  })  : _printCommunication = printCommunication,
        _logCommunicationToFile = logCommunicationToFile,
        _startTime = DateTime.now(),
38 39 40 41 42
        _driverId = _nextDriverId++
    {
      _logFilePathName = path.join(testOutputsDirectory, 'flutter_driver_commands_$_driverId.log');
    }

43 44 45

  final FlutterWebConnection _connection;
  DateTime _startTime;
46 47 48 49
  static int _nextDriverId = 0;

  /// The unique ID of this driver instance.
  final int _driverId;
50

51
  /// Start time for tracing.
52 53 54 55
  @visibleForTesting
  DateTime get startTime => _startTime;

  @override
56
  vms.Isolate get appIsolate => throw UnsupportedError('WebFlutterDriver does not support appIsolate');
57 58

  @override
59
  vms.VmService get serviceClient => throw UnsupportedError('WebFlutterDriver does not support serviceClient');
60

61 62 63
  @override
  async_io.WebDriver get webDriver => _connection._driver;

64 65 66 67 68 69
  /// Whether to print communication between host and app to `stdout`.
  final bool _printCommunication;

  /// Whether to log communication between host and app to `flutter_driver_commands.log`.
  final bool _logCommunicationToFile;

70 71 72 73 74 75
  /// Logs are written here when _logCommunicationToFile is true.
  late final String _logFilePathName;

  /// Getter for file pathname where logs are written when _logCommunicationToFile is true
  String get logFilePathName => _logFilePathName;

76 77
  /// Creates a driver that uses a connection provided by the given
  /// [hostUrl] which would fallback to environment variable VM_SERVICE_URL.
78
  /// Driver also depends on environment variables DRIVER_SESSION_ID,
79 80 81
  /// BROWSER_SUPPORTS_TIMELINE, DRIVER_SESSION_URI, DRIVER_SESSION_SPEC,
  /// DRIVER_SESSION_CAPABILITIES and ANDROID_CHROME_ON_EMULATOR for
  /// configurations.
82 83 84 85 86 87 88 89
  ///
  /// See [FlutterDriver.connect] for more documentation.
  static Future<FlutterDriver> connectWeb({
    String? hostUrl,
    bool printCommunication = false,
    bool logCommunicationToFile = true,
    Duration? timeout,
  }) async {
90 91
    hostUrl ??= Platform.environment['VM_SERVICE_URL'];
    final Map<String, dynamic> settings = <String, dynamic>{
92 93 94 95
      'support-timeline-action': Platform.environment['SUPPORT_TIMELINE_ACTION'] == 'true',
      'session-id': Platform.environment['DRIVER_SESSION_ID'],
      'session-uri': Platform.environment['DRIVER_SESSION_URI'],
      'session-spec': Platform.environment['DRIVER_SESSION_SPEC'],
96
      'android-chrome-on-emulator': Platform.environment['ANDROID_CHROME_ON_EMULATOR'] == 'true',
97
      'session-capabilities': Platform.environment['DRIVER_SESSION_CAPABILITIES'],
98 99
    };
    final FlutterWebConnection connection = await FlutterWebConnection.connect
100
      (hostUrl!, settings, timeout: timeout);
101 102 103 104 105
    return WebFlutterDriver.connectedTo(
      connection,
      printCommunication: printCommunication,
      logCommunicationToFile: logCommunicationToFile,
    );
106 107
  }

108 109 110 111 112 113 114 115
  static DriverError _createMalformedExtensionResponseError(Object? data) {
    throw DriverError(
      'Received malformed response from the FlutterDriver extension.\n'
      'Expected a JSON map containing a "response" field and, optionally, an '
      '"isError" field, but got ${data.runtimeType}: $data'
    );
  }

116 117
  @override
  Future<Map<String, dynamic>> sendCommand(Command command) async {
118 119
    final Map<String, dynamic> response;
    final Object? data;
120
    final Map<String, String> serialized = command.serialize();
121
    _logCommunication('>>> $serialized');
122
    try {
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
      data = await _connection.sendCommand("window.\$flutterDriver('${jsonEncode(serialized)}')", command.timeout);

      // The returned data is expected to be a string. If it's null or anything
      // other than a string, something's wrong.
      if (data is! String) {
        throw _createMalformedExtensionResponseError(data);
      }

      final Object? decoded = json.decode(data);
      if (decoded is! Map<String, dynamic>) {
        throw _createMalformedExtensionResponseError(data);
      } else {
        response = decoded;
      }

138
      _logCommunication('<<< $response');
139 140
    } on DriverError catch(_) {
      rethrow;
141
    } catch (error, stackTrace) {
142
      throw DriverError(
143 144
        'FlutterDriver command ${command.runtimeType} failed due to a remote error.\n'
        'Command sent: ${jsonEncode(serialized)}',
145 146
        error,
        stackTrace
147 148
      );
    }
149 150 151 152 153

    final Object? isError = response['isError'];
    final Object? responseData = response['response'];
    if (isError is! bool?) {
      throw _createMalformedExtensionResponseError(data);
154
    } else if (isError ?? false) {
155 156 157 158 159 160 161
      throw DriverError('Error in Flutter application: $responseData');
    }

    if (responseData is! Map<String, dynamic>) {
      throw _createMalformedExtensionResponseError(data);
    }
    return responseData;
162 163 164 165 166 167 168 169 170 171
  }

  @override
  Future<void> close() => _connection.close();

  @override
  Future<void> waitUntilFirstFrameRasterized() async {
    throw UnimplementedError();
  }

172 173 174 175 176
  void _logCommunication(String message) {
    if (_printCommunication) {
      driverLog('WebFlutterDriver', message);
    }
    if (_logCommunicationToFile) {
177
      final File file = fs.file(_logFilePathName);
178 179 180 181 182
      file.createSync(recursive: true); // no-op if file exists
      file.writeAsStringSync('${DateTime.now()} $message\n', mode: FileMode.append, flush: true);
    }
  }

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
  @override
  Future<List<int>> screenshot() async {
    await Future<void>.delayed(const Duration(seconds: 2));

    return _connection.screenshot();
  }

  @override
  Future<void> startTracing({
    List<TimelineStream> streams = const <TimelineStream>[TimelineStream.all],
    Duration timeout = kUnusuallyLongTimeout,
  }) async {
    _checkBrowserSupportsTimeline();
  }

  @override
  Future<Timeline> stopTracingAndDownloadTimeline({Duration timeout = kUnusuallyLongTimeout}) async {
    _checkBrowserSupportsTimeline();

    final List<Map<String, dynamic>> events = <Map<String, dynamic>>[];
203
    for (final async_io.LogEntry entry in await _connection.logs.toList()) {
204
      if (_startTime.isBefore(entry.timestamp)) {
205
        final Map<String, dynamic> data = (jsonDecode(entry.message!) as Map<String, dynamic>)['message'] as Map<String, dynamic>;
206 207 208
        if (data['method'] == 'Tracing.dataCollected') {
          // 'ts' data collected from Chrome is in double format, conversion needed
          try {
209 210
            final Map<String, dynamic> params = data['params'] as Map<String, dynamic>;
            params['ts'] = double.parse(params['ts'].toString()).toInt();
211 212 213 214
          } on FormatException catch (_) {
            // data is corrupted, skip
            continue;
          }
215
          events.add(data['params']! as Map<String, dynamic>);
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
        }
      }
    }
    final Map<String, dynamic> json = <String, dynamic>{
      'traceEvents': events,
    };
    return Timeline.fromJson(json);
  }

  @override
  Future<Timeline> traceAction(Future<dynamic> Function() action, {
    List<TimelineStream> streams = const <TimelineStream>[TimelineStream.all],
    bool retainPriorEvents = false,
  }) async {
    _checkBrowserSupportsTimeline();
    if (!retainPriorEvents) {
      await clearTimeline();
    }
    await startTracing(streams: streams);
    await action();

    return stopTracingAndDownloadTimeline();
  }

  @override
  Future<void> clearTimeline({Duration timeout = kUnusuallyLongTimeout}) async {
    _checkBrowserSupportsTimeline();

    // Reset start time
    _startTime = DateTime.now();
  }

248
  /// Checks whether browser supports Timeline related operations.
249
  void _checkBrowserSupportsTimeline() {
250
    if (!_connection.supportsTimelineAction) {
251
      throw UnsupportedError('Timeline action is not supported by current testing browser');
252 253 254 255 256 257
    }
  }
}

/// Encapsulates connection information to an instance of a Flutter Web application.
class FlutterWebConnection {
258
  /// Creates a FlutterWebConnection with WebDriver
259
  /// and whether the WebDriver supports timeline action.
260
  FlutterWebConnection(this._driver, this.supportsTimelineAction);
261

262
  final async_io.WebDriver _driver;
263

264
  /// Whether the connected WebDriver supports timeline action for Flutter Web Driver.
265
  bool supportsTimelineAction;
266

267
  /// Starts WebDriver with the given [settings] and
268 269 270 271
  /// establishes the connection to Flutter Web application.
  static Future<FlutterWebConnection> connect(
      String url,
      Map<String, dynamic> settings,
272
      {Duration? timeout}) async {
273 274 275
    final String sessionId = settings['session-id'].toString();
    final Uri sessionUri = Uri.parse(settings['session-uri'].toString());
    final async_io.WebDriver driver = async_io.WebDriver(
276 277 278 279 280 281
      sessionUri,
      sessionId,
      json.decode(settings['session-capabilities'] as String) as Map<String, dynamic>,
      async_io.AsyncIoRequestClient(sessionUri.resolve('session/$sessionId/')),
      async_io.WebDriverSpec.W3c,
    );
282 283 284 285 286 287 288
    if (settings['android-chrome-on-emulator'] == true) {
      final Uri localUri = Uri.parse(url);
      // Converts to Android Emulator Uri.
      // Hardcode the host to 10.0.2.2 based on
      // https://developer.android.com/studio/run/emulator-networking
      url = Uri(scheme: localUri.scheme, host: '10.0.2.2', port:localUri.port).toString();
    }
289
    await driver.get(url);
290

291
    await waitUntilExtensionInstalled(driver, timeout);
292
    return FlutterWebConnection(driver, settings['support-timeline-action'] as bool);
293 294
  }

295
  /// Sends command via WebDriver to Flutter web application.
296
  Future<dynamic> sendCommand(String script, Duration? duration) async {
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
    // This code should not be reachable before the VM service extension is
    // initialized. The VM service extension is expected to initialize both
    // `$flutterDriverResult` and `$flutterDriver` variables before attempting
    // to send commands. This part checks that `$flutterDriverResult` is present.
    // `$flutterDriver` is not checked because it is covered by the `script`
    // that's executed next.
    try {
      await _driver.execute(r'return $flutterDriverResult', <String>[]);
    } catch (error, stackTrace) {
      throw DriverError(
        'Driver extension has not been initialized correctly.\n'
        'If the test uses a custom VM service extension, make sure it conforms '
        'to the protocol used by package:integration_test and '
        'package:flutter_driver.\n'
        'If the test uses VM service extensions provided by the Flutter SDK, '
        'then this error is likely caused by a bug in Flutter. Please report it '
        'by filing a bug on GitHub:\n'
        '  https://github.com/flutter/flutter/issues/new?template=2_bug.md',
        error,
        stackTrace,
      );
    }

320
    String phase = 'executing';
321
    try {
322
      // Execute the script, which should leave the result in the `$flutterDriverResult` global variable.
323
      await _driver.execute(script, <void>[]);
324

325 326 327
      // Read the result.
      phase = 'reading';
      final dynamic result = await waitFor<dynamic>(
328 329 330 331
        () => _driver.execute(r'return $flutterDriverResult', <String>[]),
        matcher: isNotNull,
        timeout: duration ?? const Duration(days: 30),
      );
332 333 334 335 336 337 338 339 340 341 342

      // Reset the result to null to avoid polluting the results of future commands.
      phase = 'resetting';
      await _driver.execute(r'$flutterDriverResult = null', <void>[]);
      return result;
    } catch (error, stackTrace) {
      throw DriverError(
        'Error while $phase FlutterDriver result for command: $script',
        error,
        stackTrace,
      );
343 344 345 346
    }
  }

  /// Gets performance log from WebDriver.
347
  Stream<async_io.LogEntry> get logs => _driver.logs.get(async_io.LogType.performance);
348 349

  /// Takes screenshot via WebDriver.
350
  Future<List<int>> screenshot()  => _driver.captureScreenshotAsList();
351 352 353

  /// Closes the WebDriver.
  Future<void> close() async {
354
    await _driver.quit(closeSession: false);
355 356 357 358
  }
}

/// Waits until extension is installed.
359
Future<void> waitUntilExtensionInstalled(async_io.WebDriver driver, Duration? timeout) async {
360
  await waitFor<void>(() =>
361
      driver.execute(r'return typeof(window.$flutterDriver)', <String>[]),
362 363 364
      matcher: 'function',
      timeout: timeout ?? const Duration(days: 365));
}