如何使用TestCafe模拟键盘上按键5秒

问题描述 投票:0回答:1

跟踪后,我尝试按

.pressKey("PageDown")
将其发送到浏览器控制台,但没有任何结果。谁能告诉我下一步要采取什么步骤,也许有例子?有人告诉我你可以使用
clientfunction
,但由于缺乏相同的示例,我无法理解如何将它们组合在一起。尝试的代码如下:

    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");
    });
});
typescript keyboard e2e-testing testcafe automation-testing
1个回答
1
投票

t.pressKey 操作模拟一系列“keydown”、“keypress”和“keyup”事件。要模拟按住按键,您可以使用 t.dispatchEvent 操作。以下是其用法示例:https://github.com/DevExpress/testcafe/issues/1839#issuecomment-1506640783.

此外,根据您的代码片段,您需要在操作发生之前添加事件侦听器。将

t.eval
调用置于
click
pressKey
之上。

© www.soinside.com 2019 - 2024. All rights reserved.