1
+ var util = require ( 'util' ) ;
1
2
var url = require ( 'url' ) ;
2
3
var webdriver = require ( 'selenium-webdriver' ) ;
3
4
var helper = require ( './util' ) ;
@@ -698,14 +699,7 @@ Protractor.prototype.initDebugger_ = function(debuggerClientPath, opt_debugPort)
698
699
self . execPromiseResult_ = self . execPromiseError_ = undefined ;
699
700
700
701
self . execPromise_ = self . execPromise_ .
701
- then ( function ( ) {
702
- var result = execFn_ ( ) ;
703
- if ( webdriver . promise . isPromise ( result ) ) {
704
- return result . then ( function ( val ) { return val ; } ) ;
705
- } else {
706
- return result ;
707
- }
708
- } ) . then ( function ( result ) {
702
+ then ( execFn_ ) . then ( function ( result ) {
709
703
self . execPromiseResult_ = result ;
710
704
} , function ( err ) {
711
705
self . execPromiseError_ = err ;
@@ -720,22 +714,42 @@ Protractor.prototype.initDebugger_ = function(debuggerClientPath, opt_debugPort)
720
714
} ,
721
715
722
716
// Execute a piece of code.
717
+ // Result is a string representation of the evaluation.
723
718
execute : function ( code ) {
724
719
var execFn_ = function ( ) {
725
720
// Run code through vm so that we can maintain a local scope which is
726
721
// isolated from the rest of the execution.
727
- return vm_ . runInThisContext ( code ) ;
722
+ var res = vm_ . runInThisContext ( code ) ;
723
+ if ( ! webdriver . promise . isPromise ( res ) ) {
724
+ res = webdriver . promise . fulfilled ( res ) ;
725
+ }
726
+
727
+ return res . then ( function ( res ) {
728
+ if ( res === undefined ) {
729
+ return undefined ;
730
+ } else {
731
+ // The '' forces res to be expanded into a string instead of just
732
+ // '[Object]'. Then we remove the extra space caused by the '' using
733
+ // substring.
734
+ return util . format . apply ( this , [ '' , res ] ) . substring ( 1 ) ;
735
+ }
736
+ } ) ;
728
737
} ;
729
738
this . execute_ ( execFn_ ) ;
730
739
} ,
731
740
732
741
// Autocomplete for a line.
742
+ // Result is a JSON representation of the autocomplete response.
733
743
complete : function ( line ) {
734
744
var self = this ;
735
745
var execFn_ = function ( ) {
736
746
var deferred = webdriver . promise . defer ( ) ;
737
747
self . replServer_ . complete ( line , function ( err , res ) {
738
- deferred . fulfill ( res , err ) ;
748
+ if ( err ) {
749
+ deferred . reject ( err ) ;
750
+ } else {
751
+ deferred . fulfill ( JSON . stringify ( res ) ) ;
752
+ }
739
753
} ) ;
740
754
return deferred ;
741
755
} ;
@@ -756,8 +770,7 @@ Protractor.prototype.initDebugger_ = function(debuggerClientPath, opt_debugPort)
756
770
if ( this . execPromiseError_ ) {
757
771
throw this . execPromiseError_ ;
758
772
}
759
-
760
- return JSON . stringify ( this . execPromiseResult_ ) ;
773
+ return this . execPromiseResult_ ;
761
774
}
762
775
} ;
763
776
0 commit comments