Index: lib/src/runner/browser/chrome.dart |
diff --git a/lib/src/runner/browser/chrome.dart b/lib/src/runner/browser/chrome.dart |
index 4d4bdaf13680d19b75dc613471ba6e4a6e909f79..351c49025025257f0a422b3187639072bd930ff5 100644 |
--- a/lib/src/runner/browser/chrome.dart |
+++ b/lib/src/runner/browser/chrome.dart |
@@ -5,19 +5,14 @@ |
library test.runner.browser.chrome; |
import 'dart:async'; |
-import 'dart:convert'; |
import 'dart:io'; |
import 'package:path/path.dart' as p; |
-import 'package:stack_trace/stack_trace.dart'; |
import '../../util/io.dart'; |
-import '../../utils.dart'; |
-import '../application_exception.dart'; |
import 'browser.dart'; |
// TODO(nweiz): move this into its own package? |
-// TODO(nweiz): support other browsers. |
/// A class for running an instance of Chrome. |
/// |
/// Most of the communication with the browser is expected to happen via HTTP, |
@@ -25,79 +20,41 @@ import 'browser.dart'; |
/// constructed, and is killed when [close] is called. |
/// |
/// Any errors starting or running the process are reported through [onExit]. |
-class Chrome implements Browser { |
- /// The underlying process. |
- Process _process; |
- |
- Future get onExit => _onExitCompleter.future; |
- final _onExitCompleter = new Completer(); |
- |
- /// A future that completes when the browser process has started. |
- /// |
- /// This is used to ensure that [close] works regardless of when it's called. |
- Future get _onProcessStarted => _onProcessStartedCompleter.future; |
- final _onProcessStartedCompleter = new Completer(); |
+class Chrome extends Browser { |
+ final name = "Chrome"; |
/// Starts a new instance of Chrome open to the given [url], which may be a |
/// [Uri] or a [String]. |
/// |
/// If [executable] is passed, it's used as the Chrome executable. Otherwise |
/// the default executable name for the current OS will be used. |
- Chrome(url, {String executable}) { |
- if (executable == null) executable = _defaultExecutable(); |
- |
- // Don't return a Future here because there's no need for the caller to wait |
- // for the process to actually start. They should just wait for the HTTP |
- // request instead. |
- invoke(() async { |
- try { |
- var exitCode = await withTempDir((dir) async { |
- var process = await Process.start(executable, [ |
- "--user-data-dir=$dir", |
- url.toString(), |
- "--disable-extensions", |
- "--disable-popup-blocking", |
- "--bwsi", |
- "--no-first-run", |
- "--no-default-browser-check", |
- "--disable-default-apps", |
- "--disable-translate" |
- ]); |
- |
- _process = process; |
- _onProcessStartedCompleter.complete(); |
+ Chrome(url, {String executable}) |
+ : super(() => _startBrowser(url, executable)); |
- // TODO(nweiz): the browser's standard output is almost always useless |
- // noise, but we should allow the user to opt in to seeing it. |
- return await _process.exitCode; |
- }); |
- |
- if (exitCode != 0) { |
- var error = await UTF8.decodeStream(_process.stderr); |
- throw new ApplicationException( |
- "Chrome failed with exit code $exitCode:\n$error"); |
- } |
- |
- _onExitCompleter.complete(); |
- } catch (error, stackTrace) { |
- if (stackTrace == null) stackTrace = new Trace.current(); |
- _onExitCompleter.completeError( |
- new ApplicationException( |
- "Failed to start Chrome: ${getErrorMessage(error)}."), |
- stackTrace); |
- } |
- }); |
- } |
- |
- Future close() { |
- _onProcessStarted.then((_) => _process.kill()); |
+ static Future<Process> _startBrowser(url, [String executable]) async { |
+ if (executable == null) executable = _defaultExecutable(); |
- // Swallow exceptions. The user should explicitly use [onExit] for these. |
- return onExit.catchError((_) {}); |
+ var dir = createTempDir(); |
+ var process = await Process.start(executable, [ |
+ "--user-data-dir=$dir", |
+ url.toString(), |
+ "--disable-extensions", |
+ "--disable-popup-blocking", |
+ "--bwsi", |
+ "--no-first-run", |
+ "--no-default-browser-check", |
+ "--disable-default-apps", |
+ "--disable-translate" |
+ ]); |
+ |
+ process.exitCode |
+ .then((_) => new Directory(dir).deleteSync(recursive: true)); |
+ |
+ return process; |
} |
/// Return the default executable for the current operating system. |
- String _defaultExecutable() { |
+ static String _defaultExecutable() { |
if (Platform.isMacOS) { |
return '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'; |
} |