【发布时间】:2021-02-19 10:34:34
【问题描述】:
尝试测试一些拖放功能,好像剧作家没有拖放功能,所以我使用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" });
});
【问题讨论】:
-
你可以在这里投票支持拖放支持github.com/microsoft/playwright/issues/1094
-
放弃了我的投票,谢谢,关于上面的代码是否应该工作有什么想法吗?
-
唯一有效的测试是使用 dispatchEvent 函数github.com/microsoft/playwright/blob/…,但要找到需要传递的 dataTransfer 对象可能会非常棘手。
标签: node.js drag-and-drop playwright