6
6
7
7
var webdriver = require ( 'selenium-webdriver' ) ;
8
8
9
- /**
10
- * Wraps a function so that all passed arguments are ignored.
11
- * @param {!Function } fn The function to wrap.
12
- * @return {!Function } The wrapped function.
13
- */
14
- function seal ( fn ) {
15
- return function ( ) {
16
- fn ( ) ;
17
- } ;
18
- }
19
-
20
9
/**
21
10
* Validates that the parameter is a function.
22
11
* @param {Object } functionToValidate The function to validate.
@@ -59,10 +48,28 @@ function validateString(stringtoValidate) {
59
48
}
60
49
}
61
50
51
+ /**
52
+ * Calls a function once the control flow is idle
53
+ * @param {webdriver.promise.ControlFlow } flow The Web Driver control flow
54
+ * @param {!Function } fn The function to call
55
+ */
56
+ function callWhenIdle ( flow , fn ) {
57
+ if ( flow . isIdle ( ) ) {
58
+ fn ( ) ;
59
+ } else {
60
+ flow . once ( webdriver . promise . ControlFlow . EventType . IDLE , function ( ) {
61
+ fn ( ) ;
62
+ } ) ;
63
+ }
64
+ }
65
+
66
+
62
67
/**
63
68
* Wraps a function so it runs inside a webdriver.promise.ControlFlow and
64
69
* waits for the flow to complete before continuing.
70
+ * @param {!webdriver.promise.ControlFlow } flow The WebDriver control flow.
65
71
* @param {!Function } globalFn The function to wrap.
72
+ * @param {!string } fnName The name of the function being wrapped (e.g. `'it'`).
66
73
* @return {!Function } The new function.
67
74
*/
68
75
function wrapInControlFlow ( flow , globalFn , fnName ) {
@@ -78,30 +85,38 @@ function wrapInControlFlow(flow, globalFn, fnName) {
78
85
79
86
flow . execute ( function controlFlowExecute ( ) {
80
87
return new webdriver . promise . Promise ( function ( fulfill , reject ) {
88
+ function wrappedReject ( err ) {
89
+ var wrappedErr = new Error ( err ) ;
90
+ reject ( wrappedErr ) ;
91
+ }
81
92
if ( async ) {
82
93
// If testFn is async (it expects a done callback), resolve the promise of this
83
94
// test whenever that callback says to. Any promises returned from testFn are
84
95
// ignored.
85
96
var proxyDone = fulfill ;
86
- proxyDone . fail = function ( err ) {
87
- var wrappedErr = new Error ( err ) ;
88
- reject ( wrappedErr ) ;
89
- } ;
97
+ proxyDone . fail = wrappedReject ;
90
98
testFn ( proxyDone ) ;
91
99
} else {
92
100
// Without a callback, testFn can return a promise, or it will
93
101
// be assumed to have completed synchronously.
94
- fulfill ( testFn ( ) ) ;
102
+ var ret = testFn ( ) ;
103
+ if ( webdriver . promise . isPromise ( ret ) ) {
104
+ ret . then ( fulfill , wrappedReject ) ;
105
+ } else {
106
+ fulfill ( ret ) ;
107
+ }
95
108
}
96
109
} , 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 = '' ;
110
+ } , 'Run ' + fnName + description + ' in control flow' ) . then (
111
+ callWhenIdle . bind ( null , flow , done ) , function ( err ) {
112
+ if ( ! err ) {
113
+ err = new Error ( 'Unknown Error' ) ;
114
+ err . stack = '' ;
115
+ }
116
+ err . stack = err . stack + '\nFrom asynchronous test: \n' + driverError . stack ;
117
+ callWhenIdle ( flow , done . fail . bind ( done , err ) ) ;
101
118
}
102
- err . stack = err . stack + '\nFrom asynchronous test: \n' + driverError . stack ;
103
- done . fail ( err ) ;
104
- } ) ;
119
+ ) ;
105
120
} ;
106
121
}
107
122
0 commit comments