forked from webdriverio/expect-webdriverio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoHaveHTML.ts
More file actions
64 lines (53 loc) · 1.98 KB
/
toHaveHTML.ts
File metadata and controls
64 lines (53 loc) · 1.98 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 type { ChainablePromiseArray, ChainablePromiseElement } from 'webdriverio'
import { DEFAULT_OPTIONS } from '../../constants.js'
import {
compareText, compareTextWithArray,
enhanceError,
executeCommand,
waitUntil,
wrapExpectedWithArray
} from '../../utils.js'
async function condition(el: WebdriverIO.Element, html: string | RegExp | WdioAsymmetricMatcher<string> | Array<string | RegExp>, options: ExpectWebdriverIO.HTMLOptions) {
const actualHTML = await el.getHTML(options)
if (Array.isArray(html)) {
return compareTextWithArray(actualHTML, html, options)
}
return compareText(actualHTML, html, options)
}
export async function toHaveHTML(
received: ChainablePromiseArray | ChainablePromiseElement,
expectedValue: string | RegExp | WdioAsymmetricMatcher<string> | Array<string | RegExp>,
options: ExpectWebdriverIO.HTMLOptions = DEFAULT_OPTIONS
) {
const isNot = this.isNot
const { expectation = 'HTML', verb = 'have' } = this
await options.beforeAssertion?.({
matcherName: 'toHaveHTML',
expectedValue,
options,
})
let el = 'getElement' in received
? await received?.getElement()
: 'getElements' in received
? await received?.getElements()
: received
let actualHTML
const pass = await waitUntil(async () => {
const result = await executeCommand.call(this, el, condition, options, [expectedValue, options])
el = result.el as WebdriverIO.Element
actualHTML = result.values
return result.success
}, isNot, options)
const message = enhanceError(el, wrapExpectedWithArray(el, actualHTML, expectedValue), actualHTML, this, verb, expectation, '', options)
const result: ExpectWebdriverIO.AssertionResult = {
pass,
message: (): string => message
}
await options.afterAssertion?.({
matcherName: 'toHaveHTML',
expectedValue,
options,
result
})
return result
}