OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library test.runner.browser.phantom_js; | 5 library test.runner.browser.phantom_js; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:io'; | 8 import 'dart:io'; |
9 | 9 |
10 import 'package:path/path.dart' as p; | 10 import 'package:path/path.dart' as p; |
(...skipping 21 matching lines...) Expand all Loading... |
32 if (status !== "success") phantom.exit(1); | 32 if (status !== "success") phantom.exit(1); |
33 }); | 33 }); |
34 """; | 34 """; |
35 | 35 |
36 /// A class for running an instance of PhantomJS. | 36 /// A class for running an instance of PhantomJS. |
37 /// | 37 /// |
38 /// Any errors starting or running the process are reported through [onExit]. | 38 /// Any errors starting or running the process are reported through [onExit]. |
39 class PhantomJS extends Browser { | 39 class PhantomJS extends Browser { |
40 final name = "PhantomJS"; | 40 final name = "PhantomJS"; |
41 | 41 |
42 PhantomJS(url, {String executable}) | 42 final Future<Uri> remoteDebuggerUrl; |
43 : super(() => _startBrowser(url, executable)); | |
44 | 43 |
45 /// Starts a new instance of PhantomJS open to the given [url], which may be a | 44 factory PhantomJS(url, {String executable, bool debug: false}) { |
46 /// [Uri] or a [String]. | 45 var remoteDebuggerCompleter = new Completer.sync(); |
47 /// | 46 return new PhantomJS._(() async { |
48 /// If [executable] is passed, it's used as the PhantomJS executable. | 47 if (executable == null) { |
49 /// Otherwise the default executable name for the current OS will be used. | 48 executable = Platform.isWindows ? "phantomjs.exe" : "phantomjs"; |
50 static Future<Process> _startBrowser(url, [String executable]) async { | 49 } |
51 if (executable == null) { | |
52 executable = Platform.isWindows ? "phantomjs.exe" : "phantomjs"; | |
53 } | |
54 | 50 |
55 var dir = createTempDir(); | 51 var dir = createTempDir(); |
56 var script = p.join(dir, "script.js"); | 52 var script = p.join(dir, "script.js"); |
57 new File(script).writeAsStringSync(_script); | 53 new File(script).writeAsStringSync(_script); |
58 | 54 |
59 var process = await Process.start( | 55 var port = debug ? await getUnsafeUnusedPort() : null; |
60 executable, [script, url.toString()]); | |
61 | 56 |
62 // PhantomJS synchronously emits standard output, which means that if we | 57 var args = []; |
63 // don't drain its stdout stream it can deadlock. | 58 if (debug) { |
64 process.stdout.listen((_) {}); | 59 args.addAll([ |
| 60 "--remote-debugger-port=$port", |
| 61 "--remote-debugger-autorun=yes" |
| 62 ]); |
| 63 } |
| 64 args.addAll([script, url.toString()]); |
| 65 var process = await Process.start(executable, args); |
65 | 66 |
66 process.exitCode.then((exitCode) { | 67 // PhantomJS synchronously emits standard output, which means that if we |
67 new Directory(dir).deleteSync(recursive: true); | 68 // don't drain its stdout stream it can deadlock. |
| 69 process.stdout.listen((_) {}); |
68 | 70 |
69 if (exitCode == exit_codes.protocol) { | 71 process.exitCode.then((exitCode) { |
70 throw new ApplicationException( | 72 new Directory(dir).deleteSync(recursive: true); |
71 "Only PhantomJS version 2.0.0 or greater is supported"); | 73 |
| 74 if (exitCode == exit_codes.protocol) { |
| 75 throw new ApplicationException( |
| 76 "Only PhantomJS version 2.0.0 or greater is supported"); |
| 77 } |
| 78 }); |
| 79 |
| 80 if (port != null) { |
| 81 remoteDebuggerCompleter.complete(Uri.parse( |
| 82 "http://localhost:$port/webkit/inspector/inspector.html?page=2")); |
| 83 } else { |
| 84 remoteDebuggerCompleter.complete(null); |
72 } | 85 } |
73 }); | |
74 | 86 |
75 return process; | 87 return process; |
| 88 }, remoteDebuggerCompleter.future); |
76 } | 89 } |
| 90 |
| 91 PhantomJS._(Future<Process> startBrowser(), this.remoteDebuggerUrl) |
| 92 : super(startBrowser); |
77 } | 93 } |
OLD | NEW |