node.js - 剧作家拖放

标签 node.js drag-and-drop playwright

尝试测试一些拖放功能,看来剧作家没有拖放功能,所以我正在使用 mouse.move() , mouse.down() & mouse.up() .
然而我的尝试似乎失败了,目标没有被移动。代码如下:

test("drag and drop test", async () => {
  await page.goto("https://www.w3schools.com/html/html5_draganddrop.asp");
  await page.waitForSelector("#accept-choices");
  await page.click("#accept-choices");
  await page.waitForSelector("#div1");
  let xStart, yStart, xFinish, yFinish, elementHandle, rect;
  elementHandle = await page.$("#div1");
  rect = await elementHandle.boundingBox();
  xStart = rect.x + rect.width / 2;
  yStart = rect.y + rect.height / 2;
  elementHandle = await page.$("#div2");
  rect = await elementHandle.boundingBox();
  xFinish = rect.x + rect.width / 2;
  yFinish = rect.y + rect.height / 2;
  console.log(`move from (${xStart}, ${yStart}) to (${xFinish},${yFinish})`);
  await page.screenshot({ path: "before drag.png" });
  await page.mouse.move(xStart, yStart);
  await page.mouse.down();
  await page.mouse.move(xFinish, yFinish);
  await page.mouse.up();
  await page.screenshot({ path: "after drag.png" });
});

最佳答案

我也一直在努力如何制作 drag and drop可能与剧作家。
例如,我需要垂直移动
重新订购前
1
2
3
应用重新订购后
2
1
3

    const exampleOneDrag = await page.$(
      `[data-testid="${exampleOne}-drag-icon-button"]`
    )
    const exampleTwoDrag = await page.$(
      `[data-testid="${exampleTwo}-drag-icon-button"]`
    )
    const oneBoundingBox = await exampleOneDrag?.boundingBox()
    const twoBoundingBox = await exampleTwoDrag?.boundingBox()

    if (oneBoundingBox && twoBoundingBox) {
      await page.mouse.move(
        oneBoundingBox.x + oneBoundingBox.width / 2,
        oneBoundingBox.y + oneBoundingBox.height / 2,
        { steps: 5 }
      )
      await page.mouse.down()
      await page.mouse.move(
        twoBoundingBox.x + twoBoundingBox.width / 2,
        twoBoundingBox.y + twoBoundingBox.height / 2,
        { steps: 5 }
      )
      await page.mouse.up()
    }
能够做到这一点的关键是 steps:5选项。我不完全确定它的作用,但我从这里找到:https://github.com/codeceptjs/CodeceptJS/blob/e815d82af028e904051b5b6c70873164e1df1bfd/lib/helper/Playwright.js#L2289-L2307e
希望这对你有用。我觉得你的情况,你应该这样改
  await page.mouse.move(xStart, xFinish);
  await page.mouse.down();
  await page.mouse.move(yStart, yFinish);

关于node.js - 剧作家拖放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64718915/

相关文章:

batch-file - 批处理文件复制,使用%1进行拖放

android - 比较android中的2个ImageView

javascript - 剧作家 - 登录表单、输入和提交点击的问题

testing - 相当于剧作家中定位器的软断言

javascript - 如何在nodejs中获取header请求

javascript - 在 sapper 中,在 Polka 中创建单独的后端 API 或使用相同的 Polka server.js

node.js - Socket.io 向套接字发送消息时出错

javascript - 如何允许循环 http 请求延迟,同时使用 Promise.all 检测请求何时完成

jQuery ui 可拖动元素不在滚动 div 之外 'draggable'

playwright - 剧作家测试中的根级 Hooks