在 TestCafe 中访问剪贴板 [英] Access Clipboard in TestCafe
问题描述
如何访问 TestCafe 中的剪贴板?我无法使用 navigator.clipboard API,因为我们在无头 chrome --allow-insecure 中运行.(这不是我可以改变的).
How can I access the clipboard in TestCafe? I am unable to use the navigator.clipboard API since we run in headless chrome --allow-insecure. (This is not something I can change).
有什么想法吗?
谢谢!
推荐答案
TestCafe 目前没有内置剪贴板工具.但是,您可以使用客户端函数来模拟剪贴板:
TestCafe does not have built-in clipboard tools at the moment. However, you can use the client function to emulate the clipboard:
import { Selector, ClientFunction } from 'testcafe';
fixture `Example`
.page `http://devexpress.github.io/testcafe/example/`;
test('Clipboard test', async t => {
const text = 'Value for copy-paste';
const emulateClipboard = ClientFunction(() => {
let buffer = '';
document.addEventListener('keypress', event => {
if (event.ctrlKey) {
if (event.key === 'c')
buffer = document.getSelection().toString();
if (event.key === 'v')
document.activeElement.value = buffer;
}
});
});
await emulateClipboard();
await t
.typeText('#developer-name', text)
.selectText('#developer-name')
.pressKey('ctrl+c')
.click('#tried-test-cafe')
.click('#comments')
.pressKey('ctrl+v')
.expect(Selector('#comments').value).eql(text)
});
此示例在 Chrome 中正常运行.由于访问<textarea>的内容不同,其他浏览器可能无法运行.元素.
This example works correctly in Chrome. It may not work in other browsers due to the difference in accessing the content of <textarea> elements.
您可以从这个讨论中收集更多信息:https://github.com/DevExpress/testcafe/issues/2668
You may be able to glean more information from this discussion: https://github.com/DevExpress/testcafe/issues/2668
这篇关于在 TestCafe 中访问剪贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!