|
| 1 | +import { browser, expect, $ } from '@wdio/globals' |
| 2 | + |
| 3 | +describe('Clipboard: Chrome', () => { |
| 4 | + before(() => browser.url('/example.html')) |
| 5 | + |
| 6 | + it('should allow the browser to read the clipboard', async () => { |
| 7 | + // set clipboard permissions |
| 8 | + await browser.setPermissions({ name: 'clipboard-read' }, 'granted') |
| 9 | + |
| 10 | + const btn = await $('#copyBtn') |
| 11 | + await btn.click() |
| 12 | + |
| 13 | + // Use a focus event to trigger the async clipboard read, then save |
| 14 | + // the result to a global variable on the window object |
| 15 | + // Without this the clipboard read may be blocked |
| 16 | + // with "NotAllowedError: Document is not focused." |
| 17 | + // See: https://stackoverflow.com/questions/56306153/domexception-on-calling-navigator-clipboard-readtext |
| 18 | + await browser.execute(() => { |
| 19 | + const _asyncCopyFn = (async () => { |
| 20 | + setTimeout(async () => { |
| 21 | + window.clipboardText = await navigator.clipboard.readText(); |
| 22 | + document.body.removeEventListener("click", _asyncCopyFn); |
| 23 | + }, 200); |
| 24 | + }); |
| 25 | + document.body.addEventListener("click", _asyncCopyFn); |
| 26 | + }); |
| 27 | + |
| 28 | + await $('body').click(); |
| 29 | + |
| 30 | + await browser.pause(300); |
| 31 | + |
| 32 | + // now you can read the clipboard text from the global variable |
| 33 | + const clipboardText = await browser.execute(() => window.clipboardText); |
| 34 | + console.log('Clipboard text:', clipboardText); |
| 35 | + |
| 36 | + await expect(clipboardText).toBe('Hello from WebdriverIO clipboard test!') |
| 37 | + }) |
| 38 | + |
| 39 | +}) |
0 commit comments