@@ -19,6 +19,48 @@ function seal(fn) {
19
19
} ;
20
20
} ;
21
21
22
+ /**
23
+ * Validates that the parameter is a function.
24
+ * @param {Object } functionToValidate The function to validate.
25
+ * @throws {Error }
26
+ * @return {Object } The original parameter.
27
+ */
28
+ function validateFunction ( functionToValidate ) {
29
+ if ( functionToValidate && Object . prototype . toString . call ( functionToValidate ) === '[object Function]' ) {
30
+ return functionToValidate ;
31
+ } else {
32
+ throw Error ( functionToValidate + ' is not a function' ) ;
33
+ }
34
+ }
35
+
36
+ /**
37
+ * Validates that the parameter is a number.
38
+ * @param {Object } numberToValidate The number to validate.
39
+ * @throws {Error }
40
+ * @return {Object } The original number.
41
+ */
42
+ function validateNumber ( numberToValidate ) {
43
+ if ( ! isNaN ( numberToValidate ) ) {
44
+ return numberToValidate ;
45
+ } else {
46
+ throw Error ( numberToValidate + ' is not a number' ) ;
47
+ }
48
+ }
49
+
50
+ /**
51
+ * Validates that the parameter is a string.
52
+ * @param {Object } stringToValidate The string to validate.
53
+ * @throws {Error }
54
+ * @return {Object } The original string.
55
+ */
56
+ function validateString ( stringtoValidate ) {
57
+ if ( typeof stringtoValidate == 'string' || stringtoValidate instanceof String ) {
58
+ return stringtoValidate ;
59
+ } else {
60
+ throw Error ( stringtoValidate + ' is not a string' ) ;
61
+ }
62
+ }
63
+
22
64
/**
23
65
* Wraps a function so it runs inside a webdriver.promise.ControlFlow and
24
66
* waits for the flow to complete before continuing.
@@ -27,48 +69,55 @@ function seal(fn) {
27
69
*/
28
70
function wrapInControlFlow ( globalFn , fnName ) {
29
71
return function ( ) {
30
-
31
72
var driverError = new Error ( ) ;
32
73
driverError . stack = driverError . stack . replace ( / + a t .+ j a s m i n e w d .+ \n / , '' ) ;
33
74
34
- var description = 'Asynchronous test function: ' + fnName + '(' ;
35
- if ( arguments . length >= 2 ) {
36
- description += '"' + arguments [ 0 ] + '"' ;
37
- }
38
- description += ')' ;
39
-
40
- function asyncTestFn ( fn ) {
75
+ function asyncTestFn ( fn , desc ) {
41
76
return function ( done ) {
77
+ var desc_ = 'Asynchronous test function: ' + fnName + '(' ;
78
+ if ( desc ) {
79
+ desc_ += '"' + desc + '"' ;
80
+ }
81
+ desc_ += ')' ;
82
+
42
83
flow . execute ( function ( ) {
43
84
fn . call ( jasmine . getEnv ( ) . currentSpec , function ( ) {
44
85
throw new Error ( 'Do not use a done callback with WebDriverJS tests. ' +
45
86
'The tests are patched to be asynchronous and will terminate when ' +
46
87
'the webdriver control flow is empty.' ) ;
47
88
} ) ;
48
- } , description ) . then ( seal ( done ) , function ( e ) {
89
+ } , desc_ ) . then ( seal ( done ) , function ( e ) {
49
90
e . stack = e . stack + '==== async task ====\n' + driverError . stack ;
50
91
done ( e ) ;
51
92
} ) ;
52
93
} ;
53
94
} ;
54
95
55
- switch ( arguments . length ) {
56
- case 1 :
57
- globalFn ( asyncTestFn ( arguments [ 0 ] ) ) ;
58
- break ;
59
-
60
- case 2 :
61
- // The first variable is a description.
62
- globalFn ( arguments [ 0 ] , asyncTestFn ( arguments [ 1 ] ) ) ;
96
+ var description , func , timeout ;
97
+ switch ( fnName ) {
98
+ case 'it' :
99
+ case 'iit' :
100
+ description = validateString ( arguments [ 0 ] ) ;
101
+ func = validateFunction ( arguments [ 1 ] ) ;
102
+ if ( ! arguments [ 2 ] ) {
103
+ globalFn ( description , asyncTestFn ( func ) ) ;
104
+ } else {
105
+ timeout = validateNumber ( arguments [ 2 ] ) ;
106
+ globalFn ( description , asyncTestFn ( func ) , timeout ) ;
107
+ }
63
108
break ;
64
-
65
- case 3 :
66
- // The third variable should be the timeout in ms.
67
- globalFn ( arguments [ 0 ] , asyncTestFn ( arguments [ 1 ] ) , arguments [ 2 ] ) ;
109
+ case 'beforeEach' :
110
+ case 'afterEach' :
111
+ func = validateFunction ( arguments [ 0 ] ) ;
112
+ if ( ! arguments [ 1 ] ) {
113
+ globalFn ( asyncTestFn ( func ) ) ;
114
+ } else {
115
+ timeout = validateNumber ( arguments [ 1 ] ) ;
116
+ globalFn ( asyncTestFn ( func ) , timeout ) ;
117
+ }
68
118
break ;
69
-
70
119
default :
71
- throw Error ( 'Invalid # arguments : ' + arguments . length ) ;
120
+ throw Error ( 'invalid function : ' + fnName ) ;
72
121
}
73
122
} ;
74
123
} ;
0 commit comments