@@ -20,6 +20,7 @@ var fs = require('fs'),
20
20
21
21
var webdriver = require ( './index' ) ,
22
22
executors = require ( './executors' ) ,
23
+ http = require ( './http' ) ,
23
24
io = require ( './io' ) ,
24
25
portprober = require ( './net/portprober' ) ,
25
26
remote = require ( './remote' ) ;
@@ -58,6 +59,15 @@ var CLI_ARGS_CAPABILITY = 'phantomjs.cli.args';
58
59
var DEFAULT_LOG_FILE = 'phantomjsdriver.log' ;
59
60
60
61
62
+ /**
63
+ * Custom command names supported by PhantomJS.
64
+ * @enum {string}
65
+ */
66
+ var Command = {
67
+ EXECUTE_PHANTOM_SCRIPT : 'executePhantomScript'
68
+ } ;
69
+
70
+
61
71
/**
62
72
* Finds the PhantomJS executable.
63
73
* @param {string= } opt_exe Path to the executable to use.
@@ -111,6 +121,24 @@ function createDriver(opt_capabilities, opt_flow) {
111
121
}
112
122
113
123
124
+ /**
125
+ * Creates a command executor with support for PhantomJS' custom commands.
126
+ * @param {!webdriver.promise.Promise<string> } url The server's URL.
127
+ * @return {!webdriver.CommandExecutor } The new command executor.
128
+ */
129
+ function createExecutor ( url ) {
130
+ return new executors . DeferredExecutor ( url . then ( function ( url ) {
131
+ var client = new http . HttpClient ( url ) ;
132
+ var executor = new http . Executor ( client ) ;
133
+
134
+ executor . defineCommand (
135
+ Command . EXECUTE_PHANTOM_SCRIPT ,
136
+ 'POST' , '/session/:sessionId/phantom/execute' ) ;
137
+
138
+ return executor ;
139
+ } ) ) ;
140
+ }
141
+
114
142
/**
115
143
* Creates a new WebDriver client for PhantomJS.
116
144
*
@@ -169,7 +197,7 @@ var Driver = function(opt_capabilities, opt_flow) {
169
197
} )
170
198
} ) ;
171
199
172
- var executor = executors . createExecutor ( service . start ( ) ) ;
200
+ var executor = createExecutor ( service . start ( ) ) ;
173
201
var driver = webdriver . WebDriver . createSession (
174
202
executor , capabilities , opt_flow ) ;
175
203
@@ -186,6 +214,54 @@ var Driver = function(opt_capabilities, opt_flow) {
186
214
util . inherits ( Driver , webdriver . WebDriver ) ;
187
215
188
216
217
+ /**
218
+ * Executes a PhantomJS fragment. This method is similar to
219
+ * {@link #executeScript}, except it exposes the
220
+ * <a href="http://phantomjs.org/api/">PhantomJS API</a> to the injected
221
+ * script.
222
+ *
223
+ * <p>The injected script will execute in the context of PhantomJS's
224
+ * {@code page} variable. If a page has not been loaded before calling this
225
+ * method, one will be created.</p>
226
+ *
227
+ * <p>Be sure to wrap callback definitions in a try/catch block, as failures
228
+ * may cause future WebDriver calls to fail.</p>
229
+ *
230
+ * <p>Certain callbacks are used by GhostDriver (the PhantomJS WebDriver
231
+ * implementation) and overriding these may cause the script to fail. It is
232
+ * recommended that you check for existing callbacks before defining your own.
233
+ * </p>
234
+ *
235
+ * As with {@link #executeScript}, the injected script may be defined as
236
+ * a string for an anonymous function body (e.g. "return 123;"), or as a
237
+ * function. If a function is provided, it will be decompiled to its original
238
+ * source. Note that injecting functions is provided as a convenience to
239
+ * simplify defining complex scripts. Care must be taken that the function
240
+ * only references variables that will be defined in the page's scope and
241
+ * that the function does not override {@code Function.prototype.toString}
242
+ * (overriding toString() will interfere with how the function is
243
+ * decompiled.
244
+ *
245
+ * @param {(string|!Function) } script The script to execute.
246
+ * @param {...* } var_args The arguments to pass to the script.
247
+ * @return {!webdriver.promise.Promise<T> } A promise that resolve to the
248
+ * script's return value.
249
+ * @template T
250
+ */
251
+ Driver . prototype . executePhantomJS = function ( script , args ) {
252
+ if ( typeof script === 'function' ) {
253
+ script = 'return (' + script + ').apply(this, arguments);' ;
254
+ }
255
+ var args = arguments . length > 1
256
+ ? Array . prototype . slice . call ( arguments , 1 ) : [ ] ;
257
+ return this . schedule (
258
+ new webdriver . Command ( Command . EXECUTE_PHANTOM_SCRIPT )
259
+ . setParameter ( 'script' , script )
260
+ . setParameter ( 'args' , args ) ,
261
+ 'Driver.executePhantomJS()' ) ;
262
+ } ;
263
+
264
+
189
265
// PUBLIC API
190
266
191
267
exports . Driver = Driver ;
0 commit comments