-
-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathtoHaveText.ts
More file actions
82 lines (71 loc) · 2.83 KB
/
toHaveText.ts
File metadata and controls
82 lines (71 loc) · 2.83 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
import type { ChainablePromiseElement, ChainablePromiseArray } from 'webdriverio'
import { DEFAULT_OPTIONS } from '../../constants.js'
import {
compareText, compareTextWithArray,
enhanceError,
executeCommand,
waitUntil,
wrapExpectedWithArray
} from '../../utils.js'
async function condition(el: WebdriverIO.Element | WebdriverIO.ElementArray, text: string | RegExp | Array<string | RegExp> | WdioAsymmetricMatcher<string>, options: ExpectWebdriverIO.StringOptions) {
const actualTextArray: string[] = []
const resultArray: boolean[] = []
let checkAllValuesMatchCondition: boolean
if (Array.isArray(el)){
for (const element of el){
const actualText = await element.getText()
actualTextArray.push(actualText)
const result = Array.isArray(text)
? compareTextWithArray(actualText, text, options).result
: compareText(actualText, text, options).result
resultArray.push(result)
}
checkAllValuesMatchCondition = resultArray.every(Boolean)
} else {
const actualText = await (el as WebdriverIO.Element).getText()
actualTextArray.push(actualText)
checkAllValuesMatchCondition = Array.isArray(text)
? compareTextWithArray(actualText, text, options).result
: compareText(actualText, text, options).result
}
return {
value: actualTextArray.length === 1 ? actualTextArray[0] : actualTextArray,
result: checkAllValuesMatchCondition
}
}
export async function toHaveText(
received: ChainablePromiseElement | ChainablePromiseArray | WebdriverIO.Element | WebdriverIO.ElementArray,
expectedValue: string | RegExp | WdioAsymmetricMatcher<string> | Array<string | RegExp>,
options: ExpectWebdriverIO.StringOptions = DEFAULT_OPTIONS
) {
const { expectation = 'text', verb = 'have' } = this
await options.beforeAssertion?.({
matcherName: 'toHaveText',
expectedValue,
options,
})
let el = 'getElement' in received
? await received?.getElement()
: 'getElements' in received
? await received?.getElements()
: received
let actualText
const pass = await waitUntil(async () => {
const result = await executeCommand.call(this, el, condition, options, [expectedValue, options])
el = result.el
actualText = result.values
return result.success
}, options)
const message = enhanceError(el, wrapExpectedWithArray(el, actualText, expectedValue), actualText, this, verb, expectation, '', options)
const result: ExpectWebdriverIO.AssertionResult = {
pass,
message: (): string => message
}
await options.afterAssertion?.({
matcherName: 'toHaveText',
expectedValue,
options,
result
})
return result
}