Skip to content

Commit 0aac530

Browse files
BilalWalkerLongCatIsLooong
authored andcommitted
[flutter_tools] Exit cleanly when the WebDriver server cannot create a session (flutter#194207)
`WebDriverService.startTest` only catches `SocketException` around `async_io.createDriver`. That covers the case where nothing is listening on the driver port, but when the WebDriver server (chromedriver, safaridriver, geckodriver) is reachable and then fails to launch or connect to the browser, `package:webdriver` throws a `WebDriverException` instead, for example: ``` SessionNotCreatedException (500): session not created: DevToolsActivePort file doesn't exist ``` That exception escapes `flutter drive` as an unhandled tool crash. It is the flutter#6 tool crasher in the 3.47.5 crash report (see the linked issue), typically caused by Chrome failing to start, a driver/browser version mismatch, or a Safari session timeout. This PR adds an `on async_io.WebDriverException` clause next to the existing `SocketException` one and turns it into a `ToolExit`. The message includes the server's own error text and points at the usual causes (browser not installed, driver version does not match the browser) plus the existing web integration-test docs link. The existing `SocketException` path is unchanged. Tests: a new test in `web_driver_service_test.dart` starts a loopback `HttpServer` that answers every request with a W3C `session not created` error, points `startTest` at it, and asserts a `ToolExit` containing the server's message. It fails on master (the raw `SessionNotCreatedException` escapes) and passes with this change. Fixes flutter#193517 ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant in-code documentation (doc comments with `///`). - [x] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/CONTRIBUTING.ai.md [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [flutter/website]: https://github.com/flutter/website [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 8356155 commit 0aac530

2 files changed

Lines changed: 66 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+
throwToolExit(
210+
'Unable to start a WebDriver session for web testing.\n'
211+
'The WebDriver server running at ${spec.driverPort} could not create a browser session:\n'
212+
'$error\n'
213+
'Make sure the WebDriver server matches --browser-name=${spec.browserName}, '
214+
'the browser is installed, and its version is compatible with the WebDriver server '
215+
'(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: 51 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';
@@ -12,13 +13,15 @@ import 'package:flutter_tools/src/build_system/build_system.dart';
1213
import 'package:flutter_tools/src/build_system/build_targets.dart';
1314
import 'package:flutter_tools/src/context/tool_context.dart';
1415
import 'package:flutter_tools/src/device.dart';
16+
import 'package:flutter_tools/src/drive/drive_service.dart';
1517
import 'package:flutter_tools/src/drive/web_driver_service.dart';
1618
import 'package:flutter_tools/src/isolated/build_targets.dart';
1719
import 'package:flutter_tools/src/project.dart';
1820
import 'package:flutter_tools/src/resident_runner.dart';
1921
import 'package:flutter_tools/src/web/chrome_constants.dart';
2022
import 'package:flutter_tools/src/web/devfs_config.dart';
2123
import 'package:flutter_tools/src/web/web_runner.dart';
24+
import 'package:package_config/package_config.dart';
2225
import 'package:test/fake.dart';
2326
import 'package:unified_analytics/unified_analytics.dart';
2427
import 'package:webdriver/sync_io.dart' as sync_io;
@@ -420,6 +423,54 @@ void main() {
420423
WebRunnerFactory: () => FakeWebRunnerFactory(doResolveToError: true),
421424
},
422425
);
426+
427+
testWithoutContext(
428+
'WebDriverService exits the tool when the WebDriver server cannot create a session',
429+
() async {
430+
// A fake WebDriver server that rejects every session request, the way
431+
// chromedriver does when it cannot launch the browser.
432+
final io.HttpServer driverServer = await io.HttpServer.bind(
433+
io.InternetAddress.loopbackIPv4,
434+
0,
435+
);
436+
addTearDown(() => driverServer.close(force: true));
437+
driverServer.listen((io.HttpRequest request) {
438+
request.response
439+
..statusCode = io.HttpStatus.internalServerError
440+
..headers.contentType = io.ContentType.json
441+
..write(
442+
json.encode(<String, Object?>{
443+
'value': <String, Object?>{
444+
'error': 'session not created',
445+
'message': "session not created: DevToolsActivePort file doesn't exist",
446+
'stacktrace': '',
447+
},
448+
}),
449+
);
450+
unawaited(request.response.close());
451+
});
452+
453+
final service = WebDriverService(
454+
analytics: const NoOpAnalytics(),
455+
buildSystem: TestBuildSystem.all(BuildResult(success: true)),
456+
buildTargets: const BuildTargetsImpl(),
457+
dartSdkPath: 'dart',
458+
toolContext: FakeToolContext(),
459+
);
460+
await expectLater(
461+
service.startTest(
462+
'test_driver/app_test.dart',
463+
DriveTestSpecification(
464+
packageConfig: PackageConfig.empty,
465+
browserName: 'chrome',
466+
headless: true,
467+
driverPort: driverServer.port,
468+
),
469+
),
470+
throwsToolExit(message: "DevToolsActivePort file doesn't exist"),
471+
);
472+
},
473+
);
423474
}
424475

425476
class FakeWebRunnerFactory implements WebRunnerFactory {

0 commit comments

Comments
 (0)