-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
255 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import type { Page } from '@playwright/test'; | ||
import { SHORTKEY } from '../utils/index.js'; | ||
|
||
class Clipboard { | ||
constructor(private page: Page) {} | ||
|
||
async copy() { | ||
await this.page.keyboard.press(`${SHORTKEY}+c`); | ||
} | ||
|
||
async cut() { | ||
await this.page.keyboard.press(`${SHORTKEY}+x`); | ||
} | ||
|
||
async paste() { | ||
await this.page.keyboard.press(`${SHORTKEY}+v`); | ||
} | ||
|
||
async writeText(value: string) { | ||
// Playwright + Safari + Linux doesn't support async clipboard API | ||
// https://github.com/microsoft/playwright/issues/18901 | ||
const hasFallbackWritten = await this.page.evaluate((value) => { | ||
if (navigator.clipboard) return false; | ||
const textArea = document.createElement('textarea'); | ||
textArea.value = value; | ||
|
||
textArea.style.top = '0'; | ||
textArea.style.left = '0'; | ||
textArea.style.position = 'fixed'; | ||
|
||
document.body.appendChild(textArea); | ||
textArea.focus(); | ||
textArea.select(); | ||
|
||
const isSupported = document.execCommand('copy'); | ||
textArea.remove(); | ||
return isSupported; | ||
}, value); | ||
|
||
if (!hasFallbackWritten) { | ||
await this.write(value, 'text/plain'); | ||
} | ||
} | ||
|
||
async writeHTML(value: string) { | ||
return this.write(value, 'text/html'); | ||
} | ||
|
||
async readText() { | ||
return this.read('text/plain'); | ||
} | ||
|
||
async readHTML() { | ||
const html = await this.read('text/html'); | ||
return html.replace(/<meta[^>]*>/g, ''); | ||
} | ||
|
||
private async read(type: string) { | ||
const isHTML = type === 'text/html'; | ||
await this.page.evaluate((isHTML) => { | ||
const dataContainer = document.createElement(isHTML ? 'div' : 'textarea'); | ||
if (isHTML) dataContainer.setAttribute('contenteditable', 'true'); | ||
dataContainer.id = '_readClipboard'; | ||
document.body.appendChild(dataContainer); | ||
dataContainer.focus(); | ||
return dataContainer; | ||
}, isHTML); | ||
await this.paste(); | ||
const locator = this.page.locator('#_readClipboard'); | ||
const data = await (isHTML ? locator.innerHTML() : locator.inputValue()); | ||
await locator.evaluate((node) => node.remove()); | ||
return data; | ||
} | ||
|
||
private async write(data: string, type: string) { | ||
await this.page.evaluate( | ||
async ({ data, type }) => { | ||
if (type === 'text/html') { | ||
await navigator.clipboard.write([ | ||
new ClipboardItem({ | ||
'text/html': new Blob([data], { type: 'text/html' }), | ||
}), | ||
]); | ||
} else { | ||
await navigator.clipboard.writeText(data); | ||
} | ||
}, | ||
{ data, type }, | ||
); | ||
} | ||
} | ||
|
||
export default Clipboard; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { unlink, writeFile } from 'fs/promises'; | ||
import { unlinkSync } from 'fs'; | ||
import { tmpdir } from 'os'; | ||
import { join } from 'path'; | ||
import { globSync } from 'glob'; | ||
|
||
const sleep = (ms: number) => | ||
new Promise((resolve) => { | ||
setTimeout(resolve, ms); | ||
}); | ||
|
||
const PREFIX = 'playwright_locker_'; | ||
|
||
class Locker { | ||
public static clearAll() { | ||
globSync(join(tmpdir(), `${PREFIX}*.txt`)).forEach(unlinkSync); | ||
} | ||
|
||
constructor(private key: string) {} | ||
|
||
private get filePath() { | ||
return join(tmpdir(), `${PREFIX}${this.key}.txt`); | ||
} | ||
|
||
async lock() { | ||
try { | ||
await writeFile(this.filePath, '', { flag: 'wx' }); | ||
} catch { | ||
await sleep(50); | ||
await this.lock(); | ||
} | ||
} | ||
|
||
async release() { | ||
await unlink(this.filePath); | ||
} | ||
} | ||
|
||
export default Locker; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters