如何在使用TestCafe时模拟键盘按键持续按压5秒。

huangapple go评论63阅读模式
英文:

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 调用放在 clickpressKey 上方。

英文:

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.

huangapple
  • 本文由 发表于 2023年5月17日 16:58:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76270280.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定