@@ -429,34 +429,29 @@ class Executor {
429
429
return doSend ( this , request ) . then ( response => {
430
430
this . log_ . finer ( ( ) => `>>>\n${ request } \n<<<\n${ response } ` ) ;
431
431
432
- let parsed =
433
- parseHttpResponse (
434
- command , /** @type {!Response } */ ( response ) , this . w3c ) ;
432
+ let httpResponse = /** @type {!Response } */ ( response ) ;
433
+ let { isW3C, value} = parseHttpResponse ( command , httpResponse ) ;
435
434
436
435
if ( command . getName ( ) === cmd . Name . NEW_SESSION
437
436
|| command . getName ( ) === cmd . Name . DESCRIBE_SESSION ) {
438
- if ( ! parsed || ! parsed [ ' sessionId' ] ) {
437
+ if ( ! value || ! value . sessionId ) {
439
438
throw new error . WebDriverError (
440
- ' Unable to parse new session response: ' + response . body ) ;
439
+ ` Unable to parse new session response: ${ response . body } ` ) ;
441
440
}
442
441
443
442
// The remote end is a W3C compliant server if there is no `status`
444
443
// field in the response. This is not applicable for the DESCRIBE_SESSION
445
444
// command, which is not defined in the W3C spec.
446
445
if ( command . getName ( ) === cmd . Name . NEW_SESSION ) {
447
- this . w3c = this . w3c || ! ( 'status' in parsed ) ;
446
+ this . w3c = this . w3c || isW3C ;
448
447
}
449
448
450
- return new Session ( parsed [ 'sessionId' ] , parsed [ 'value' ] ) ;
449
+ // No implementations use the `capabilities` key yet...
450
+ let capabilities = value . capabilities || value . value ;
451
+ return new Session ( value . sessionId , capabilities ) ;
451
452
}
452
453
453
- if ( parsed
454
- && typeof parsed === 'object'
455
- && 'value' in parsed ) {
456
- let value = parsed [ 'value' ] ;
457
- return typeof value === 'undefined' ? null : value ;
458
- }
459
- return parsed ;
454
+ return typeof value === 'undefined' ? null : value ;
460
455
} ) ;
461
456
} ) ;
462
457
}
@@ -482,40 +477,45 @@ function tryParse(str) {
482
477
*
483
478
* @param {!cmd.Command } command The command the response is for.
484
479
* @param {!Response } httpResponse The HTTP response to parse.
485
- * @param {boolean } w3c Whether the response should be processed using the
486
- * W3C wire protocol.
487
- * @return {? } The parsed response.
480
+ * @return {{isW3C: boolean, value: ?} } An object describing the parsed
481
+ * response. This object will have two fields: `isW3C` indicates whether
482
+ * the response looks like it came from a remote end that conforms with the
483
+ * W3C WebDriver spec, and `value`, the actual response value.
488
484
* @throws {WebDriverError } If the HTTP response is an error.
489
485
*/
490
- function parseHttpResponse ( command , httpResponse , w3c ) {
491
- let parsed = tryParse ( httpResponse . body ) ;
492
- if ( parsed !== undefined ) {
493
- if ( httpResponse . status < 200 ) {
494
- // This should never happen, but throw the raw response so
495
- // users report it.
496
- throw new error . WebDriverError (
497
- `Unexpected HTTP response:\n${ httpResponse } ` ) ;
498
- }
486
+ function parseHttpResponse ( command , httpResponse ) {
487
+ if ( httpResponse . status < 200 ) {
488
+ // This should never happen, but throw the raw response so users report it.
489
+ throw new error . WebDriverError (
490
+ `Unexpected HTTP response:\n${ httpResponse } ` ) ;
491
+ }
499
492
500
- if ( w3c ) {
501
- if ( httpResponse . status > 399 ) {
502
- error . throwDecodedError ( parsed ) ;
493
+ let parsed = tryParse ( httpResponse . body ) ;
494
+ if ( parsed && typeof parsed === 'object' ) {
495
+ let value = parsed . value ;
496
+ let isW3C =
497
+ value !== null && typeof value === 'object'
498
+ && typeof parsed . status === 'undefined' ;
499
+
500
+ if ( ! isW3C ) {
501
+ error . checkLegacyResponse ( parsed ) ;
502
+
503
+ // Adjust legacy new session responses to look like W3C to simplify
504
+ // later processing.
505
+ if ( command . getName ( ) === cmd . Name . NEW_SESSION
506
+ || command . getName ( ) == cmd . Name . DESCRIBE_SESSION ) {
507
+ value = parsed ;
503
508
}
504
- return parsed ;
505
- }
506
509
507
- // If this is a new session command, we need to check for a W3C compliant
508
- // error object. This is necessary since a successful new session command
509
- // is what puts the executor into W3C mode.
510
- if ( httpResponse . status > 399
511
- && ( command . getName ( ) == cmd . Name . NEW_SESSION
512
- || command . getName ( ) === cmd . Name . DESCRIBE_SESSION )
513
- && error . isErrorResponse ( parsed ) ) {
514
- error . throwDecodedError ( parsed ) ;
510
+ } else if ( httpResponse . status > 399 ) {
511
+ error . throwDecodedError ( value ) ;
515
512
}
516
513
517
- error . checkLegacyResponse ( parsed ) ;
518
- return parsed ;
514
+ return { isW3C, value} ;
515
+ }
516
+
517
+ if ( parsed !== undefined ) {
518
+ return { isW3C : false , value : parsed } ;
519
519
}
520
520
521
521
let value = httpResponse . body . replace ( / \r \n / g, '\n' ) ;
@@ -528,7 +528,7 @@ function parseHttpResponse(command, httpResponse, w3c) {
528
528
throw new error . WebDriverError ( value ) ;
529
529
}
530
530
531
- return value || null ;
531
+ return { isW3C : false , value : value || null } ;
532
532
}
533
533
534
534
0 commit comments