Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(104)

Unified Diff: lib/src/runner/browser/phantom_js.dart

Issue 1082943003: Support PhantomJS. (Closed) Base URL: git@github.com:dart-lang/test@master
Patch Set: Code review changes Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/src/backend/test_platform.dart ('k') | lib/src/runner/browser/server.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/runner/browser/phantom_js.dart
diff --git a/lib/src/runner/browser/phantom_js.dart b/lib/src/runner/browser/phantom_js.dart
new file mode 100644
index 0000000000000000000000000000000000000000..b4e2f4a40e44ce8326e3be1f6f7a5ffb08d53e9d
--- /dev/null
+++ b/lib/src/runner/browser/phantom_js.dart
@@ -0,0 +1,87 @@
+// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library test.runner.browser.phantom_js;
+
+import 'dart:async';
+import 'dart:io';
+
+import 'package:path/path.dart' as p;
+
+import '../../util/io.dart';
+import 'browser.dart';
+
+/// The PhantomJS script that opens the host page.
+final _script = """
+var system = require('system');
+var page = require('webpage').create();
+
+// Pipe browser messages to the process's stdout. This isn't used by default,
+// but it can be useful for debugging.
+page.onConsoleMessage = function(message) {
+ console.log(message);
+}
+
+page.open(system.args[1], function(status) {
+ if (status !== "success") phantom.exit(1);
+});
+""";
+
+/// A class for running an instance of PhantomJS.
+///
+/// Any errors starting or running the process are reported through [onExit].
+class PhantomJS 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();
+
+ /// Starts a new instance of PhantomJS open to the given [url], which may be a
+ /// [Uri] or a [String].
+ ///
+ /// If [executable] is passed, it's used as the PhantomJS executable.
+ /// Otherwise the default executable name for the current OS will be used.
+ PhantomJS(url, {String executable}) {
+ if (executable == null) {
+ executable = Platform.isWindows ? "phantomjs.exe" : "phantomjs";
+ }
+
+ // 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.
+ withTempDir((dir) {
+ var script = p.join(dir, "script.js");
+ new File(script).writeAsStringSync(_script);
+
+ return Process.start(executable, [script, url.toString()])
+ .then((process) {
+ // PhantomJS synchronously emits standard output, which means that if we
+ // don't drain its stdout stream it can deadlock.
+ process.stdout.listen((_) {});
+
+ _process = process;
+ _onProcessStartedCompleter.complete();
+
+ return _process.exitCode;
+ });
+ }).then((exitCode) {
+ if (exitCode != 0) throw "PhantomJS failed with exit code $exitCode.";
+ }).then(_onExitCompleter.complete)
+ .catchError(_onExitCompleter.completeError);
+ }
+
+ Future close() {
+ _onProcessStarted.then((_) => _process.kill());
+
+ // Swallow exceptions. The user should explicitly use [onExit] for these.
+ return onExit.catchError((_) {});
+ }
+}
« no previous file with comments | « lib/src/backend/test_platform.dart ('k') | lib/src/runner/browser/server.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698