forked from webdriverio/webdriverio
-
Notifications
You must be signed in to change notification settings - Fork 1
/
shim.ts
489 lines (431 loc) · 16.1 KB
/
shim.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
import iterators from 'p-iteration'
import logger from '@wdio/logger'
import { Clients } from '@wdio/types'
const log = logger('@wdio/utils:shim')
let inCommandHook = false
let hasWdioSyncSupport = false
let runSync: (this: unknown, fn: Function, repeatTest: any, args: unknown[]) => (resolve: Function, reject: Function) => unknown
interface Retries {
limit: number
attempts: number
}
interface WDIOSync {
runFnInFiberContext: any
wrapCommand: any
executeHooksWithArgs: any
executeSync: any
runSync: any
}
declare global {
var _HAS_FIBER_CONTEXT: boolean
var browser: any
var expectAsync: any
}
declare global {
namespace NodeJS {
interface Global {
expect: any
expectAsync: any
}
}
}
/**
* Jasmine differentiates between sync and async matchers.
* In order to offer a consistent experience WebdriverIO is
* replacing `expect` with `expectAsync` in every spec file
* that is async. Now to also allow assertions of literal values
* like string, numbers etc. in an async function we overwrite expect
* with this shim to check the input value. If we assert a promise,
* a browser or element object we use `expectAsync` otherwise the
* normal sync `expect`.
*
* Note: `syncMatcher` as parameter is only for testing purposes
*/
let expectSync: Function
export function expectAsyncShim (actual?: any, syncMatcher = expectSync) {
const expectAsync = global.expectAsync
const useSync = (
!actual ||
(
typeof actual.then !== 'function' &&
!actual.sessionId &&
!actual.elementId
)
)
if (useSync) {
return syncMatcher(actual)
}
return expectAsync(actual)
}
const ELEMENT_QUERY_COMMANDS = [
'$', '$$', 'custom$', 'custom$$', 'shadow$', 'shadow$$', 'react$',
'react$$', 'nextElement', 'previousElement', 'parentElement'
]
const ELEMENT_PROPS = [
'elementId', 'error', 'selector', 'parent', 'index', 'isReactElement',
'length'
]
const PROMISE_METHODS = ['then', 'catch', 'finally']
/**
* shim to make sure that we only wrap commands if wdio-sync is installed as dependency
*/
let wdioSync: WDIOSync | undefined
export let runAsync = false
export let asyncSpec = false
try {
const packageName = '@wdio/sync'
wdioSync = require(packageName)
hasWdioSyncSupport = true
/**
* only print within worker process
*/
if (process.send) {
log.warn(
'You are running tests with @wdio/sync which will be discontinued starting Node.js v16.' +
'Read more on https://github.com/webdriverio/webdriverio/discussions/6702'
)
}
} catch (err: any) {
runAsync = true
asyncSpec = true
}
let executeHooksWithArgs = async function executeHooksWithArgsShim<T> (hookName: string, hooks: Function | Function[] = [], args: any[] = []): Promise<(T | Error)[]> {
runAsync = true
/**
* make sure hooks are an array of functions
*/
if (!Array.isArray(hooks)) {
hooks = [hooks]
}
/**
* make sure args is an array since we are calling apply
*/
if (!Array.isArray(args)) {
args = [args]
}
const hooksPromises = hooks.map((hook) => new Promise<T | Error>((resolve) => {
let result
try {
result = hook.apply(null, args)
} catch (e: any) {
log.error(e.stack)
return resolve(e)
}
/**
* if a promise is returned make sure we don't have a catch handler
* so in case of a rejection it won't cause the hook to fail
*/
if (result && typeof result.then === 'function') {
return result.then(resolve, (e: Error) => {
log.error(e.stack)
resolve(e)
})
}
resolve(result)
}))
const start = Date.now()
const result = await Promise.all(hooksPromises)
if (hooksPromises.length) {
log.debug(`Finished to run "${hookName}" hook in ${Date.now() - start}ms`)
}
return result
}
let runFnInFiberContext = function (fn: Function) {
return function (this: any, ...args: any[]) {
runAsync = true
return Promise.resolve(fn.apply(this, args))
}
}
/**
* wrap command to enable before and after command to be executed
* @param commandName name of the command (e.g. getTitle)
* @param fn command function
*/
let wrapCommand = function wrapCommand<T>(commandName: string, fn: Function): (...args: any) => Promise<T> {
async function wrapCommandFn(this: any, ...args: any[]) {
const beforeHookArgs = [commandName, args]
if (!inCommandHook && this.options.beforeCommand) {
inCommandHook = true
await executeHooksWithArgs.call(this, 'beforeCommand', this.options.beforeCommand, beforeHookArgs)
inCommandHook = false
}
let commandResult
let commandError
try {
commandResult = await fn.apply(this, args)
} catch (err: any) {
commandError = err
}
if (!inCommandHook && this.options.afterCommand) {
inCommandHook = true
const afterHookArgs = [...beforeHookArgs, commandResult, commandError]
await executeHooksWithArgs.call(this, 'afterCommand', this.options.afterCommand, afterHookArgs)
inCommandHook = false
}
if (commandError) {
throw commandError
}
return commandResult
}
function wrapElementFn (promise: Promise<Clients.Browser>, cmd: Function, args: any[], prevInnerArgs?: { prop: string | number, args: any[] }): any {
return new Proxy(
Promise.resolve(promise).then((ctx: Clients.Browser) => cmd.call(ctx, ...args)),
{
get: (target, prop: string) => {
/**
* handle symbols, e.g. async iterators
*/
if (typeof prop === 'symbol') {
return () => ({
i: 0,
target,
async next () {
const elems = await this.target
if (!Array.isArray(elems)) {
throw new Error('Can not iterate over non array')
}
if (this.i < elems.length) {
return { value: elems[this.i++], done: false }
}
return { done: true }
}
})
}
/**
* if we access an index on an element array promise, e.g.:
* ```js
* const elems = await $$('foo')[2]
* ```
*/
const numValue = parseInt(prop, 10)
if (!isNaN(numValue)) {
return wrapElementFn(
target,
/**
* `this` is an array of WebdriverIO elements
*
* Note(Christian): types for elements are defined in the
* webdriverio package and not accessible here
*/
function (this: object[], index: number) {
return this[index]
},
[prop],
{ prop, args }
)
}
/**
* if we call a query method on a resolve promise, e.g.:
* ```js
* await $('foo').$('bar')
* ```
*/
if (ELEMENT_QUERY_COMMANDS.includes(prop) || prop.endsWith('$')) {
// this: WebdriverIO.Element
return wrapCommand(prop, function (this: any, ...args: any) {
return this[prop].apply(this, args)
})
}
/**
* if we call an array iterator function like map or forEach on an
* set of elements, e.g.:
* ```js
* await $('body').$('header').$$('div').map((elem) => elem.getLocation())
* ```
*/
if (commandName.endsWith('$$') && typeof iterators[prop as keyof typeof iterators] === 'function') {
return (mapIterator: Function) => wrapElementFn(
target,
function (this: never, mapIterator: Function): any {
// @ts-ignore
return iterators[prop](this, mapIterator)
},
[mapIterator]
)
}
/**
* allow to grab the length or other properties of fetched element set, e.g.:
* ```js
* const elemAmount = await $$('foo').length
* ```
*/
if (ELEMENT_PROPS.includes(prop)) {
return target.then((res) => res[prop])
}
/**
* allow to resolve an chained element query, e.g.:
* ```js
* const elem = await $('foo').$('bar')
* console.log(elem.selector) // "bar"
* ```
*/
if (PROMISE_METHODS.includes(prop)) {
return target[prop as 'then' | 'catch' | 'finally'].bind(target)
}
/**
* call a command on an element query, e.g.:
* ```js
* const tagName = await $('foo').$('bar').getTagName()
* ```
*/
return (...args: any[]) => target.then(async (elem) => {
if (!elem) {
let errMsg = 'Element could not be found'
const prevElem = await promise
if (Array.isArray(prevElem) && prevInnerArgs && prevInnerArgs.prop === 'get') {
errMsg = `Index out of bounds! $$(${prevInnerArgs.args[0]}) returned only ${prevElem.length} elements.`
}
throw new Error(errMsg)
}
return elem[prop](...args)
})
}
}
)
}
function chainElementQuery(this: Promise<Clients.Browser>, ...args: any[]): any {
return wrapElementFn(this, wrapCommandFn, args)
}
return function (this: Clients.Browser, ...args: any[]) {
/**
* use sync mode if:
* - @wdio/sync package is installed and can be resolved
* - if a global.browser is define so we run with wdio testrunner
* - we are in a fiber context (flag is set when outer function is wrapped into fibers context)
*
* also if we run command asynchronous and the command suppose to return an element, we
* apply `chainElementQuery` to allow chaining of these promises.
*/
const command = hasWdioSyncSupport && wdioSync && Boolean(global.browser) && !runAsync && !asyncSpec
? wdioSync!.wrapCommand(commandName, fn)
: ELEMENT_QUERY_COMMANDS.includes(commandName) || commandName.endsWith('$')
? chainElementQuery
: wrapCommandFn
return command.apply(this, args)
}
}
/**
* execute test or hook synchronously
*
* @param {Function} fn spec or hook method
* @param {Number} retries { limit: number, attempts: number }
* @param {Array} args arguments passed to hook
* @return {Promise} that gets resolved once test/hook is done or was retried enough
*/
async function executeSyncFn (this: any, fn: Function, retries: Retries, args: any[] = []): Promise<unknown> {
this.wdioRetries = retries.attempts
try {
runAsync = true
let res = fn.apply(this, args)
/**
* sometimes function result is Promise,
* we need to await result before proceeding
*/
if (res instanceof Promise) {
return await res
}
return res
} catch (err: any) {
if (retries.limit > retries.attempts) {
retries.attempts++
return await executeSyncFn.call(this, fn, retries, args)
}
return Promise.reject(err)
}
}
/**
* execute test or hook asynchronously
*
* @param {Function} fn spec or hook method
* @param {object} retries { limit: number, attempts: number }
* @param {Array} args arguments passed to hook
* @return {Promise} that gets resolved once test/hook is done or was retried enough
*/
async function executeAsync(this: any, fn: Function, retries: Retries, args: any[] = []): Promise<unknown> {
const isJasmine = global.jasmine && global.expectAsync
const asyncSpecBefore = asyncSpec
this.wdioRetries = retries.attempts
// @ts-ignore
expectSync = global.expect
if (isJasmine) {
// @ts-ignore
global.expect = expectAsyncShim
}
try {
runAsync = true
asyncSpec = true
const result = fn.apply(this, args)
if (result && typeof result.finally === 'function') {
result
.finally(() => (asyncSpec = asyncSpecBefore))
.catch((err: any) => err)
} else {
asyncSpec = asyncSpecBefore
}
return await result
} catch (err: any) {
if (retries.limit > retries.attempts) {
retries.attempts++
return await executeAsync.call(this, fn, retries, args)
}
throw err
} finally {
if (isJasmine) {
// @ts-ignore
global.expect = expectSync
}
}
}
let executeSync = executeSyncFn
/**
* Method to switch between sync and async execution. It allows to have async
* tests in between synchronous tests. `fn` can either return a promise (e.g. for `executeSync`)
* or a function (e.g. for `runSync`). In both cases we need to make sure that
* we flip `runAsync` flag to true to that commands are wrapped with the @wdio/sync
* wrapper.
*/
export function switchSyncFlag (fn: Function) {
return function (this: unknown, ...args: any[]) {
const switchFlag = runAsync
runAsync = false
const result = fn.apply(this, args)
if (typeof result.finally === 'function') {
runAsync = switchFlag
return result
}
if (typeof result === 'function') {
return function (this: any, ...args: any[]) {
const switchFlagWithinFn = runAsync
const res = result.apply(this, args)
if (typeof result.finally === 'function') {
return result.finally(() => (runAsync = switchFlagWithinFn))
}
runAsync = switchFlagWithinFn
return res
}
}
runAsync = switchFlag
return result
}
}
/**
* only require `@wdio/sync` if `WDIO_NO_SYNC_SUPPORT` which allows us to
* create a smoke test scenario to test actual absence of the package
* (internal use only)
*/
/* istanbul ignore if */
if (!process.env.WDIO_NO_SYNC_SUPPORT && hasWdioSyncSupport && wdioSync) {
runFnInFiberContext = switchSyncFlag(wdioSync.runFnInFiberContext)
executeHooksWithArgs = switchSyncFlag(wdioSync.executeHooksWithArgs)
executeSync = switchSyncFlag(wdioSync.executeSync)
runSync = switchSyncFlag(wdioSync.runSync)
}
export {
executeHooksWithArgs,
runFnInFiberContext,
wrapCommand,
hasWdioSyncSupport,
executeSync,
executeAsync,
runSync
}