1
- const output = require ( './output' ) ;
2
- const store = require ( './store' ) ;
3
- const recorder = require ( './recorder' ) ;
4
- const container = require ( './container' ) ;
5
- const event = require ( './event' ) ;
6
- const Step = require ( './step' ) ;
7
- const { truth } = require ( './assert/truth' ) ;
8
- const { isAsyncFunction, humanizeFunction } = require ( './utils' ) ;
1
+ const output = require ( './output' )
2
+ const store = require ( './store' )
3
+ const container = require ( './container' )
4
+ const StepConfig = require ( './step/config' )
5
+ const recordStep = require ( './step/record' )
6
+ const FuncStep = require ( './step/func' )
7
+ const { truth } = require ( './assert/truth' )
8
+ const { isAsyncFunction, humanizeFunction } = require ( './utils' )
9
9
10
10
function element ( purpose , locator , fn ) {
11
- if ( ! fn ) {
12
- fn = locator ;
13
- locator = purpose ;
14
- purpose = 'first element' ;
11
+ let stepConfig
12
+ if ( arguments [ arguments . length - 1 ] instanceof StepConfig ) {
13
+ stepConfig = arguments [ arguments . length - 1 ]
15
14
}
16
15
17
- const step = prepareStep ( purpose , locator , fn ) ;
18
- if ( ! step ) return ;
16
+ if ( ! fn || fn === stepConfig ) {
17
+ fn = locator
18
+ locator = purpose
19
+ purpose = 'first element'
20
+ }
19
21
20
- return executeStep ( step , async ( ) => {
21
- const els = await step . helper . _locate ( locator ) ;
22
- output . debug ( `Found ${ els . length } elements, using first element` ) ;
22
+ const step = prepareStep ( purpose , locator , fn )
23
+ if ( ! step ) return
23
24
24
- return fn ( els [ 0 ] ) ;
25
- } ) ;
25
+ return executeStep (
26
+ step ,
27
+ async ( ) => {
28
+ const els = await step . helper . _locate ( locator )
29
+ output . debug ( `Found ${ els . length } elements, using first element` )
30
+
31
+ return fn ( els [ 0 ] )
32
+ } ,
33
+ stepConfig ,
34
+ )
26
35
}
27
36
28
37
function eachElement ( purpose , locator , fn ) {
29
38
if ( ! fn ) {
30
- fn = locator ;
31
- locator = purpose ;
32
- purpose = 'for each element' ;
39
+ fn = locator
40
+ locator = purpose
41
+ purpose = 'for each element'
33
42
}
34
43
35
- const step = prepareStep ( purpose , locator , fn ) ;
36
- if ( ! step ) return ;
44
+ const step = prepareStep ( purpose , locator , fn )
45
+ if ( ! step ) return
37
46
38
47
return executeStep ( step , async ( ) => {
39
- const els = await step . helper . _locate ( locator ) ;
40
- output . debug ( `Found ${ els . length } elements for each elements to iterate` ) ;
48
+ const els = await step . helper . _locate ( locator )
49
+ output . debug ( `Found ${ els . length } elements for each elements to iterate` )
41
50
42
- const errs = [ ] ;
43
- let i = 0 ;
51
+ const errs = [ ]
52
+ let i = 0
44
53
for ( const el of els ) {
45
54
try {
46
- await fn ( el , i ) ;
55
+ await fn ( el , i )
47
56
} catch ( err ) {
48
- output . error ( `eachElement: failed operation on element #${ i } ${ el } ` ) ;
49
- errs . push ( err ) ;
57
+ output . error ( `eachElement: failed operation on element #${ i } ${ el } ` )
58
+ errs . push ( err )
50
59
}
51
- i ++ ;
60
+ i ++
52
61
}
53
62
54
63
if ( errs . length ) {
55
- throw errs [ 0 ] ;
64
+ throw errs [ 0 ]
56
65
}
57
- } ) ;
66
+ } )
58
67
}
59
68
60
69
function expectElement ( locator , fn ) {
61
- const step = prepareStep ( 'expect element to be' , locator , fn ) ;
62
- if ( ! step ) return ;
70
+ const step = prepareStep ( 'expect element to be' , locator , fn )
71
+ if ( ! step ) return
63
72
64
73
return executeStep ( step , async ( ) => {
65
- const els = await step . helper . _locate ( locator ) ;
66
- output . debug ( `Found ${ els . length } elements, first will be used for assertion` ) ;
74
+ const els = await step . helper . _locate ( locator )
75
+ output . debug ( `Found ${ els . length } elements, first will be used for assertion` )
67
76
68
- const result = await fn ( els [ 0 ] ) ;
69
- const assertion = truth ( `element (${ locator } )` , fn . toString ( ) ) ;
70
- assertion . assert ( result ) ;
71
- } ) ;
77
+ const result = await fn ( els [ 0 ] )
78
+ const assertion = truth ( `element (${ locator } )` , fn . toString ( ) )
79
+ assertion . assert ( result )
80
+ } )
72
81
}
73
82
74
83
function expectAnyElement ( locator , fn ) {
75
- const step = prepareStep ( 'expect any element to be' , locator , fn ) ;
76
- if ( ! step ) return ;
84
+ const step = prepareStep ( 'expect any element to be' , locator , fn )
85
+ if ( ! step ) return
77
86
78
87
return executeStep ( step , async ( ) => {
79
- const els = await step . helper . _locate ( locator ) ;
80
- output . debug ( `Found ${ els . length } elements, at least one should pass the assertion` ) ;
88
+ const els = await step . helper . _locate ( locator )
89
+ output . debug ( `Found ${ els . length } elements, at least one should pass the assertion` )
81
90
82
- const assertion = truth ( `any element of (${ locator } )` , fn . toString ( ) ) ;
91
+ const assertion = truth ( `any element of (${ locator } )` , fn . toString ( ) )
83
92
84
- let found = false ;
93
+ let found = false
85
94
for ( const el of els ) {
86
- const result = await fn ( el ) ;
95
+ const result = await fn ( el )
87
96
if ( result ) {
88
- found = true ;
89
- break ;
97
+ found = true
98
+ break
90
99
}
91
100
}
92
- if ( ! found ) throw assertion . getException ( ) ;
93
- } ) ;
101
+ if ( ! found ) throw assertion . getException ( )
102
+ } )
94
103
}
95
104
96
105
function expectAllElements ( locator , fn ) {
97
- const step = prepareStep ( 'expect all elements' , locator , fn ) ;
98
- if ( ! step ) return ;
106
+ const step = prepareStep ( 'expect all elements' , locator , fn )
107
+ if ( ! step ) return
99
108
100
109
return executeStep ( step , async ( ) => {
101
- const els = await step . helper . _locate ( locator ) ;
102
- output . debug ( `Found ${ els . length } elements, all should pass the assertion` ) ;
110
+ const els = await step . helper . _locate ( locator )
111
+ output . debug ( `Found ${ els . length } elements, all should pass the assertion` )
103
112
104
- let i = 1 ;
113
+ let i = 1
105
114
for ( const el of els ) {
106
- output . debug ( `checking element #${ i } : ${ el } ` ) ;
107
- const result = await fn ( el ) ;
108
- const assertion = truth ( `element #${ i } of (${ locator } )` , humanizeFunction ( fn ) ) ;
109
- assertion . assert ( result ) ;
110
- i ++ ;
115
+ output . debug ( `checking element #${ i } : ${ el } ` )
116
+ const result = await fn ( el )
117
+ const assertion = truth ( `element #${ i } of (${ locator } )` , humanizeFunction ( fn ) )
118
+ assertion . assert ( result )
119
+ i ++
111
120
}
112
- } ) ;
121
+ } )
113
122
}
114
123
115
124
module . exports = {
@@ -118,60 +127,32 @@ module.exports = {
118
127
expectElement,
119
128
expectAnyElement,
120
129
expectAllElements,
121
- } ;
130
+ }
122
131
123
132
function prepareStep ( purpose , locator , fn ) {
124
- if ( store . dryRun ) return ;
125
- const helpers = Object . values ( container . helpers ( ) ) ;
133
+ if ( store . dryRun ) return
134
+ const helpers = Object . values ( container . helpers ( ) )
126
135
127
- const helper = helpers . filter ( h => ! ! h . _locate ) [ 0 ] ;
136
+ const helper = helpers . filter ( h => ! ! h . _locate ) [ 0 ]
128
137
129
138
if ( ! helper ) {
130
- throw new Error ( 'No helper enabled with _locate method with returns a list of elements.' ) ;
139
+ throw new Error ( 'No helper enabled with _locate method with returns a list of elements.' )
131
140
}
132
141
133
142
if ( ! isAsyncFunction ( fn ) ) {
134
- throw new Error ( 'Async function should be passed into each element' ) ;
143
+ throw new Error ( 'Async function should be passed into each element' )
135
144
}
136
145
137
- const isAssertion = purpose . startsWith ( 'expect' ) ;
146
+ const isAssertion = purpose . startsWith ( 'expect' )
138
147
139
- const step = new Step ( helper , `${ purpose } within "${ locator } " ${ isAssertion ? 'to be' : 'to' } ` ) ;
140
- step . setActor ( 'EL' ) ;
141
- step . setArguments ( [ humanizeFunction ( fn ) ] ) ;
142
- step . helperMethod = '_locate' ;
148
+ const step = new FuncStep ( `${ purpose } within "${ locator } " ${ isAssertion ? 'to be' : 'to' } ` )
149
+ step . setHelper ( helper )
150
+ step . setArguments ( [ humanizeFunction ( fn ) ] ) // user defined function is a passed argument
143
151
144
- return step ;
152
+ return step
145
153
}
146
154
147
- async function executeStep ( step , action ) {
148
- let error ;
149
- const promise = recorder . add ( 'register element wrapper' , async ( ) => {
150
- event . emit ( event . step . started , step ) ;
151
-
152
- try {
153
- await action ( ) ;
154
- } catch ( err ) {
155
- recorder . throw ( err ) ;
156
- event . emit ( event . step . failed , step , err ) ;
157
- event . emit ( event . step . finished , step ) ;
158
- // event.emit(event.step.after, step)
159
- error = err ;
160
- // await recorder.promise();
161
- return ;
162
- }
163
-
164
- event . emit ( event . step . after , step ) ;
165
- event . emit ( event . step . passed , step ) ;
166
- event . emit ( event . step . finished , step ) ;
167
- } ) ;
168
-
169
- // await recorder.promise();
170
-
171
- // if (error) {
172
- // console.log('error', error.inspect())
173
- // return recorder.throw(error);
174
- // }
175
-
176
- return promise ;
155
+ async function executeStep ( step , action , stepConfig = { } ) {
156
+ step . setCallable ( action )
157
+ return recordStep ( step , [ stepConfig ] )
177
158
}
0 commit comments