@@ -59,6 +59,22 @@ function validateString(stringtoValidate) {
59
59
}
60
60
}
61
61
62
+ /**
63
+ * Calls a function once the control flow is idle
64
+ * @param {webdriver.promise.ControlFlow } flow The Web Driver control flow
65
+ * @param {!Function } fn The function to call
66
+ */
67
+ function callWhenIdle ( flow , fn ) {
68
+ if ( flow . isIdle ( ) ) {
69
+ fn ( ) ;
70
+ } else {
71
+ flow . once ( webdriver . promise . ControlFlow . EventType . IDLE , function ( ) {
72
+ fn ( ) ;
73
+ } ) ;
74
+ }
75
+ }
76
+
77
+
62
78
/**
63
79
* Wraps a function so it runs inside a webdriver.promise.ControlFlow and
64
80
* waits for the flow to complete before continuing.
@@ -78,30 +94,38 @@ function wrapInControlFlow(flow, globalFn, fnName) {
78
94
79
95
flow . execute ( function controlFlowExecute ( ) {
80
96
return new webdriver . promise . Promise ( function ( fulfill , reject ) {
97
+ function wrappedReject ( err ) {
98
+ var wrappedErr = new Error ( err ) ;
99
+ reject ( wrappedErr ) ;
100
+ }
81
101
if ( async ) {
82
102
// If testFn is async (it expects a done callback), resolve the promise of this
83
103
// test whenever that callback says to. Any promises returned from testFn are
84
104
// ignored.
85
105
var proxyDone = fulfill ;
86
- proxyDone . fail = function ( err ) {
87
- var wrappedErr = new Error ( err ) ;
88
- reject ( wrappedErr ) ;
89
- } ;
106
+ proxyDone . fail = wrappedReject ;
90
107
testFn ( proxyDone ) ;
91
108
} else {
92
109
// Without a callback, testFn can return a promise, or it will
93
110
// be assumed to have completed synchronously.
94
- fulfill ( testFn ( ) ) ;
111
+ var ret = testFn ( ) ;
112
+ if ( webdriver . promise . isPromise ( ret ) ) {
113
+ ret . then ( fulfill , wrappedReject ) ;
114
+ } else {
115
+ fulfill ( ret ) ;
116
+ }
95
117
}
96
118
} , flow ) ;
97
- } , 'Run ' + fnName + description + ' in control flow' ) . then ( seal ( done ) , function ( err ) {
98
- if ( ! err ) {
99
- err = new Error ( 'Unknown Error' ) ;
100
- err . stack = '' ;
119
+ } , 'Run ' + fnName + description + ' in control flow' ) . then (
120
+ callWhenIdle . bind ( null , flow , done ) , function ( err ) {
121
+ if ( ! err ) {
122
+ err = new Error ( 'Unknown Error' ) ;
123
+ err . stack = '' ;
124
+ }
125
+ err . stack = err . stack + '\nFrom asynchronous test: \n' + driverError . stack ;
126
+ callWhenIdle ( flow , done . fail . bind ( done , err ) ) ;
101
127
}
102
- err . stack = err . stack + '\nFrom asynchronous test: \n' + driverError . stack ;
103
- done . fail ( err ) ;
104
- } ) ;
128
+ ) ;
105
129
} ;
106
130
}
107
131
0 commit comments