跟踪后,我尝试按
.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");
});
});
t.pressKey 操作模拟一系列“keydown”、“keypress”和“keyup”事件。要模拟按住按键,您可以使用 t.dispatchEvent 操作。以下是其用法示例:https://github.com/DevExpress/testcafe/issues/1839#issuecomment-1506640783.
此外,根据您的代码片段,您需要在操作发生之前添加事件侦听器。将
t.eval
调用置于 click
和 pressKey
之上。