Skip to content

Commit 0543fa6

Browse files
committed
Exit cleanly when the WebDriver server cannot create a session
`WebDriverService.startTest` only caught `SocketException` around `createDriver`. When the WebDriver server (chromedriver, safaridriver, geckodriver) was reachable but failed to launch or connect to the browser, `package:webdriver` threw a `WebDriverException` such as `SessionNotCreatedException (500): session not created: DevToolsActivePort file doesn't exist`, which escaped `flutter drive` as an unhandled tool crash. Catch `WebDriverException` as well and turn it into a `ToolExit` that includes the server's error message and points at the usual causes (browser not installed, driver/browser version mismatch, session timeout). Add a regression test that runs a fake WebDriver server which rejects the session request. Fixes flutter#193517
1 parent 9cba739 commit 0543fa6

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

‎packages/flutter_tools/lib/src/drive/web_driver_service.dart‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,21 @@ class WebDriverService extends DriverService {
201201
'For instructions on how to obtain and run a WebDriver server, see:\n'
202202
'https://flutter.dev/to/integration-test-on-web\n',
203203
);
204+
} on async_io.WebDriverException catch (error) {
205+
// The WebDriver server is reachable, but it could not create a browser
206+
// session. This typically happens when the browser fails to launch, when
207+
// the browser and WebDriver server versions do not match, or when the
208+
// session request times out.
209+
logger.printTrace('$error');
210+
throwToolExit(
211+
'Unable to start a WebDriver session for web testing.\n'
212+
'The WebDriver server running at $driverPort could not create a browser session:\n'
213+
'$error\n'
214+
'Make sure the browser is installed and that its version is compatible with '
215+
'the WebDriver server (e.g. chromedriver must match the installed Chrome version).\n'
216+
'For instructions on how to obtain and run a WebDriver server, see:\n'
217+
'https://flutter.dev/to/integration-test-on-web\n',
218+
);
204219
}
205220

206221
if (!isAndroidChrome && browserDimension != null) {

‎packages/flutter_tools/test/general.shard/drive/web_driver_service_test.dart‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// found in the LICENSE file.
44

55
import 'dart:async';
6+
import 'dart:convert';
67
import 'dart:io' as io;
78

89
import 'package:flutter_tools/src/base/net.dart';
@@ -19,6 +20,7 @@ import 'package:flutter_tools/src/resident_runner.dart';
1920
import 'package:flutter_tools/src/web/chrome_constants.dart';
2021
import 'package:flutter_tools/src/web/devfs_config.dart';
2122
import 'package:flutter_tools/src/web/web_runner.dart';
23+
import 'package:package_config/package_config.dart';
2224
import 'package:test/fake.dart';
2325
import 'package:unified_analytics/unified_analytics.dart';
2426
import 'package:webdriver/sync_io.dart' as sync_io;
@@ -420,6 +422,47 @@ void main() {
420422
WebRunnerFactory: () => FakeWebRunnerFactory(doResolveToError: true),
421423
},
422424
);
425+
426+
testUsingContext(
427+
'WebDriverService exits the tool when the WebDriver server cannot create a session',
428+
() async {
429+
// A fake WebDriver server that rejects every session request, the way
430+
// chromedriver does when it cannot launch the browser.
431+
final io.HttpServer driverServer = await io.HttpServer.bind(
432+
io.InternetAddress.loopbackIPv4,
433+
0,
434+
);
435+
addTearDown(() => driverServer.close(force: true));
436+
driverServer.listen((io.HttpRequest request) {
437+
request.response
438+
..statusCode = io.HttpStatus.internalServerError
439+
..headers.contentType = io.ContentType.json
440+
..write(
441+
json.encode(<String, Object?>{
442+
'value': <String, Object?>{
443+
'error': 'session not created',
444+
'message': "session not created: DevToolsActivePort file doesn't exist",
445+
'stacktrace': '',
446+
},
447+
}),
448+
);
449+
unawaited(request.response.close());
450+
});
451+
452+
final WebDriverService service = setUpDriverService();
453+
await expectLater(
454+
service.startTest(
455+
'test_driver/app_test.dart',
456+
<String>[],
457+
PackageConfig.empty,
458+
browserName: 'chrome',
459+
headless: true,
460+
driverPort: driverServer.port,
461+
),
462+
throwsToolExit(message: "DevToolsActivePort file doesn't exist"),
463+
);
464+
},
465+
);
423466
}
424467

425468
class FakeWebRunnerFactory implements WebRunnerFactory {

0 commit comments

Comments
 (0)