forked from webdriverio/expect-webdriverio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoHaveElementProperty.ts
More file actions
89 lines (75 loc) · 2.56 KB
/
toHaveElementProperty.ts
File metadata and controls
89 lines (75 loc) · 2.56 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
86
87
88
89
import { DEFAULT_OPTIONS } from '../../constants.js'
import type { WdioElementMaybePromise } from '../../types.js'
import {
compareText,
enhanceError,
executeCommand,
waitUntil,
wrapExpectedWithArray
} from '../../utils.js'
async function condition(
el: WebdriverIO.Element,
property: string,
value: unknown,
options: ExpectWebdriverIO.StringOptions = DEFAULT_OPTIONS
) {
const { asString = false } = options
let prop = await el.getProperty(property)
// As specified in the w3c spec, cases where property does not exist
if (prop === null || prop === undefined) {
return { result: false, value: prop }
}
// As specified in the w3c spec, cases where property simply exists, missing undefined here?
if (value === null) {
return { result: true, value: prop }
}
if (!(value instanceof RegExp) && typeof prop !== 'string' && !asString) {
return { result: prop === value, value: prop }
}
prop = prop.toString()
return compareText(prop as string, value as string | RegExp | WdioAsymmetricMatcher<string>, options)
}
export async function toHaveElementProperty(
received: WdioElementMaybePromise,
property: string,
value?: string | RegExp | WdioAsymmetricMatcher<string> | null,
options: ExpectWebdriverIO.StringOptions = DEFAULT_OPTIONS
) {
const isNot = this.isNot
const { expectation = 'property', verb = 'have' } = this
await options.beforeAssertion?.({
matcherName: 'toHaveElementProperty',
expectedValue: [property, value],
options,
})
let el = await received?.getElement()
let prop: unknown
const pass = await waitUntil(
async () => {
const result = await executeCommand.call(this, el, condition, options, [property, value])
el = result.el as WebdriverIO.Element
prop = result.values
return result.success
},
isNot,
options
)
let message: string
if (value === undefined) {
message = enhanceError(el, !isNot, pass, this, verb, expectation, property, options)
} else {
const expected = wrapExpectedWithArray(el, prop, value)
message = enhanceError(el, expected, prop, this, verb, expectation, property, options)
}
const result: ExpectWebdriverIO.AssertionResult = {
pass,
message: (): string => message
}
await options.afterAssertion?.({
matcherName: 'toHaveElementProperty',
expectedValue: [property, value],
options,
result
})
return result
}