Flutter Web Driver Support (#47890)

This commit is contained in:
Angjie Li
2020-01-02 19:48:01 -08:00
committed by Flutter GitHub Bot
parent 57a93419b4
commit 9df1790f24
43 changed files with 2023 additions and 703 deletions

View File

@@ -3,6 +3,7 @@
// found in the LICENSE file.
import 'dart:async';
import 'dart:convert';
import 'package:flutter_driver/src/common/error.dart';
import 'package:flutter_driver/src/common/health.dart';
@@ -20,6 +21,8 @@ import 'common.dart';
/// Magical timeout value that's different from the default.
const Duration _kTestTimeout = Duration(milliseconds: 1234);
const String _kSerializedTestTimeout = '1234';
const String _kWebScriptPrefix = 'window.\$flutterDriver(\'';
const String _kWebScriptSuffix = '\')';
void main() {
final List<String> log = <String>[];
@@ -27,7 +30,7 @@ void main() {
log.add('$source: $message');
};
group('FlutterDriver.connect', () {
group('VMServiceFlutterDriver.connect', () {
MockVMServiceClient mockClient;
MockVM mockVM;
MockIsolate mockIsolate;
@@ -115,17 +118,17 @@ void main() {
});
});
group('FlutterDriver', () {
group('VMServiceFlutterDriver', () {
MockVMServiceClient mockClient;
MockPeer mockPeer;
MockIsolate mockIsolate;
FlutterDriver driver;
VMServiceFlutterDriver driver;
setUp(() {
mockClient = MockVMServiceClient();
mockPeer = MockPeer();
mockIsolate = MockIsolate();
driver = FlutterDriver.connectedTo(mockClient, mockPeer, mockIsolate);
driver = VMServiceFlutterDriver.connectedTo(mockClient, mockPeer, mockIsolate);
});
test('checks the health of the driver extension', () async {
@@ -615,7 +618,7 @@ void main() {
expect(log, <String>[]);
time.elapse(kUnusuallyLongTimeout);
});
expect(log, <String>['FlutterDriver: waitFor message is taking a long time to complete...']);
expect(log, <String>['VMServiceFlutterDriver: waitFor message is taking a long time to complete...']);
});
test('local custom timeout', () async {
@@ -630,7 +633,7 @@ void main() {
expect(log, <String>[]);
time.elapse(customTimeout);
});
expect(log, <String>['FlutterDriver: waitFor message is taking a long time to complete...']);
expect(log, <String>['VMServiceFlutterDriver: waitFor message is taking a long time to complete...']);
});
test('remote error', () async {
@@ -650,17 +653,17 @@ void main() {
});
});
group('FlutterDriver with custom timeout', () {
group('VMServiceFlutterDriver with custom timeout', () {
MockVMServiceClient mockClient;
MockPeer mockPeer;
MockIsolate mockIsolate;
FlutterDriver driver;
VMServiceFlutterDriver driver;
setUp(() {
mockClient = MockVMServiceClient();
mockPeer = MockPeer();
mockIsolate = MockIsolate();
driver = FlutterDriver.connectedTo(mockClient, mockPeer, mockIsolate);
driver = VMServiceFlutterDriver.connectedTo(mockClient, mockPeer, mockIsolate);
});
test('GetHealth has no default timeout', () async {
@@ -684,6 +687,378 @@ void main() {
await driver.checkHealth(timeout: _kTestTimeout);
});
});
group('WebFlutterDriver', () {
MockFlutterWebConnection mockConnection;
WebFlutterDriver driver;
setUp(() {
mockConnection = MockFlutterWebConnection();
driver = WebFlutterDriver.connectedTo(mockConnection, Browser.chrome);
});
test('closes connection', () async {
when(mockConnection.close()).thenAnswer((Invocation invocation) => Future<dynamic>.value(null));
await driver.close();
});
group('ByValueKey', () {
test('restricts value types', () async {
expect(() => find.byValueKey(null),
throwsA(isInstanceOf<DriverError>()));
});
test('finds by ValueKey', () async {
when(mockConnection.sendCommand(any, any)).thenAnswer((Invocation i) async {
final String script = _checkAndEncode(i.positionalArguments[0]);
expect(Map<String, String>.from(jsonDecode(script) as Map<String, dynamic>), <String, String>{
'command': 'tap',
'timeout': _kSerializedTestTimeout,
'finderType': 'ByValueKey',
'keyValueString': 'foo',
'keyValueType': 'String',
});
return jsonEncode(await makeMockResponse(<String, dynamic>{}));
});
await driver.tap(find.byValueKey('foo'), timeout: _kTestTimeout);
});
});
group('BySemanticsLabel', () {
test('finds by Semantic label using String', () async {
when(mockConnection.sendCommand(any, any)).thenAnswer((Invocation i) async {
final String script = _checkAndEncode(i.positionalArguments[0]);
expect(Map<String, String>.from(jsonDecode(script) as Map<String, dynamic>), <String, String>{
'command': 'tap',
'timeout': _kSerializedTestTimeout,
'finderType': 'BySemanticsLabel',
'label': 'foo',
});
return jsonEncode(await makeMockResponse(<String, dynamic>{}));
});
await driver.tap(find.bySemanticsLabel('foo'), timeout: _kTestTimeout);
});
test('finds by Semantic label using RegExp', () async {
when(mockConnection.sendCommand(any, any)).thenAnswer((Invocation i) async {
final String script = _checkAndEncode(i.positionalArguments[0]);
expect(Map<String, String>.from(jsonDecode(script) as Map<String, dynamic>), <String, String>{
'command': 'tap',
'timeout': _kSerializedTestTimeout,
'finderType': 'BySemanticsLabel',
'label': '^foo',
'isRegExp': 'true',
});
return jsonEncode(await makeMockResponse(<String, dynamic>{}));
});
await driver.tap(find.bySemanticsLabel(RegExp('^foo')), timeout: _kTestTimeout);
});
});
group('tap', () {
test('requires a target reference', () async {
expect(driver.tap(null), throwsA(isInstanceOf<DriverError>()));
});
test('sends the tap command', () async {
when(mockConnection.sendCommand(any, any)).thenAnswer((Invocation i) async {
final String script = _checkAndEncode(i.positionalArguments[0]);
expect(Map<String, String>.from(jsonDecode(script) as Map<String, dynamic>), <String, String>{
'command': 'tap',
'timeout': _kSerializedTestTimeout,
'finderType': 'ByText',
'text': 'foo',
});
return jsonEncode(await makeMockResponse(<String, dynamic>{}));
});
await driver.tap(find.text('foo'), timeout: _kTestTimeout);
});
});
group('getText', () {
test('requires a target reference', () async {
expect(driver.getText(null), throwsA(isInstanceOf<DriverError>()));
});
test('sends the getText command', () async {
when(mockConnection.sendCommand(any, any)).thenAnswer((Invocation i) async {
final String script = _checkAndEncode(i.positionalArguments[0]);
expect(Map<String, String>.from(jsonDecode(script) as Map<String, dynamic>), <String, String>{
'command': 'get_text',
'timeout': _kSerializedTestTimeout,
'finderType': 'ByValueKey',
'keyValueString': '123',
'keyValueType': 'int',
});
return jsonEncode(await makeMockResponse(<String, String>{
'text': 'hello',
}));
});
final String result = await driver.getText(find.byValueKey(123), timeout: _kTestTimeout);
expect(result, 'hello');
});
});
group('waitFor', () {
test('requires a target reference', () async {
expect(driver.waitFor(null), throwsA(isInstanceOf<DriverError>()));
});
test('sends the waitFor command', () async {
when(mockConnection.sendCommand(any, any)).thenAnswer((Invocation i) async {
final String script = _checkAndEncode(i.positionalArguments[0]);
expect(Map<String, String>.from(jsonDecode(script) as Map<String, dynamic>), <String, String>{
'command': 'waitFor',
'finderType': 'ByTooltipMessage',
'text': 'foo',
'timeout': _kSerializedTestTimeout,
});
return jsonEncode(await makeMockResponse(<String, dynamic>{}));
});
await driver.waitFor(find.byTooltip('foo'), timeout: _kTestTimeout);
});
});
group('waitForCondition', () {
test('sends the wait for NoPendingFrameCondition command', () async {
when(mockConnection.sendCommand(any, any)).thenAnswer((Invocation i) async {
final String script = _checkAndEncode(i.positionalArguments[0]);
expect(Map<String, String>.from(jsonDecode(script) as Map<String, dynamic>), <String, String>{
'command': 'waitForCondition',
'timeout': _kSerializedTestTimeout,
'conditionName': 'NoPendingFrameCondition',
});
return jsonEncode(await makeMockResponse(<String, dynamic>{}));
});
await driver.waitForCondition(const NoPendingFrame(), timeout: _kTestTimeout);
});
test('sends the wait for NoPendingPlatformMessages command', () async {
when(mockConnection.sendCommand(any, any)).thenAnswer((Invocation i) async {
final String script = _checkAndEncode(i.positionalArguments[0]);
expect(Map<String, String>.from(jsonDecode(script) as Map<String, dynamic>), <String, String>{
'command': 'waitForCondition',
'timeout': _kSerializedTestTimeout,
'conditionName': 'NoPendingPlatformMessagesCondition',
});
return jsonEncode(await makeMockResponse(<String, dynamic>{}));
});
await driver.waitForCondition(const NoPendingPlatformMessages(), timeout: _kTestTimeout);
});
test('sends the waitForCondition of combined conditions command', () async {
when(mockConnection.sendCommand(any, any)).thenAnswer((Invocation i) async {
final String script = _checkAndEncode(i.positionalArguments[0]);
expect(Map<String, String>.from(jsonDecode(script) as Map<String, dynamic>), <String, String>{
'command': 'waitForCondition',
'timeout': _kSerializedTestTimeout,
'conditionName': 'CombinedCondition',
'conditions': '[{"conditionName":"NoPendingFrameCondition"},{"conditionName":"NoTransientCallbacksCondition"}]',
});
return jsonEncode(await makeMockResponse(<String, dynamic>{}));
});
const SerializableWaitCondition combinedCondition =
CombinedCondition(<SerializableWaitCondition>[NoPendingFrame(), NoTransientCallbacks()]);
await driver.waitForCondition(combinedCondition, timeout: _kTestTimeout);
});
});
group('waitUntilNoTransientCallbacks', () {
test('sends the waitUntilNoTransientCallbacks command', () async {
when(mockConnection.sendCommand(any, any)).thenAnswer((Invocation i) async {
final String script = _checkAndEncode(i.positionalArguments[0]);
expect(Map<String, String>.from(jsonDecode(script) as Map<String, dynamic>), <String, String>{
'command': 'waitForCondition',
'timeout': _kSerializedTestTimeout,
'conditionName': 'NoTransientCallbacksCondition',
});
return jsonEncode(await makeMockResponse(<String, dynamic>{}));
});
await driver.waitUntilNoTransientCallbacks(timeout: _kTestTimeout);
});
});
group('getOffset', () {
test('requires a target reference', () async {
expect(driver.getCenter(null), throwsA(isInstanceOf<DriverError>()));
expect(driver.getTopLeft(null), throwsA(isInstanceOf<DriverError>()));
expect(driver.getTopRight(null), throwsA(isInstanceOf<DriverError>()));
expect(driver.getBottomLeft(null), throwsA(isInstanceOf<DriverError>()));
expect(driver.getBottomRight(null), throwsA(isInstanceOf<DriverError>()));
});
test('sends the getCenter command', () async {
when(mockConnection.sendCommand(any, any)).thenAnswer((Invocation i) async {
final String script = _checkAndEncode(i.positionalArguments[0]);
expect(Map<String, String>.from(jsonDecode(script) as Map<String, dynamic>), <String, String>{
'command': 'get_offset',
'offsetType': 'center',
'timeout': _kSerializedTestTimeout,
'finderType': 'ByValueKey',
'keyValueString': '123',
'keyValueType': 'int',
});
return jsonEncode(await makeMockResponse(<String, double>{
'dx': 11,
'dy': 12,
}));
});
final DriverOffset result = await driver.getCenter(find.byValueKey(123), timeout: _kTestTimeout);
expect(result, const DriverOffset(11, 12));
});
test('sends the getTopLeft command', () async {
when(mockConnection.sendCommand(any, any)).thenAnswer((Invocation i) async {
final String script = _checkAndEncode(i.positionalArguments[0]);
expect(Map<String, String>.from(jsonDecode(script) as Map<String, dynamic>), <String, String>{
'command': 'get_offset',
'offsetType': 'topLeft',
'timeout': _kSerializedTestTimeout,
'finderType': 'ByValueKey',
'keyValueString': '123',
'keyValueType': 'int',
});
return jsonEncode(await makeMockResponse(<String, double>{
'dx': 11,
'dy': 12,
}));
});
final DriverOffset result = await driver.getTopLeft(find.byValueKey(123), timeout: _kTestTimeout);
expect(result, const DriverOffset(11, 12));
});
test('sends the getTopRight command', () async {
when(mockConnection.sendCommand(any, any)).thenAnswer((Invocation i) async {
final String script = _checkAndEncode(i.positionalArguments[0]);
expect(Map<String, String>.from(jsonDecode(script) as Map<String, dynamic>), <String, String>{
'command': 'get_offset',
'offsetType': 'topRight',
'timeout': _kSerializedTestTimeout,
'finderType': 'ByValueKey',
'keyValueString': '123',
'keyValueType': 'int',
});
return jsonEncode(await makeMockResponse(<String, double>{
'dx': 11,
'dy': 12,
}));
});
final DriverOffset result = await driver.getTopRight(find.byValueKey(123), timeout: _kTestTimeout);
expect(result, const DriverOffset(11, 12));
});
test('sends the getBottomLeft command', () async {
when(mockConnection.sendCommand(any, any)).thenAnswer((Invocation i) async {
final String script = _checkAndEncode(i.positionalArguments[0]);
expect(Map<String, String>.from(jsonDecode(script) as Map<String, dynamic>), <String, String>{
'command': 'get_offset',
'offsetType': 'bottomLeft',
'timeout': _kSerializedTestTimeout,
'finderType': 'ByValueKey',
'keyValueString': '123',
'keyValueType': 'int',
});
return jsonEncode(await makeMockResponse(<String, double>{
'dx': 11,
'dy': 12,
}));
});
final DriverOffset result = await driver.getBottomLeft(find.byValueKey(123), timeout: _kTestTimeout);
expect(result, const DriverOffset(11, 12));
});
test('sends the getBottomRight command', () async {
when(mockConnection.sendCommand(any, any)).thenAnswer((Invocation i) async {
final String script = _checkAndEncode(i.positionalArguments[0]);
expect(Map<String, String>.from(jsonDecode(script) as Map<String, dynamic>), <String, String>{
'command': 'get_offset',
'offsetType': 'bottomRight',
'timeout': _kSerializedTestTimeout,
'finderType': 'ByValueKey',
'keyValueString': '123',
'keyValueType': 'int',
});
return jsonEncode(await makeMockResponse(<String, double>{
'dx': 11,
'dy': 12,
}));
});
final DriverOffset result = await driver.getBottomRight(find.byValueKey(123), timeout: _kTestTimeout);
expect(result, const DriverOffset(11, 12));
});
});
test('checks the health of the driver extension', () async {
when(mockConnection.sendCommand(any, any)).thenAnswer((Invocation i) async {
final String script = _checkAndEncode(i.positionalArguments[0]);
expect(Map<String, String>.from(jsonDecode(script) as Map<String, dynamic>), <String, String>{
'command': 'get_health',
});
return jsonEncode(await makeMockResponse(<String, dynamic>{'status': 'ok'}));
});
await driver.checkHealth();
});
group('WebFlutterDriver Unimplemented error', () {
test('forceGC', () async {
expect(driver.forceGC(),
throwsA(isInstanceOf<UnimplementedError>()));
});
test('getVmFlags', () async {
expect(driver.getVmFlags(),
throwsA(isInstanceOf<UnimplementedError>()));
});
test('waitUntilFirstFrameRasterized', () async {
expect(driver.waitUntilFirstFrameRasterized(),
throwsA(isInstanceOf<UnimplementedError>()));
});
test('appIsoloate', () async {
expect(() => driver.appIsolate.invokeExtension('abc', <String, String>{'abc': '123'}),
throwsA(isInstanceOf<UnsupportedError>()));
});
test('serviceClient', () async {
expect(() => driver.serviceClient.getVM(),
throwsA(isInstanceOf<UnsupportedError>()));
});
});
});
group('WebFlutterDriver with non-chrome browser', () {
MockFlutterWebConnection mockConnection;
WebFlutterDriver driver;
setUp(() {
mockConnection = MockFlutterWebConnection();
driver = WebFlutterDriver.connectedTo(mockConnection, Browser.edge);
});
test('tracing', () async {
expect(driver.traceAction(() async { return Future<dynamic>.value(); }),
throwsA(isInstanceOf<UnimplementedError>()));
expect(driver.startTracing(),
throwsA(isInstanceOf<UnimplementedError>()));
expect(driver.stopTracingAndDownloadTimeline(),
throwsA(isInstanceOf<UnimplementedError>()));
expect(driver.clearTimeline(),
throwsA(isInstanceOf<UnimplementedError>()));
});
});
}
/// This function will verify the format of the script
/// and return the actual script.
/// script will be in the following format:
// window.flutterDriver('[actual script]')
String _checkAndEncode(dynamic script) {
expect(script is String, isTrue);
expect(script.startsWith(_kWebScriptPrefix), isTrue);
expect(script.endsWith(_kWebScriptSuffix), isTrue);
// Strip prefix and suffix
return script.substring(_kWebScriptPrefix.length, script.length - 2) as String;
}
Future<Map<String, dynamic>> makeMockResponse(
@@ -708,6 +1083,8 @@ class MockVMPauseBreakpointEvent extends Mock implements VMPauseBreakpointEvent
class MockVMResumeEvent extends Mock implements VMResumeEvent { }
class MockFlutterWebConnection extends Mock implements FlutterWebConnection { }
class MockPeer extends Mock implements rpc.Peer {
@override
bool get isClosed => false;

View File

@@ -4,7 +4,7 @@
import 'package:flutter_driver/src/common/find.dart';
import '../common.dart';
import '../../common.dart';
void main() {
test('Ancestor finder serialize', () {

View File

@@ -0,0 +1,24 @@
// 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 'package:flutter_driver/src/extension/_io_extension.dart';
import '../../common.dart';
void main() {
group('test io_extension',() {
Future<Map<String, dynamic>> Function(Map<String, String>) call;
setUp(() {
call = (Map<String, String> args) async {
return Future<Map<String, dynamic>>.value(args);
};
});
test('io_extension should throw exception', () {
expect(() => registerWebServiceExtension(call),
throwsA(isInstanceOf<UnsupportedError>()));
});
});
}

View File

@@ -9,7 +9,7 @@ import 'package:flutter_driver/flutter_driver.dart';
import 'package:flutter_driver/src/driver/common.dart';
import 'package:path/path.dart' as path;
import '../common.dart';
import '../../common.dart';
void main() {
group('TimelineSummary', () {

View File

@@ -4,7 +4,7 @@
import 'package:flutter_driver/src/driver/timeline.dart';
import '../common.dart';
import '../../common.dart';
void main() {
group('Timeline', () {

View File

@@ -4,7 +4,7 @@
import 'package:flutter_driver/src/common/wait.dart';
import '../common.dart';
import '../../common.dart';
void main() {
group('WaitForCondition', () {

View File

@@ -0,0 +1,149 @@
// 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 'package:flutter_driver/src/driver/web_driver_config.dart';
import 'package:webdriver/sync_io.dart' as sync_io;
import '../../common.dart';
void main() {
group('getDesiredCapabilities', () {
test('Chrome with headless on', () {
final Map<String, dynamic> expected = <String, dynamic>{
'acceptInsecureCerts': true,
'browserName': 'chrome',
'goog:loggingPrefs': <String, String>{ sync_io.LogType.performance: 'ALL'},
'chromeOptions': <String, dynamic>{
'args': <String>[
'--bwsi',
'--disable-background-timer-throttling',
'--disable-default-apps',
'--disable-extensions',
'--disable-popup-blocking',
'--disable-translate',
'--no-default-browser-check',
'--no-sandbox',
'--no-first-run',
'--headless'
],
'perfLoggingPrefs': <String, String>{
'traceCategories':
'devtools.timeline,'
'v8,blink.console,benchmark,blink,'
'blink.user_timing'
}
}
};
expect(getDesiredCapabilities(Browser.chrome, true), expected);
});
test('Chrome with headless off', () {
final Map<String, dynamic> expected = <String, dynamic>{
'acceptInsecureCerts': true,
'browserName': 'chrome',
'goog:loggingPrefs': <String, String>{ sync_io.LogType.performance: 'ALL'},
'chromeOptions': <String, dynamic>{
'args': <String>[
'--bwsi',
'--disable-background-timer-throttling',
'--disable-default-apps',
'--disable-extensions',
'--disable-popup-blocking',
'--disable-translate',
'--no-default-browser-check',
'--no-sandbox',
'--no-first-run',
],
'perfLoggingPrefs': <String, String>{
'traceCategories':
'devtools.timeline,'
'v8,blink.console,benchmark,blink,'
'blink.user_timing'
}
}
};
expect(getDesiredCapabilities(Browser.chrome, false), expected);
});
test('Firefox with headless on', () {
final Map<String, dynamic> expected = <String, dynamic>{
'acceptInsecureCerts': true,
'browserName': 'firefox',
'moz:firefoxOptions' : <String, dynamic>{
'args': <String>['-headless'],
'prefs': <String, dynamic>{
'dom.file.createInChild': true,
'dom.timeout.background_throttling_max_budget': -1,
'media.autoplay.default': 0,
'media.gmp-manager.url': '',
'media.gmp-provider.enabled': false,
'network.captive-portal-service.enabled': false,
'security.insecure_field_warning.contextual.enabled': false,
'test.currentTimeOffsetSeconds': 11491200
},
'log': <String, String>{'level': 'trace'}
}
};
expect(getDesiredCapabilities(Browser.firefox, true), expected);
});
test('Firefox with headless off', () {
final Map<String, dynamic> expected = <String, dynamic>{
'acceptInsecureCerts': true,
'browserName': 'firefox',
'moz:firefoxOptions' : <String, dynamic>{
'args': <String>[],
'prefs': <String, dynamic>{
'dom.file.createInChild': true,
'dom.timeout.background_throttling_max_budget': -1,
'media.autoplay.default': 0,
'media.gmp-manager.url': '',
'media.gmp-provider.enabled': false,
'network.captive-portal-service.enabled': false,
'security.insecure_field_warning.contextual.enabled': false,
'test.currentTimeOffsetSeconds': 11491200
},
'log': <String, String>{'level': 'trace'}
}
};
expect(getDesiredCapabilities(Browser.firefox, false), expected);
});
test('Edge', () {
final Map<String, dynamic> expected = <String, dynamic>{
'acceptInsecureCerts': true,
'browserName': 'edge',
};
expect(getDesiredCapabilities(Browser.edge, false), expected);
});
test('macOS Safari', () {
final Map<String, dynamic> expected = <String, dynamic>{
'browserName': 'safari',
'safari.options': <String, dynamic>{
'skipExtensionInstallation': true,
'cleanSession': true
}
};
expect(getDesiredCapabilities(Browser.safari, false), expected);
});
test('iOS Safari', () {
final Map<String, dynamic> expected = <String, dynamic>{
'platformName': 'ios',
'browserName': 'safari',
'safari:useSimulator': true
};
expect(getDesiredCapabilities(Browser.iosSafari, false), expected);
});
});
}

View File

@@ -0,0 +1,26 @@
// 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:js' as js;
import 'package:flutter_driver/src/extension/_web_extension.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
group('test web_extension', () {
Future<Map<String, dynamic>> Function(Map<String, String>) call;
setUp(() {
call = (Map<String, String> args) async {
return Future<Map<String, dynamic>>.value(args);
};
});
test('web_extension should register a function', () {
expect(() => registerWebServiceExtension(call),
returnsNormally);
expect(js.context.hasProperty('\$flutterDriver'), true);
});
});
}