-
-
Notifications
You must be signed in to change notification settings - Fork 530
/
Copy pathmouse.ts
56 lines (46 loc) · 1.87 KB
/
mouse.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { Locator, Page } from "@playwright/test";
import { DRAG_HANDLE_SELECTOR } from "./const.js";
async function getElementLeftCoords(_page: Page, element: Locator) {
const boundingBox = (await element.boundingBox())!;
const centerY = boundingBox.y + boundingBox.height / 2;
return { x: boundingBox.x + 1, y: centerY };
}
async function getElementRightCoords(_page: Page, element: Locator) {
const boundingBox = (await element.boundingBox())!;
const centerY = boundingBox.y + boundingBox.height / 2;
return { x: boundingBox.x + boundingBox.width - 1, y: centerY };
}
async function getElementCenterCoords(_page: Page, element: Locator) {
const boundingBox = (await element.boundingBox())!;
const centerX = boundingBox.x + boundingBox.width / 2;
const centerY = boundingBox.y + boundingBox.height / 2;
return { x: centerX, y: centerY };
}
export async function moveMouseOverElement(page: Page, element: Locator) {
const boundingBox = (await element.boundingBox())!;
const coords = {
x: boundingBox.x + boundingBox.width / 2,
y: boundingBox.y + boundingBox.height / 2,
};
await page.mouse.move(coords.x, coords.y, { steps: 5 });
}
export async function dragAndDropBlock(
page: Page,
dragTarget: Locator,
dropTarget: Locator,
dropAbove: boolean
) {
await moveMouseOverElement(page, dragTarget);
await page.waitForSelector(DRAG_HANDLE_SELECTOR);
const dragHandle = await page.locator(DRAG_HANDLE_SELECTOR);
const dragHandleCenterCoords = await getElementCenterCoords(page, dragHandle);
await page.mouse.move(dragHandleCenterCoords.x, dragHandleCenterCoords.y, {
steps: 5,
});
await page.mouse.down();
const dropTargetCoords = dropAbove
? await getElementLeftCoords(page, dropTarget)
: await getElementRightCoords(page, dropTarget);
await page.mouse.move(dropTargetCoords.x, dropTargetCoords.y, { steps: 5 });
await page.mouse.up();
}