forked from webdriverio/expect-webdriverio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoHaveHeight.ts
More file actions
85 lines (72 loc) · 2.28 KB
/
toHaveHeight.ts
File metadata and controls
85 lines (72 loc) · 2.28 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { DEFAULT_OPTIONS } from '../../constants.js'
import type { WdioElementMaybePromise } from '../../types.js'
import {
compareNumbers,
enhanceError,
executeCommand,
numberError,
waitUntil,
} from '../../utils.js'
async function condition(el: WebdriverIO.Element, height: number, options: ExpectWebdriverIO.NumberOptions) {
const actualHeight = await el.getSize('height')
return {
result: compareNumbers(actualHeight, options),
value: actualHeight
}
}
export async function toHaveHeight(
received: WdioElementMaybePromise,
expectedValue: number | ExpectWebdriverIO.NumberOptions,
options: ExpectWebdriverIO.CommandOptions = DEFAULT_OPTIONS
) {
const isNot = this.isNot
const { expectation = 'height', verb = 'have' } = this
await options.beforeAssertion?.({
matcherName: 'toHaveHeight',
expectedValue,
options,
})
// type check
let numberOptions: ExpectWebdriverIO.NumberOptions
if (typeof expectedValue === 'number') {
numberOptions = { eq: expectedValue } as ExpectWebdriverIO.NumberOptions
} else if (!expectedValue || (typeof expectedValue.eq !== 'number' && typeof expectedValue.gte !== 'number' && typeof expectedValue.lte !== 'number')) {
throw new Error('Invalid params passed to toHaveHeight.')
} else {
numberOptions = expectedValue
}
let el = await received?.getElement()
let actualHeight
const pass = await waitUntil(
async () => {
const result = await executeCommand.call(this, el, condition, numberOptions, [expectedValue, numberOptions])
el = result.el as WebdriverIO.Element
actualHeight = result.values
return result.success
},
isNot,
{ ...numberOptions, ...options }
)
const error = numberError(numberOptions)
const message = enhanceError(
el,
error,
actualHeight,
this,
verb,
expectation,
'',
options
)
const result: ExpectWebdriverIO.AssertionResult = {
pass,
message: (): string => message
}
await options.afterAssertion?.({
matcherName: 'toHaveHeight',
expectedValue,
options,
result
})
return result
}