forked from webdriverio/expect-webdriverio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoHaveClipboardText.ts
More file actions
47 lines (39 loc) · 1.49 KB
/
toHaveClipboardText.ts
File metadata and controls
47 lines (39 loc) · 1.49 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
import logger from '@wdio/logger'
import { waitUntil, enhanceError, compareText } from '../../utils.js'
import { DEFAULT_OPTIONS } from '../../constants.js'
const log = logger('expect-webdriverio')
export async function toHaveClipboardText(
browser: WebdriverIO.Browser,
expectedValue: string | RegExp | WdioAsymmetricMatcher<string>,
options: ExpectWebdriverIO.StringOptions = DEFAULT_OPTIONS
) {
const isNot = this.isNot
const { expectation = 'clipboard text', verb = 'have' } = this
await options.beforeAssertion?.({
matcherName: 'toHaveClipboardText',
expectedValue,
options,
})
let actual
const pass = await waitUntil(async () => {
await browser.setPermissions({ name: 'clipboard-read' }, 'granted')
/**
* changes are that some browser don't support the clipboard API yet
*/
.catch((err) => log.warn(`Couldn't set clipboard permissions: ${err}`))
actual = await browser.execute(() => window.navigator.clipboard.readText())
return compareText(actual, expectedValue, options).result
}, isNot, options)
const message = enhanceError('browser', expectedValue, actual, this, verb, expectation, '', options)
const result: ExpectWebdriverIO.AssertionResult = {
pass,
message: () => message
}
await options.afterAssertion?.({
matcherName: 'toHaveClipboardText',
expectedValue,
options,
result
})
return result
}