剧作家拖放
playwright drag and drop
正在尝试测试一些拖放功能,剧作家似乎没有拖放功能,所以我正在使用 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);
版本 1.13.0 为此添加了 API 方法。查看 documentation.
从文档中复制:
page.dragAndDrop(source, target[, options])
- source:
string
- target:
string
- options:
Object
- force
boolean
Whether to bypass the actionability checks. Defaults to false.
- noWaitAfter
boolean
Actions that initiate navigations are waiting for these navigations to happen and for pages to start
loading. You can opt out of waiting via setting this flag. You would
only need this option in the exceptional cases such as navigating to
inaccessible pages. Defaults to false.
- timeout
number
Maximum time in milliseconds, defaults to 30 seconds, pass 0 to disable timeout. The default value can be changed
by using the browserContext.setDefaultTimeout(timeout)
or
page.setDefaultTimeout(timeout)
methods.
- trial:
boolean
When set, this method only performs the actionability checks and skips the action. Defaults to false. Useful
to wait until the element is ready for the action without performing
it.
- returns:
Promise<void>
正在尝试测试一些拖放功能,剧作家似乎没有拖放功能,所以我正在使用 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);
版本 1.13.0 为此添加了 API 方法。查看 documentation.
从文档中复制:
page.dragAndDrop(source, target[, options])
- source:
string
- target:
string
- options:
Object
- force
boolean
Whether to bypass the actionability checks. Defaults to false.- noWaitAfter
boolean
Actions that initiate navigations are waiting for these navigations to happen and for pages to start loading. You can opt out of waiting via setting this flag. You would only need this option in the exceptional cases such as navigating to inaccessible pages. Defaults to false.- timeout
number
Maximum time in milliseconds, defaults to 30 seconds, pass 0 to disable timeout. The default value can be changed by using thebrowserContext.setDefaultTimeout(timeout)
orpage.setDefaultTimeout(timeout)
methods.- trial:
boolean
When set, this method only performs the actionability checks and skips the action. Defaults to false. Useful to wait until the element is ready for the action without performing it.- returns:
Promise<void>