From 11549e45a386220cbacd683b8a94cbac85d62a41 Mon Sep 17 00:00:00 2001 From: Angjie Li <56002538+angjieli@users.noreply.github.com> Date: Wed, 19 Feb 2020 13:31:10 -0800 Subject: [PATCH] Use Async WebDriver for WebFlutterDriver. (#50835) --- .../lib/src/driver/web_driver.dart | 33 +++++---- .../flutter_tools/lib/src/commands/drive.dart | 70 ++++++++++++------- .../commands.shard/hermetic/drive_test.dart | 4 -- 3 files changed, 62 insertions(+), 45 deletions(-) diff --git a/packages/flutter_driver/lib/src/driver/web_driver.dart b/packages/flutter_driver/lib/src/driver/web_driver.dart index 69aeac081a..88a859eb89 100644 --- a/packages/flutter_driver/lib/src/driver/web_driver.dart +++ b/packages/flutter_driver/lib/src/driver/web_driver.dart @@ -9,7 +9,7 @@ import 'dart:io'; import 'package:matcher/matcher.dart'; import 'package:meta/meta.dart'; import 'package:vm_service_client/vm_service_client.dart'; -import 'package:webdriver/sync_io.dart' as sync_io; +import 'package:webdriver/async_io.dart' as async_io; import 'package:webdriver/support/async.dart'; import '../common/error.dart'; @@ -109,7 +109,7 @@ class WebFlutterDriver extends FlutterDriver { _checkBrowserSupportsTimeline(); final List> events = >[]; - for (final sync_io.LogEntry entry in _connection.logs) { + for (final async_io.LogEntry entry in await _connection.logs.toList()) { if (_startTime.isBefore(entry.timestamp)) { final Map data = jsonDecode(entry.message)['message'] as Map; if (data['method'] == 'Tracing.dataCollected') { @@ -168,7 +168,7 @@ class FlutterWebConnection { /// and whether the WebDriver supports timeline action FlutterWebConnection(this._driver, this._supportsTimelineAction); - final sync_io.WebDriver _driver; + final async_io.WebDriver _driver; bool _supportsTimelineAction; @@ -191,12 +191,11 @@ class FlutterWebConnection { {Duration timeout}) async { // Use sync WebDriver because async version will create a 15 seconds // overhead when quitting. - final sync_io.WebDriver driver = sync_io.fromExistingSession( + final async_io.WebDriver driver = await async_io.fromExistingSession( settings['session-id'].toString(), uri: Uri.parse(settings['session-uri'].toString()), - spec: _convertToSpec(settings['session-spec'].toString().toLowerCase()), - capabilities: jsonDecode(settings['session-capabilities'].toString()) as Map); - driver.get(url); + spec: _convertToSpec(settings['session-spec'].toString().toLowerCase())); + await driver.get(url); await waitUntilExtensionInstalled(driver, timeout); return FlutterWebConnection(driver, settings['support-timeline-action'] as bool); @@ -206,7 +205,7 @@ class FlutterWebConnection { Future sendCommand(String script, Duration duration) async { dynamic result; try { - _driver.execute(script, []); + await _driver.execute(script, []); } catch (_) { // In case there is an exception, do nothing } @@ -222,7 +221,7 @@ class FlutterWebConnection { return null; } finally { // Resets the result. - _driver.execute(r''' + await _driver.execute(r''' $flutterDriverResult = null ''', []); } @@ -230,32 +229,32 @@ class FlutterWebConnection { } /// Gets performance log from WebDriver. - List get logs => _driver.logs.get(sync_io.LogType.performance); + Stream get logs => _driver.logs.get(async_io.LogType.performance); /// Takes screenshot via WebDriver. - List screenshot() => _driver.captureScreenshotAsList(); + Future> screenshot() => _driver.captureScreenshotAsList(); /// Closes the WebDriver. Future close() async { - _driver.quit(closeSession: false); + await _driver.quit(closeSession: false); } } /// Waits until extension is installed. -Future waitUntilExtensionInstalled(sync_io.WebDriver driver, Duration timeout) async { +Future waitUntilExtensionInstalled(async_io.WebDriver driver, Duration timeout) async { await waitFor(() => driver.execute(r'return typeof(window.$flutterDriver)', []), matcher: 'function', timeout: timeout ?? const Duration(days: 365)); } -sync_io.WebDriverSpec _convertToSpec(String specString) { +async_io.WebDriverSpec _convertToSpec(String specString) { switch (specString.toLowerCase()) { case 'webdriverspec.w3c': - return sync_io.WebDriverSpec.W3c; + return async_io.WebDriverSpec.W3c; case 'webdriverspec.jsonwire': - return sync_io.WebDriverSpec.JsonWire; + return async_io.WebDriverSpec.JsonWire; default: - return sync_io.WebDriverSpec.Auto; + return async_io.WebDriverSpec.Auto; } } diff --git a/packages/flutter_tools/lib/src/commands/drive.dart b/packages/flutter_tools/lib/src/commands/drive.dart index e33f9befcf..a747b3220f 100644 --- a/packages/flutter_tools/lib/src/commands/drive.dart +++ b/packages/flutter_tools/lib/src/commands/drive.dart @@ -5,8 +5,8 @@ import 'dart:async'; import 'dart:math' as math; -import 'package:webdriver/sync_io.dart' as sync_io; import 'package:meta/meta.dart'; +import 'package:webdriver/async_io.dart' as async_io; import '../application_package.dart'; import '../base/common.dart'; @@ -146,6 +146,13 @@ class DriveCommand extends RunCommandBase { if (argResults['use-existing-app'] == null) { globals.printStatus('Starting application: $targetFile'); + if (isWebPlatform) { + throwToolExit( + 'Flutter Driver (web) does not support running without use-existing-app.\n' + 'Please launch your application beforehand and connects via use-existing-app.' + ); + } + if (getBuildInfo().isRelease && !isWebPlatform) { // This is because we need VM service to be able to drive the app. // For Flutter Web, testing in release mode is allowed. @@ -173,27 +180,46 @@ class DriveCommand extends RunCommandBase { 'VM_SERVICE_URL': observatoryUri, }; - sync_io.WebDriver driver; + async_io.WebDriver driver; // For web device, WebDriver session will be launched beforehand // so that FlutterDriver can reuse it. if (isWebPlatform) { + final Browser browser = _browserNameToEnum( + argResults['browser-name'].toString()); + final String driverPort = argResults['driver-port'].toString(); // start WebDriver - final Browser browser = _browserNameToEnum(argResults['browser-name'].toString()); - driver = _createDriver( - argResults['driver-port'].toString(), - browser, - argResults['headless'].toString() == 'true', - ); + try { + driver = await _createDriver( + driverPort, + browser, + argResults['headless'].toString() == 'true', + ); + } on Exception catch (ex) { + throwToolExit( + 'Unable to start WebDriver Session for Flutter for Web testing. \n' + 'Make sure you have the correct WebDriver Server running at $driverPort. \n' + 'Make sure the WebDriver Server matches option --browser-name. \n' + '$ex' + ); + } // set window size final List dimensions = argResults['browser-dimension'].split(',') as List; assert(dimensions.length == 2); - final int x = int.parse(dimensions[0]); - final int y = int.parse(dimensions[1]); - final sync_io.Window window = driver.window; + int x, y; try { - window.setLocation(const math.Point(0, 0)); - window.setSize(math.Rectangle(0, 0, x, y)); + x = int.parse(dimensions[0]); + y = int.parse(dimensions[1]); + } on FormatException catch (ex) { + throwToolExit(''' +Dimension provided to --browser-dimension is invalid: +$ex + '''); + } + final async_io.Window window = await driver.window; + try { + await window.setLocation(const math.Point(0, 0)); + await window.setSize(math.Rectangle(0, 0, x, y)); } catch (_) { // Error might be thrown in some browsers. } @@ -215,9 +241,9 @@ class DriveCommand extends RunCommandBase { if (error is ToolExit) { rethrow; } - throwToolExit('CAUGHT EXCEPTION: $error\n$stackTrace'); + throw Exception('Unable to run test: $error\n$stackTrace'); } finally { - driver?.quit(); + await driver?.quit(); if (boolArg('keep-app-running') ?? (argResults['use-existing-app'] != null)) { globals.printStatus('Leaving the application running.'); } else { @@ -437,11 +463,11 @@ Browser _browserNameToEnum(String browserName){ throw UnsupportedError('Browser $browserName not supported'); } -sync_io.WebDriver _createDriver(String driverPort, Browser browser, bool headless) { - return sync_io.createDriver( - uri: Uri.parse('http://localhost:$driverPort/wd/hub/'), +Future _createDriver(String driverPort, Browser browser, bool headless) async { + return async_io.createDriver( + uri: Uri.parse('http://localhost:$driverPort/'), desired: getDesiredCapabilities(browser, headless), - spec: browser != Browser.iosSafari ? sync_io.WebDriverSpec.JsonWire : sync_io.WebDriverSpec.W3c + spec: async_io.WebDriverSpec.Auto ); } @@ -453,7 +479,7 @@ Map getDesiredCapabilities(Browser browser, bool headless) { return { 'acceptInsecureCerts': true, 'browserName': 'chrome', - 'goog:loggingPrefs': { sync_io.LogType.performance: 'ALL'}, + 'goog:loggingPrefs': { async_io.LogType.performance: 'ALL'}, 'chromeOptions': { 'w3c': false, 'args': [ @@ -508,10 +534,6 @@ Map getDesiredCapabilities(Browser browser, bool headless) { case Browser.safari: return { 'browserName': 'safari', - 'safari.options': { - 'skipExtensionInstallation': true, - 'cleanSession': true - } }; break; case Browser.iosSafari: diff --git a/packages/flutter_tools/test/commands.shard/hermetic/drive_test.dart b/packages/flutter_tools/test/commands.shard/hermetic/drive_test.dart index 3801cf51a7..67415e26e5 100644 --- a/packages/flutter_tools/test/commands.shard/hermetic/drive_test.dart +++ b/packages/flutter_tools/test/commands.shard/hermetic/drive_test.dart @@ -699,10 +699,6 @@ void main() { test('macOS Safari', () { final Map expected = { 'browserName': 'safari', - 'safari.options': { - 'skipExtensionInstallation': true, - 'cleanSession': true - } }; expect(getDesiredCapabilities(Browser.safari, false), expected);