[英]Simulating a long press of mouse left button in JS with testing-library
我正在寫一個集成測試,我需要點擊 html object 超過 0.5 秒。 在同一測試中,我已經能夠使用userEvent
來處理各種鍵的操作,例如:
const user = userEvent.setup();
await user.keyboard("[ShiftLeft>]"); // Press Shift (without releasing it)
const pointer = await screen.findByText(objectName);
await user.click(pointer);
await user.keyboard("[/ShiftLeft]"); // Release Shift
我想知道是否有一種方法可以做類似的事情(有或沒有 userEvent)來在頁面中長按 object。 就像是:
謝謝!
我找到了解決我問題的方法,也許它對其他人有用:)它使用來自測試庫/用戶事件的userEvent
pointer
。
export async function longPress(target: string) {
const myTarget = await screen.findByText(target);
const user = userEvent.setup();
await user.pointer({
keys: "[MouseLeft>]",
target: myTarget ,
});
await new Promise((resolve) => {
setTimeout(resolve, 1000);
});
await user.pointer({ keys: "[/MouseLeft]", target: myTarget });
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.