forked from webdriverio/expect-webdriverio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoHaveAttribute.ts
More file actions
101 lines (81 loc) · 3.3 KB
/
toHaveAttribute.ts
File metadata and controls
101 lines (81 loc) · 3.3 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
90
91
92
93
94
95
96
97
98
99
100
101
import { DEFAULT_OPTIONS } from '../../constants.js'
import type { WdioElementMaybePromise } from '../../types.js'
import {
compareText,
enhanceError,
executeCommand,
waitUntil,
wrapExpectedWithArray
} from '../../utils.js'
async function conditionAttr(el: WebdriverIO.Element, attribute: string) {
const attr = await el.getAttribute(attribute)
if (typeof attr !== 'string') {
return { result: false, value: attr }
}
return { result: true, value: attr }
}
async function conditionAttrAndValue(el: WebdriverIO.Element, attribute: string, value: string | RegExp | WdioAsymmetricMatcher<string>, options: ExpectWebdriverIO.StringOptions) {
const attr = await el.getAttribute(attribute)
if (typeof attr !== 'string') {
return { result: false, value: attr }
}
return compareText(attr, value, options)
}
export async function toHaveAttributeAndValue(received: WdioElementMaybePromise, attribute: string, value: string | RegExp | WdioAsymmetricMatcher<string>, options: ExpectWebdriverIO.StringOptions = DEFAULT_OPTIONS) {
const isNot = this.isNot
const { expectation = 'attribute', verb = 'have' } = this
let el = await received?.getElement()
let attr
const pass = await waitUntil(async () => {
const result = await executeCommand.call(this, el, conditionAttrAndValue, options, [attribute, value, options])
el = result.el as WebdriverIO.Element
attr = result.values
return result.success
}, isNot, options)
const expected = wrapExpectedWithArray(el, attr, value)
const message = enhanceError(el, expected, attr, this, verb, expectation, attribute, options)
return {
pass,
message: (): string => message
} as ExpectWebdriverIO.AssertionResult
}
async function toHaveAttributeFn(received: WdioElementMaybePromise, attribute: string) {
const isNot = this.isNot
const { expectation = 'attribute', verb = 'have' } = this
let el = await received?.getElement()
const pass = await waitUntil(async () => {
const result = await executeCommand.call(this, el, conditionAttr, {}, [attribute])
el = result.el as WebdriverIO.Element
return result.success
}, isNot, {})
const message = enhanceError(el, !isNot, pass, this, verb, expectation, attribute, {})
return {
pass,
message: (): string => message
}
}
export async function toHaveAttribute(
received: WdioElementMaybePromise,
attribute: string,
value?: string | RegExp | WdioAsymmetricMatcher<string>,
options: ExpectWebdriverIO.StringOptions = DEFAULT_OPTIONS
) {
await options.beforeAssertion?.({
matcherName: 'toHaveAttribute',
expectedValue: [attribute, value],
options,
})
const result = value !== undefined
// Name and value is passed in e.g. el.toHaveAttribute('attr', 'value', (opts))
? await toHaveAttributeAndValue.call(this, received, attribute, value, options)
// Only name is passed in e.g. el.toHaveAttribute('attr')
: await toHaveAttributeFn.call(this, received, attribute)
await options.afterAssertion?.({
matcherName: 'toHaveAttribute',
expectedValue: [attribute, value],
options,
result
})
return result
}
export const toHaveAttr = toHaveAttribute