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 test.runner.browser.phantom_js; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:io'; |
| 9 |
| 10 import 'package:path/path.dart' as p; |
| 11 |
| 12 import '../../util/io.dart'; |
| 13 import 'browser.dart'; |
| 14 |
| 15 /// The PhantomJS script that opens the host page. |
| 16 final _script = """ |
| 17 var system = require('system'); |
| 18 var page = require('webpage').create(); |
| 19 |
| 20 // Pipe browser messages to the process's stdout. This isn't used by default, |
| 21 // but it can be useful for debugging. |
| 22 page.onConsoleMessage = function(message) { |
| 23 console.log(message); |
| 24 } |
| 25 |
| 26 page.open(system.args[1], function(status) { |
| 27 if (status !== "success") phantom.exit(1); |
| 28 }); |
| 29 """; |
| 30 |
| 31 /// A class for running an instance of PhantomJS. |
| 32 /// |
| 33 /// Any errors starting or running the process are reported through [onExit]. |
| 34 class PhantomJS implements Browser { |
| 35 /// The underlying process. |
| 36 Process _process; |
| 37 |
| 38 Future get onExit => _onExitCompleter.future; |
| 39 final _onExitCompleter = new Completer(); |
| 40 |
| 41 /// A future that completes when the browser process has started. |
| 42 /// |
| 43 /// This is used to ensure that [close] works regardless of when it's called. |
| 44 Future get _onProcessStarted => _onProcessStartedCompleter.future; |
| 45 final _onProcessStartedCompleter = new Completer(); |
| 46 |
| 47 /// Starts a new instance of PhantomJS open to the given [url], which may be a |
| 48 /// [Uri] or a [String]. |
| 49 /// |
| 50 /// If [executable] is passed, it's used as the PhantomJS executable. |
| 51 /// Otherwise the default executable name for the current OS will be used. |
| 52 PhantomJS(url, {String executable}) { |
| 53 if (executable == null) { |
| 54 executable = Platform.isWindows ? "phantomjs.exe" : "phantomjs"; |
| 55 } |
| 56 |
| 57 // Don't return a Future here because there's no need for the caller to wait |
| 58 // for the process to actually start. They should just wait for the HTTP |
| 59 // request instead. |
| 60 withTempDir((dir) { |
| 61 var script = p.join(dir, "script.js"); |
| 62 new File(script).writeAsStringSync(_script); |
| 63 |
| 64 return Process.start(executable, [script, url.toString()]) |
| 65 .then((process) { |
| 66 // PhantomJS synchronously emits standard output, which means that if we |
| 67 // don't drain its stdout stream it can deadlock. |
| 68 process.stdout.listen((_) {}); |
| 69 |
| 70 _process = process; |
| 71 _onProcessStartedCompleter.complete(); |
| 72 |
| 73 return _process.exitCode; |
| 74 }); |
| 75 }).then((exitCode) { |
| 76 if (exitCode != 0) throw "PhantomJS failed with exit code $exitCode."; |
| 77 }).then(_onExitCompleter.complete) |
| 78 .catchError(_onExitCompleter.completeError); |
| 79 } |
| 80 |
| 81 Future close() { |
| 82 _onProcessStarted.then((_) => _process.kill()); |
| 83 |
| 84 // Swallow exceptions. The user should explicitly use [onExit] for these. |
| 85 return onExit.catchError((_) {}); |
| 86 } |
| 87 } |
OLD | NEW |