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.chrome; | 5 library test.runner.browser.internet_explorer; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:convert'; | 8 import 'dart:convert'; |
9 import 'dart:io'; | 9 import 'dart:io'; |
10 | 10 |
11 import 'package:path/path.dart' as p; | 11 import 'package:path/path.dart' as p; |
12 import 'package:stack_trace/stack_trace.dart'; | 12 import 'package:stack_trace/stack_trace.dart'; |
13 | 13 |
14 import '../../util/io.dart'; | |
15 import '../../utils.dart'; | 14 import '../../utils.dart'; |
16 import '../application_exception.dart'; | 15 import '../application_exception.dart'; |
17 import 'browser.dart'; | 16 import 'browser.dart'; |
18 | 17 |
19 // TODO(nweiz): move this into its own package? | 18 /// A class for running an instance of Internet Explorer. |
20 // TODO(nweiz): support other browsers. | |
21 /// A class for running an instance of Chrome. | |
22 /// | |
23 /// Most of the communication with the browser is expected to happen via HTTP, | |
24 /// so this exposes a bare-bones API. The browser starts as soon as the class is | |
25 /// constructed, and is killed when [close] is called. | |
26 /// | 19 /// |
27 /// Any errors starting or running the process are reported through [onExit]. | 20 /// Any errors starting or running the process are reported through [onExit]. |
28 class Chrome implements Browser { | 21 class InternetExplorer implements Browser { |
29 /// The underlying process. | 22 /// The underlying process. |
30 Process _process; | 23 Process _process; |
31 | 24 |
32 Future get onExit => _onExitCompleter.future; | 25 Future get onExit => _onExitCompleter.future; |
33 final _onExitCompleter = new Completer(); | 26 final _onExitCompleter = new Completer(); |
34 | 27 |
35 /// A future that completes when the browser process has started. | 28 /// A future that completes when the browser process has started. |
36 /// | 29 /// |
37 /// This is used to ensure that [close] works regardless of when it's called. | 30 /// This is used to ensure that [close] works regardless of when it's called. |
38 Future get _onProcessStarted => _onProcessStartedCompleter.future; | 31 Future get _onProcessStarted => _onProcessStartedCompleter.future; |
39 final _onProcessStartedCompleter = new Completer(); | 32 final _onProcessStartedCompleter = new Completer(); |
40 | 33 |
41 /// Starts a new instance of Chrome open to the given [url], which may be a | 34 /// Starts a new instance of Internet Explorer open to the given [url], which |
42 /// [Uri] or a [String]. | 35 /// may be a [Uri] or a [String]. |
43 /// | 36 /// |
44 /// If [executable] is passed, it's used as the Chrome executable. Otherwise | 37 /// If [executable] is passed, it's used as the Internet Explorer executable. |
45 /// the default executable name for the current OS will be used. | 38 /// Otherwise the default executable name will be used. |
46 Chrome(url, {String executable}) { | 39 InternetExplorer(url, {String executable}) { |
47 if (executable == null) executable = _defaultExecutable(); | 40 if (executable == null) executable = _defaultExecutable(); |
48 | 41 |
49 // Don't return a Future here because there's no need for the caller to wait | 42 // Don't return a Future here because there's no need for the caller to wait |
50 // for the process to actually start. They should just wait for the HTTP | 43 // for the process to actually start. They should just wait for the HTTP |
51 // request instead. | 44 // request instead. |
52 withTempDir((dir) { | 45 Process.start(executable, ['-extoff', url.toString()]) |
53 return Process.start(executable, [ | 46 .then((process) { |
54 "--user-data-dir=$dir", | 47 _process = process; |
55 url.toString(), | 48 _onProcessStartedCompleter.complete(); |
56 "--disable-extensions", | |
57 "--disable-popup-blocking", | |
58 "--bwsi", | |
59 "--no-first-run", | |
60 "--no-default-browser-check", | |
61 "--disable-default-apps", | |
62 "--disable-translate" | |
63 ]).then((process) { | |
64 _process = process; | |
65 _onProcessStartedCompleter.complete(); | |
66 | 49 |
67 // TODO(nweiz): the browser's standard output is almost always useless | 50 // TODO(nweiz): the browser's standard output is almost always useless |
68 // noise, but we should allow the user to opt in to seeing it. | 51 // noise, but we should allow the user to opt in to seeing it. |
69 return _process.exitCode; | 52 return _process.exitCode; |
70 }); | |
71 }).then((exitCode) { | 53 }).then((exitCode) { |
72 if (exitCode == 0) return null; | 54 if (exitCode == 0) return null; |
73 | 55 |
74 return UTF8.decodeStream(_process.stderr).then((error) { | 56 return UTF8.decodeStream(_process.stderr).then((error) { |
75 throw new ApplicationException( | 57 throw new ApplicationException( |
76 "Chrome failed with exit code $exitCode:\n$error"); | 58 "Internet Explorer failed with exit code $exitCode:\n$error"); |
77 }); | 59 }); |
78 }).then(_onExitCompleter.complete).catchError((error, stackTrace) { | 60 }).then(_onExitCompleter.complete).catchError((error, stackTrace) { |
79 if (stackTrace == null) stackTrace = new Trace.current(); | 61 if (stackTrace == null) stackTrace = new Trace.current(); |
80 _onExitCompleter.completeError( | 62 _onExitCompleter.completeError( |
81 new ApplicationException( | 63 new ApplicationException( |
82 "Failed to start Chrome: ${getErrorMessage(error)}."), | 64 "Failed to start Internet Explorer: ${getErrorMessage(error)}."), |
83 stackTrace); | 65 stackTrace); |
84 }); | 66 }); |
85 } | 67 } |
86 | 68 |
87 Future close() { | 69 Future close() { |
88 _onProcessStarted.then((_) => _process.kill()); | 70 _onProcessStarted.then((_) => _process.kill()); |
89 | 71 |
90 // Swallow exceptions. The user should explicitly use [onExit] for these. | 72 // Swallow exceptions. The user should explicitly use [onExit] for these. |
91 return onExit.catchError((_) {}); | 73 return onExit.catchError((_) {}); |
92 } | 74 } |
93 | 75 |
94 /// Return the default executable for the current operating system. | 76 /// Return the default executable for the current operating system. |
95 String _defaultExecutable() { | 77 String _defaultExecutable() { |
96 if (Platform.isMacOS) { | |
97 return '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'; | |
98 } | |
99 if (!Platform.isWindows) return 'google-chrome'; | |
100 | |
101 // Chrome could be installed in several places on Windows. The only way to | 78 // Chrome could be installed in several places on Windows. The only way to |
102 // find it is to check. | 79 // find it is to check. |
103 var prefixes = [ | 80 var prefixes = [ |
104 Platform.environment['LOCALAPPDATA'], | 81 Platform.environment['PROGRAMW6432'], |
105 Platform.environment['PROGRAMFILES'], | 82 Platform.environment['PROGRAMFILES'], |
106 Platform.environment['PROGRAMFILES(X86)'] | 83 Platform.environment['PROGRAMFILES(X86)'] |
107 ]; | 84 ]; |
108 var suffix = r'Google\Chrome\Application\chrome.exe'; | 85 var suffix = r'Internet Explorer\iexplore.exe'; |
109 | 86 |
110 for (var prefix in prefixes) { | 87 for (var prefix in prefixes) { |
111 if (prefix == null) continue; | 88 if (prefix == null) continue; |
112 | 89 |
113 var path = p.join(prefix, suffix); | 90 var path = p.join(prefix, suffix); |
114 if (new File(p.join(prefix, suffix)).existsSync()) return path; | 91 if (new File(p.join(prefix, suffix)).existsSync()) return path; |
115 } | 92 } |
116 | 93 |
117 // Fall back on looking it up on the path. This probably won't work, but at | 94 // Fall back on looking it up on the path. This probably won't work, but at |
118 // least it will fail with a useful error message. | 95 // least it will fail with a useful error message. |
119 return "chrome.exe"; | 96 return "iexplore.exe"; |
120 } | 97 } |
121 } | 98 } |
OLD | NEW |