【问题标题】:Simulating a long press of mouse left button in JS with testing-library用testing-library在JS中模拟长按鼠标左键
【发布时间】:2022-11-30 21:45:01
【问题描述】:

我正在写一个集成测试,我需要点击一个 html 对象超过 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)来对页面中的对象执行长按。 就像是:

  • 鼠标左键单击 X 而不松开按钮
  • 等待1秒
  • 松开鼠标左键

谢谢!

【问题讨论】:

    标签: javascript integration-testing mouse testing-library


    【解决方案1】:

    我找到了解决我问题的方法,也许对其他人有用:) 它使用测试库/用户事件中的userEventpointer

    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 });
    }
    

    【讨论】:

      猜你喜欢
      • 2021-01-13
      • 1970-01-01
      • 2020-05-26
      • 2017-11-01
      • 2021-10-08
      • 2021-07-16
      • 2019-10-30
      • 2010-12-31
      • 2023-03-23
      相关资源
      最近更新 更多