This repository was archived by the owner on Mar 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathphantomjs.js
More file actions
193 lines (163 loc) · 5.53 KB
/
phantomjs.js
File metadata and controls
193 lines (163 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright 2013 Selenium committers
// Copyright 2013 Software Freedom Conservancy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use strict';
var fs = require('fs'),
util = require('util');
var webdriver = require('./index'),
executors = require('./executors'),
io = require('./io'),
portprober = require('./net/portprober'),
remote = require('./remote');
/**
* Name of the PhantomJS executable.
* @type {string}
* @const
*/
var PHANTOMJS_EXE =
process.platform === 'win32' ? 'phantomjs.exe' : 'phantomjs';
/**
* Capability that designates the location of the PhantomJS executable to use.
* @type {string}
* @const
*/
var BINARY_PATH_CAPABILITY = 'phantomjs.binary.path';
/**
* Capability that designates the CLI arguments to pass to PhantomJS.
* @type {string}
* @const
*/
var CLI_ARGS_CAPABILITY = 'phantomjs.cli.args';
/**
* Default log file to use if one is not specified through CLI args.
* @type {string}
* @const
*/
var DEFAULT_LOG_FILE = 'phantomjsdriver.log';
/**
* Finds the PhantomJS executable.
* @param {string=} opt_exe Path to the executable to use.
* @return {string} The located executable.
* @throws {Error} If the executable cannot be found on the PATH, or if the
* provided executable path does not exist.
*/
function findExecutable(opt_exe) {
var exe = opt_exe || io.findInPath(PHANTOMJS_EXE, true);
if (!exe) {
throw Error(
'The PhantomJS executable could not be found on the current PATH. ' +
'Please download the latest version from ' +
'http://phantomjs.org/download.html and ensure it can be found on ' +
'your PATH. For more information, see ' +
'https://github.com/ariya/phantomjs/wiki');
}
if (!fs.existsSync(exe)) {
throw Error('File does not exist: ' + exe);
}
return exe;
}
/**
* Maps WebDriver logging level name to those recognised by PhantomJS.
* @type {!Object.<string, string>}
* @const
*/
var WEBDRIVER_TO_PHANTOMJS_LEVEL = (function() {
var map = {};
map[webdriver.logging.Level.ALL.name] = 'DEBUG';
map[webdriver.logging.Level.DEBUG.name] = 'DEBUG';
map[webdriver.logging.Level.INFO.name] = 'INFO';
map[webdriver.logging.Level.WARNING.name] = 'WARN';
map[webdriver.logging.Level.SEVERE.name] = 'ERROR';
return map;
})();
/**
* Creates a new PhantomJS WebDriver client.
* @param {webdriver.Capabilities=} opt_capabilities The desired capabilities.
* @param {webdriver.promise.ControlFlow=} opt_flow The control flow to use, or
* {@code null} to use the currently active flow.
* @return {!webdriver.WebDriver} A new WebDriver instance.
* @deprecated Use {@link Driver}.
*/
function createDriver(opt_capabilities, opt_flow) {
return new Driver(opt_capabilities, opt_flow);
}
/**
* Creates a new WebDriver client for PhantomJS.
*
* @param {webdriver.Capabilities=} opt_capabilities The desired capabilities.
* @param {webdriver.promise.ControlFlow=} opt_flow The control flow to use, or
* {@code null} to use the currently active flow.
* @constructor
* @extends {webdriver.WebDriver}
*/
var Driver = function(opt_capabilities, opt_flow) {
var capabilities = opt_capabilities || webdriver.Capabilities.phantomjs();
var exe = findExecutable(capabilities.get(BINARY_PATH_CAPABILITY));
var args = ['--webdriver-logfile=' + DEFAULT_LOG_FILE];
var logPrefs = capabilities.get(webdriver.Capability.LOGGING_PREFS);
if (logPrefs instanceof webdriver.logging.Preferences) {
logPrefs = logPrefs.toJSON();
}
if (logPrefs && logPrefs[webdriver.logging.Type.DRIVER]) {
var level = WEBDRIVER_TO_PHANTOMJS_LEVEL[
logPrefs[webdriver.logging.Type.DRIVER]];
if (level) {
args.push('--webdriver-loglevel=' + level);
}
}
var proxy = capabilities.get(webdriver.Capability.PROXY);
if (proxy) {
switch (proxy.proxyType) {
case 'manual':
if (proxy.httpProxy) {
args.push(
'--proxy-type=http',
'--proxy=http://' + proxy.httpProxy);
}
break;
case 'pac':
throw Error('PhantomJS does not support Proxy PAC files');
case 'system':
args.push('--proxy-type=system');
break;
case 'direct':
args.push('--proxy-type=none');
break;
}
}
args = args.concat(capabilities.get(CLI_ARGS_CAPABILITY) || []);
var port = portprober.findFreePort();
var service = new remote.DriverService(exe, {
port: port,
args: webdriver.promise.when(port, function(port) {
args.push('--webdriver=' + port);
return args;
})
});
var executor = executors.createExecutor(service.start());
var driver = webdriver.WebDriver.createSession(
executor, capabilities, opt_flow);
webdriver.WebDriver.call(
this, driver.getSession(), executor, driver.controlFlow());
var boundQuit = this.quit.bind(this);
/** @override */
this.quit = function() {
return boundQuit().thenFinally(service.kill.bind(service));
};
return driver;
};
util.inherits(Driver, webdriver.WebDriver);
// PUBLIC API
exports.Driver = Driver;
exports.createDriver = createDriver;