forked from webdriverio/expect-webdriverio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoHaveClass.ts
More file actions
91 lines (76 loc) · 2.88 KB
/
toHaveClass.ts
File metadata and controls
91 lines (76 loc) · 2.88 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
import { DEFAULT_OPTIONS } from '../../constants.js'
import type { WdioElementMaybePromise } from '../../types.js'
import { compareText, compareTextWithArray, enhanceError, executeCommand, isAsymmetricMatcher, waitUntil, wrapExpectedWithArray } from '../../utils.js'
import { toHaveAttributeAndValue } from './toHaveAttribute.js'
async function condition(el: WebdriverIO.Element, attribute: string, value: string | RegExp | Array<string | RegExp> | WdioAsymmetricMatcher<string>, options: ExpectWebdriverIO.StringOptions) {
const actualClass = await el.getAttribute(attribute)
if (typeof actualClass !== 'string') {
return { result: false }
}
/**
* if value is an asymmetric matcher, no need to split class names
* into an array and compare each of them
*/
if (isAsymmetricMatcher(value)) {
return compareText(actualClass, value, options)
}
const classes = actualClass.split(' ')
const isValueInClasses = classes.some((t) => {
return Array.isArray(value)
? compareTextWithArray(t, value, options).result
: compareText(t, value, options).result
})
return {
value: actualClass,
result: isValueInClasses
}
}
/**
* @deprecated
*/
export function toHaveClass(...args: unknown[]) {
return toHaveElementClass.call(this || {}, ...args)
}
export async function toHaveElementClass(
received: WdioElementMaybePromise,
expectedValue: string | RegExp | Array<string | RegExp> | WdioAsymmetricMatcher<string>,
options: ExpectWebdriverIO.StringOptions = DEFAULT_OPTIONS
) {
const isNot = this.isNot
const { expectation = 'class', verb = 'have' } = this
await options.beforeAssertion?.({
matcherName: 'toHaveElementClass',
expectedValue,
options,
})
const attribute = 'class'
let el = await received?.getElement()
let attr
const pass = await waitUntil(async () => {
const result = await executeCommand.call(this, el, condition, options, [attribute, expectedValue, options])
el = result.el as WebdriverIO.Element
attr = result.values
return result.success
}, isNot, options)
const message = enhanceError(el, wrapExpectedWithArray(el, attr, expectedValue), attr, this, verb, expectation, '', options)
const result: ExpectWebdriverIO.AssertionResult = {
pass,
message: (): string => message
}
await options.afterAssertion?.({
matcherName: 'toHaveElementClass',
expectedValue,
options,
result
})
return result
}
/**
* @deprecated
*/
export function toHaveClassContaining(el: WebdriverIO.Element, className: string | RegExp | WdioAsymmetricMatcher<string>, options: ExpectWebdriverIO.StringOptions = DEFAULT_OPTIONS) {
return toHaveAttributeAndValue.call(this, el, 'class', className, {
...options,
containing: true
})
}