From 1d08642a906664e313850ab160a9411b2e2dc65a Mon Sep 17 00:00:00 2001 From: Angjie Li <56002538+angjieli@users.noreply.github.com> Date: Fri, 10 Jan 2020 14:48:02 -0800 Subject: [PATCH] =?UTF-8?q?Create=20helper=20functions=20to=20handle=20Web?= =?UTF-8?q?Driver=20actions.=20Some=20actions=20on=20base=20=E2=80=A6=20(#?= =?UTF-8?q?48538)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../flutter_driver/lib/src/driver/driver.dart | 22 ++++---- .../lib/src/driver/web_driver.dart | 56 ++++++++++--------- 2 files changed, 41 insertions(+), 37 deletions(-) diff --git a/packages/flutter_driver/lib/src/driver/driver.dart b/packages/flutter_driver/lib/src/driver/driver.dart index cf12cab2b5..0a81556e29 100644 --- a/packages/flutter_driver/lib/src/driver/driver.dart +++ b/packages/flutter_driver/lib/src/driver/driver.dart @@ -157,10 +157,10 @@ abstract class FlutterDriver { } /// Getter of appIsolate - VMIsolate get appIsolate; + VMIsolate get appIsolate => throw UnimplementedError(); /// Getter of serviceClient - VMServiceClient get serviceClient; + VMServiceClient get serviceClient => throw UnimplementedError(); /// Sends [command] to the Flutter Driver extensions. /// This must be implemented by subclass. @@ -169,7 +169,7 @@ abstract class FlutterDriver { /// /// * [VMServiceFlutterDriver], which uses vmservice to implement. /// * [WebFlutterDriver], which uses webdriver to implement. - Future> sendCommand(Command command); + Future> sendCommand(Command command) => throw UnimplementedError(); /// Checks the status of the Flutter Driver extension. Future checkHealth({ Duration timeout }) async { @@ -561,7 +561,7 @@ abstract class FlutterDriver { /// In practice, sometimes the device gets really busy for a while and /// even two seconds isn't enough, which means that this is still racy /// and a source of flakes. - Future> screenshot(); + Future> screenshot() => throw UnimplementedError(); /// Returns the Flags set in the Dart VM as JSON. /// @@ -584,7 +584,7 @@ abstract class FlutterDriver { /// [getFlagList]: https://github.com/dart-lang/sdk/blob/master/runtime/vm/service/service.md#getflaglist /// /// Throws [UnimplementedError] on [WebFlutterDriver] instances. - Future>> getVmFlags(); + Future>> getVmFlags() => throw UnimplementedError(); /// Starts recording performance traces. /// @@ -596,7 +596,7 @@ abstract class FlutterDriver { Future startTracing({ List streams = const [TimelineStream.all], Duration timeout = kUnusuallyLongTimeout, - }); + }) => throw UnimplementedError(); /// Stops recording performance traces and downloads the timeline. /// @@ -607,7 +607,7 @@ abstract class FlutterDriver { /// For [WebFlutterDriver], this is only supported for Chrome. Future stopTracingAndDownloadTimeline({ Duration timeout = kUnusuallyLongTimeout, - }); + }) => throw UnimplementedError(); /// Runs [action] and outputs a performance trace for it. /// @@ -632,7 +632,7 @@ abstract class FlutterDriver { Future action(), { List streams = const [TimelineStream.all], bool retainPriorEvents = false, - }); + }) => throw UnimplementedError(); /// Clears all timeline events recorded up until now. /// @@ -643,7 +643,7 @@ abstract class FlutterDriver { /// For [WebFlutterDriver], this is only supported for Chrome. Future clearTimeline({ Duration timeout = kUnusuallyLongTimeout, - }); + }) => throw UnimplementedError(); /// [action] will be executed with the frame sync mechanism disabled. /// @@ -675,12 +675,12 @@ abstract class FlutterDriver { /// Force a garbage collection run in the VM. /// /// Throws [UnimplementedError] on [WebFlutterDriver] instances. - Future forceGC(); + Future forceGC() => throw UnimplementedError(); /// Closes the underlying connection to the VM service. /// /// Returns a [Future] that fires once the connection has been closed. - Future close(); + Future close() => throw UnimplementedError(); } /// Provides convenient accessors to frequently used finders. diff --git a/packages/flutter_driver/lib/src/driver/web_driver.dart b/packages/flutter_driver/lib/src/driver/web_driver.dart index ba772a3867..a11c93957c 100644 --- a/packages/flutter_driver/lib/src/driver/web_driver.dart +++ b/packages/flutter_driver/lib/src/driver/web_driver.dart @@ -88,16 +88,6 @@ class WebFlutterDriver extends FlutterDriver { @override Future close() => _connection.close(); - @override - Future forceGC() async { - throw UnimplementedError(); - } - - @override - Future>> getVmFlags() async { - throw UnimplementedError(); - } - @override Future waitUntilFirstFrameRasterized() async { throw UnimplementedError(); @@ -178,7 +168,8 @@ class WebFlutterDriver extends FlutterDriver { /// Encapsulates connection information to an instance of a Flutter Web application. class FlutterWebConnection { - FlutterWebConnection._(this._driver); + /// Creates a FlutterWebConnection with WebDriver + FlutterWebConnection(this._driver); final sync_io.WebDriver _driver; @@ -193,22 +184,10 @@ class FlutterWebConnection { final sync_io.WebDriver driver = createDriver(settings); driver.get(url); - // Configure WebDriver browser by setting its location and dimension. - final List dimensions = settings['browser-dimension'].split(',') as List; - if (dimensions.length != 2) { - throw DriverError('Invalid browser window size.'); - } - final int x = int.parse(dimensions[0]); - final int y = int.parse(dimensions[1]); - final sync_io.Window window = driver.window; - window.setLocation(const math.Point(0, 0)); - window.setSize(math.Rectangle(0, 0, x, y)); + setDriverLocationAndDimension(driver, settings); - // Wait until extension is installed. - await waitFor(() => driver.execute('return typeof(window.\$flutterDriver)', []), - matcher: 'function', - timeout: timeout ?? const Duration(days: 365)); - return FlutterWebConnection._(driver); + await waitUntilExtensionInstalled(driver, timeout); + return FlutterWebConnection(driver); } /// Sends command via WebDriver to Flutter web application @@ -248,3 +227,28 @@ class FlutterWebConnection { _driver.quit(); } } + +/// Configures the location and dimension of WebDriver. +void setDriverLocationAndDimension(sync_io.WebDriver driver, Map settings) { + final List dimensions = settings['browser-dimension'].split(',') as List; + if (dimensions.length != 2) { + throw DriverError('Invalid browser window size.'); + } + final int x = int.parse(dimensions[0]); + final int y = int.parse(dimensions[1]); + final sync_io.Window window = driver.window; + try { + window.setLocation(const math.Point(0, 0)); + window.setSize(math.Rectangle(0, 0, x, y)); + } catch (_) { + // Error might be thrown in some browsers. + } +} + +/// Waits until extension is installed. +Future waitUntilExtensionInstalled(sync_io.WebDriver driver, Duration timeout) async { + await waitFor(() => + driver.execute('return typeof(window.\$flutterDriver)', []), + matcher: 'function', + timeout: timeout ?? const Duration(days: 365)); +}