forked from webdriverio/expect-webdriverio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoBeElementsArrayOfSize.ts
More file actions
64 lines (55 loc) · 2.25 KB
/
toBeElementsArrayOfSize.ts
File metadata and controls
64 lines (55 loc) · 2.25 KB
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
import { waitUntil, enhanceError, compareNumbers, numberError } from '../../utils.js'
import { refetchElements } from '../../util/refetchElements.js'
import { DEFAULT_OPTIONS } from '../../constants.js'
import type { WdioElements, WdioElementsMaybePromise } from '../../types.js'
export async function toBeElementsArrayOfSize(
received: WdioElementsMaybePromise,
expectedValue: number | ExpectWebdriverIO.NumberOptions,
options: ExpectWebdriverIO.StringOptions = DEFAULT_OPTIONS
) {
const { expectation = 'elements array of size', verb = 'be' } = this
await options.beforeAssertion?.({
matcherName: 'toBeElementsArrayOfSize',
expectedValue,
options,
})
let numberOptions: ExpectWebdriverIO.NumberOptions
if (typeof expectedValue === 'number') {
numberOptions = { eq: expectedValue } satisfies ExpectWebdriverIO.NumberOptions
} else if (!expectedValue || (typeof expectedValue.eq !== 'number' && typeof expectedValue.gte !== 'number' && typeof expectedValue.lte !== 'number')) {
throw new Error('Invalid params passed to toBeElementsArrayOfSize.')
} else {
numberOptions = expectedValue
}
let elements = await received as WdioElements
const originalLength = elements.length
const pass = await waitUntil(async () => {
/**
* check numbers first before refetching elements
*/
const isPassing = compareNumbers(elements.length, numberOptions)
if (isPassing) {
return isPassing
}
elements = await refetchElements(elements, numberOptions.wait, true)
return false
}, { ...numberOptions, ...options })
if (Array.isArray(received) && pass) {
for (let index = originalLength; index < elements.length; index++) {
received.push(elements[index])
}
}
const error = numberError(numberOptions)
const message = enhanceError(elements, error, originalLength, this, verb, expectation, '', numberOptions)
const result: ExpectWebdriverIO.AssertionResult = {
pass,
message: (): string => message
}
await options.afterAssertion?.({
matcherName: 'toBeElementsArrayOfSize',
expectedValue,
options,
result
})
return result
}