[web] define $flutterDriverResult variable early in driver test initalization (#107337)

This commit is contained in:
Yegor
2022-07-22 17:48:06 -07:00
committed by GitHub
parent 6606372a99
commit e505529344
6 changed files with 74 additions and 1 deletions

View File

@@ -294,6 +294,29 @@ class FlutterWebConnection {
/// Sends command via WebDriver to Flutter web application.
Future<dynamic> sendCommand(String script, Duration? duration) async {
// 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,
);
}
String phase = 'executing';
try {
// Execute the script, which should leave the result in the `$flutterDriverResult` global variable.

View File

@@ -18,6 +18,11 @@ import 'dart:js_util' as js_util;
///
/// * [_extension_io.dart], which has the dart:io implementation
void registerWebServiceExtension(Future<Map<String, dynamic>> Function(Map<String, String>) call) {
// Define the result variable because packages/flutter_driver/lib/src/driver/web_driver.dart
// checks for this value to become non-null when waiting for the result. If this value is
// undefined at the time of the check, WebDriver throws an exception.
context[r'$flutterDriverResult'] = null;
js_util.setProperty(html.window, r'$flutterDriver', allowInterop((dynamic message) async {
final Map<String, String> params = Map<String, String>.from(
jsonDecode(message as String) as Map<String, dynamic>);

View File

@@ -20,7 +20,12 @@ void main() {
test('web_extension should register a function', () {
expect(() => registerWebServiceExtension(call),
returnsNormally);
expect(js.context.hasProperty(r'$flutterDriver'), true);
expect(js.context[r'$flutterDriver'], isNotNull);
expect(js.context.hasProperty(r'$flutterDriverResult'), true);
expect(js.context[r'$flutterDriverResult'], isNull);
});
});
}