@@ -29,8 +29,8 @@ const cmd = require('./command');
29
29
const error = require ( './error' ) ;
30
30
const logging = require ( './logging' ) ;
31
31
const promise = require ( './promise' ) ;
32
- const Session = require ( './session' ) . Session ;
33
- const WebElement = require ( './webdriver' ) . WebElement ;
32
+ const { Session} = require ( './session' ) ;
33
+ const { WebElement} = require ( './webdriver' ) ;
34
34
35
35
const { getAttribute, isDisplayed} = /** @suppress {undefinedVars|uselessCode} */ ( function ( ) {
36
36
try {
@@ -145,7 +145,7 @@ function resource(method, path) { return {method: method, path: path}; }
145
145
var CommandSpec ;
146
146
147
147
148
- /** @typedef {function(!cmd.Command): !Promise<! cmd.Command> } */
148
+ /** @typedef {function(!cmd.Command): !cmd.Command } */
149
149
var CommandTransformer ;
150
150
151
151
@@ -155,21 +155,17 @@ class InternalTypeError extends TypeError {}
155
155
/**
156
156
* @param {!cmd.Command } command The initial command.
157
157
* @param {Atom } atom The name of the atom to execute.
158
- * @return {!Promise<! cmd.Command> } The transformed command to execute.
158
+ * @return {!cmd.Command } The transformed command to execute.
159
159
*/
160
160
function toExecuteAtomCommand ( command , atom , ...params ) {
161
- return new Promise ( ( resolve , reject ) => {
162
- if ( typeof atom !== 'function' ) {
163
- reject ( new InternalTypeError ( 'atom is not a function: ' + typeof atom ) ) ;
164
- return ;
165
- }
161
+ if ( typeof atom !== 'function' ) {
162
+ throw new InternalTypeError ( 'atom is not a function: ' + typeof atom ) ;
163
+ }
166
164
167
- let newCmd = new cmd . Command ( cmd . Name . EXECUTE_SCRIPT )
168
- . setParameter ( 'sessionId' , command . getParameter ( 'sessionId' ) )
169
- . setParameter ( 'script' , `return (${ atom } ).apply(null, arguments)` )
170
- . setParameter ( 'args' , params . map ( param => command . getParameter ( param ) ) ) ;
171
- resolve ( newCmd ) ;
172
- } ) ;
165
+ return new cmd . Command ( cmd . Name . EXECUTE_SCRIPT )
166
+ . setParameter ( 'sessionId' , command . getParameter ( 'sessionId' ) )
167
+ . setParameter ( 'script' , `return (${ atom } ).apply(null, arguments)` )
168
+ . setParameter ( 'args' , params . map ( param => command . getParameter ( param ) ) ) ;
173
169
}
174
170
175
171
@@ -287,35 +283,13 @@ class Client {
287
283
}
288
284
289
285
290
- const CLIENTS =
291
- /** !WeakMap<!Executor, !(Client|IThenable<!Client>)> */ new WeakMap ;
292
-
293
-
294
- /**
295
- * Sends a request using the given executor.
296
- * @param {!Executor } executor
297
- * @param {!Request } request
298
- * @return {!Promise<Response> }
299
- */
300
- function doSend ( executor , request ) {
301
- const client = CLIENTS . get ( executor ) ;
302
- if ( promise . isPromise ( client ) ) {
303
- return client . then ( client => {
304
- CLIENTS . set ( executor , client ) ;
305
- return client . send ( request ) ;
306
- } ) ;
307
- } else {
308
- return client . send ( request ) ;
309
- }
310
- }
311
-
312
286
313
287
/**
314
288
* @param {Map<string, CommandSpec> } customCommands
315
289
* A map of custom command definitions.
316
290
* @param {boolean } w3c Whether to use W3C command mappings.
317
291
* @param {!cmd.Command } command The command to resolve.
318
- * @return {!Promise<! Request> } A promise that will resolve with the
292
+ * @return {!Request } A promise that will resolve with the
319
293
* command to execute.
320
294
*/
321
295
function buildRequest ( customCommands , w3c , command ) {
@@ -329,8 +303,8 @@ function buildRequest(customCommands, w3c, command) {
329
303
spec = W3C_COMMAND_MAP . get ( command . getName ( ) ) ;
330
304
if ( typeof spec === 'function' ) {
331
305
LOG . finest ( ( ) => `Transforming command for W3C: ${ command . getName ( ) } ` ) ;
332
- return spec ( command )
333
- . then ( newCommand => buildRequest ( customCommands , w3c , newCommand ) ) ;
306
+ let newCommand = spec ( command ) ;
307
+ return buildRequest ( customCommands , w3c , newCommand ) ;
334
308
} else if ( spec ) {
335
309
return toHttpRequest ( spec ) ;
336
310
}
@@ -340,23 +314,26 @@ function buildRequest(customCommands, w3c, command) {
340
314
if ( spec ) {
341
315
return toHttpRequest ( spec ) ;
342
316
}
343
- return Promise . reject (
344
- new error . UnknownCommandError (
345
- 'Unrecognized command: ' + command . getName ( ) ) ) ;
317
+ throw new error . UnknownCommandError (
318
+ 'Unrecognized command: ' + command . getName ( ) ) ;
346
319
347
320
/**
348
321
* @param {CommandSpec } resource
349
- * @return {!Promise<! Request> }
322
+ * @return {!Request }
350
323
*/
351
324
function toHttpRequest ( resource ) {
352
325
LOG . finest ( ( ) => `Building HTTP request: ${ JSON . stringify ( resource ) } ` ) ;
353
326
let parameters = command . getParameters ( ) ;
354
327
let path = buildPath ( resource . path , parameters ) ;
355
- return Promise . resolve ( new Request ( resource . method , path , parameters ) ) ;
328
+ return new Request ( resource . method , path , parameters ) ;
356
329
}
357
330
}
358
331
359
332
333
+ const CLIENTS =
334
+ /** !WeakMap<!Executor, !(Client|IThenable<!Client>)> */ new WeakMap ;
335
+
336
+
360
337
/**
361
338
* A command executor that communicates with the server using JSON over HTTP.
362
339
*
@@ -416,36 +393,41 @@ class Executor {
416
393
}
417
394
418
395
/** @override */
419
- execute ( command ) {
396
+ async execute ( command ) {
420
397
let request = buildRequest ( this . customCommands_ , this . w3c , command ) ;
421
- return request . then ( request => {
422
- this . log_ . finer ( ( ) => `>>> ${ request . method } ${ request . path } ` ) ;
423
- return doSend ( this , request ) . then ( response => {
424
- this . log_ . finer ( ( ) => `>>>\n${ request } \n<<<\n${ response } ` ) ;
425
-
426
- let httpResponse = /** @type {!Response } */ ( response ) ;
427
- let { isW3C, value} = parseHttpResponse ( command , httpResponse ) ;
428
-
429
- if ( command . getName ( ) === cmd . Name . NEW_SESSION ) {
430
- if ( ! value || ! value . sessionId ) {
431
- throw new error . WebDriverError (
432
- `Unable to parse new session response: ${ response . body } ` ) ;
433
- }
434
-
435
- // The remote end is a W3C compliant server if there is no `status`
436
- // field in the response.
437
- if ( command . getName ( ) === cmd . Name . NEW_SESSION ) {
438
- this . w3c = this . w3c || isW3C ;
439
- }
440
-
441
- // No implementations use the `capabilities` key yet...
442
- let capabilities = value . capabilities || value . value ;
443
- return new Session ( value . sessionId , capabilities ) ;
444
- }
398
+ this . log_ . finer ( ( ) => `>>> ${ request . method } ${ request . path } ` ) ;
399
+
400
+ let client = CLIENTS . get ( this ) ;
401
+ if ( promise . isPromise ( client ) ) {
402
+ client = await client ;
403
+ CLIENTS . set ( this , client ) ;
404
+ }
405
+
406
+ let response = await client . send ( request ) ;
407
+ this . log_ . finer ( ( ) => `>>>\n${ request } \n<<<\n${ response } ` ) ;
408
+
409
+ let httpResponse = /** @type {!Response } */ ( response ) ;
410
+ let { isW3C, value} = parseHttpResponse ( command , httpResponse ) ;
411
+
412
+ if ( command . getName ( ) === cmd . Name . NEW_SESSION ) {
413
+ if ( ! value || ! value . sessionId ) {
414
+ throw new error . WebDriverError (
415
+ `Unable to parse new session response: ${ response . body } ` ) ;
416
+ }
417
+
418
+ // The remote end is a W3C compliant server if there is no `status`
419
+ // field in the response.
420
+ if ( command . getName ( ) === cmd . Name . NEW_SESSION ) {
421
+ this . w3c = this . w3c || isW3C ;
422
+ }
423
+
424
+ // No implementations use the `capabilities` key yet...
425
+ let capabilities = value . capabilities || value . value ;
426
+ return new Session (
427
+ /** @type {{sessionId: string} } */ ( value ) . sessionId , capabilities ) ;
428
+ }
445
429
446
- return typeof value === 'undefined' ? null : value ;
447
- } ) ;
448
- } ) ;
430
+ return typeof value === 'undefined' ? null : value ;
449
431
}
450
432
}
451
433
0 commit comments