英文:
How to simulate a key press for 5 seconds on the keyboard using TestCafe
问题
I tried to send it to the browser console by pressing .pressKey("PageDown")
, after tracking it, but nothing came of it. Who can tell what steps to take next, maybe there are examples? I was told that you can use the clientfunction
, but I can't understand how to put it all together due to the lack of the same examples. The attempted code is below:
await t
.click(S(sCounter))
.pressKey("PageDown");
await t.eval(() => {
document.addEventListener("keydown", function (event) {
console.log(event.key);
if (event.key === "PageDown") {
console.log("PageDown press");
let isKeyPressed = false;
let timeoutId = null;
isKeyPressed = true;
timeoutId = setTimeout(() => {
isKeyPressed = false;
if (timeoutId !== null) {
clearTimeout(timeoutId);
}
}, 10 * 1000); // set the timeout to the specified number of seconds
}
console.log("PageDown Up");
});
});
英文:
I tried to send it to the browser console by pressing .pressKey("PageDown")
, after tracking it, but nothing came of it. Who can tell what steps to take next, maybe there are examples? I was told that you can use the clientfunction
, but I can’t understand how to put it all together due to the lack of the same examples. The attempted code is below:
await t
.click(S(sCounter))
.pressKey("PageDown");
await t.eval(() => {
document.addEventListener("keydown", function (event) {
console.log(event.key);
if (event.key === "PageDown") {
console.log("PageDown press");
let isKeyPressed = false;
let timeoutId: ReturnType<typeof setTimeout> | null = null;
isKeyPressed = true;
timeoutId = setTimeout(() => {
isKeyPressed = false;
if (timeoutId !== null) {
clearTimeout(timeoutId);
}
}, 10 * 1000); // set the timeout to the specified number of seconds
}
console.log("PageDown Up");
});
});
答案1
得分: 1
t.pressKey 操作模拟一系列的 "keydown"、"keypress" 和 "keyup" 事件。要模拟按住键,您可以使用 t.dispatchEvent 操作。以下是其使用示例:https://github.com/DevExpress/testcafe/issues/1839#issuecomment-1506640783。
此外,根据您的代码片段,您需要在操作发生之前添加一个事件侦听器。将 t.eval
调用放在 click
和 pressKey
上方。
英文:
The t.pressKey action simulates a sequence of "keydown", "keypress", and "keyup" events. To simulate holding a key, you can use the t.dispatchEvent action. Here is an example of its usage: https://github.com/DevExpress/testcafe/issues/1839#issuecomment-1506640783.
Also, according to your code snippet, you need to add an event listener before the action occurs. Place the t.eval
call above click
and pressKey
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论