OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library unittest.runner.browser.chrome; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:io'; |
| 9 |
| 10 import '../../util/io.dart'; |
| 11 |
| 12 // TODO(nweiz): move this into its own package? |
| 13 // TODO(nweiz): support other browsers. |
| 14 /// A class for running an instance of Chrome. |
| 15 /// |
| 16 /// Most of the communication with the browser is expected to happen via HTTP, |
| 17 /// so this exposes a bare-bones API. The browser starts as soon as the class is |
| 18 /// constructed, and is killed when [close] is called. |
| 19 /// |
| 20 /// Any errors starting or running the process are reported through [onExit]. |
| 21 class Chrome { |
| 22 /// The underlying process. |
| 23 Process _process; |
| 24 |
| 25 /// The temporary directory used as the browser's user data dir. |
| 26 /// |
| 27 /// A new data dir is created for each run to ensure that they're |
| 28 /// well-isolated. |
| 29 String _dir; |
| 30 |
| 31 /// A future that completes when the browser exits. |
| 32 /// |
| 33 /// If there's a problem starting or running the browser, this will complete |
| 34 /// with an error. |
| 35 Future get onExit => _onExitCompleter.future; |
| 36 final _onExitCompleter = new Completer(); |
| 37 |
| 38 /// A future that completes when the browser process has started. |
| 39 /// |
| 40 /// This is used to ensure that [close] works regardless of when it's called. |
| 41 Future get _onProcessStarted => _onProcessStartedCompleter.future; |
| 42 final _onProcessStartedCompleter = new Completer(); |
| 43 |
| 44 /// Starts a new instance of Chrome open to the given [url], which may be a |
| 45 /// [Uri] or a [String]. |
| 46 /// |
| 47 /// If [executable] is passed, it's used as the Chrome executable. Otherwise |
| 48 /// `"google-chrome"` will be looked up on the system PATH. |
| 49 Chrome(url, {String executable}) { |
| 50 if (executable == null) executable = "google-chrome"; |
| 51 |
| 52 // Don't return a Future here because there's no need for the caller to wait |
| 53 // for the process to actually start. They should just wait for the HTTP |
| 54 // request instead. |
| 55 withTempDir((dir) { |
| 56 _dir = dir; |
| 57 return Process.start(executable, [ |
| 58 "--user-data-dir=$_dir", |
| 59 url.toString(), |
| 60 "--disable-extensions", |
| 61 "--disable-popup-blocking", |
| 62 "--bwsi", |
| 63 "--no-first-run" |
| 64 ]).then((process) { |
| 65 _process = process; |
| 66 _onProcessStartedCompleter.complete(); |
| 67 |
| 68 // TODO(nweiz): the browser's standard output is almost always useless |
| 69 // noise, but we should allow the user to opt in to seeing it. |
| 70 return _process.exitCode; |
| 71 }); |
| 72 }).then((exitCode) { |
| 73 if (exitCode != 0) throw "Chrome failed with exit code $exitCode."; |
| 74 }).then(_onExitCompleter.complete) |
| 75 .catchError(_onExitCompleter.completeError); |
| 76 } |
| 77 |
| 78 /// Kills the browser process. |
| 79 /// |
| 80 /// Returns the same [Future] as [onExit], except that it won't emit |
| 81 /// exceptions. |
| 82 Future close() { |
| 83 _onProcessStarted.then((_) => _process.kill()); |
| 84 |
| 85 // Swallow exceptions. The user should explicitly use [onExit] for these. |
| 86 return onExit.catchError((_) {}); |
| 87 } |
| 88 } |
OLD | NEW |