2026-08-03 18:04:19 +08:00
|
|
|
import type { Locator, Page } from '@playwright/test'
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Performs a pointer drag from one card to another. dnd-kit v6 uses a
|
|
|
|
|
* `PointerSensor` with a 10px distance activation constraint, and WebKit only
|
|
|
|
|
* reliably starts the drag when pointer moves are delivered in separate,
|
|
|
|
|
* awaited batches (a single fast `mouse.move(steps)` can be coalesced before
|
|
|
|
|
* the drag activates, losing the drop). Moves are therefore chunked with
|
|
|
|
|
* intermediate waits so the activation and drag move handlers run between
|
|
|
|
|
* batches on every browser.
|
|
|
|
|
*/
|
|
|
|
|
export async function dragCard(page: Page, source: Locator, target: Locator): Promise<void> {
|
|
|
|
|
const sourceBox = (await source.boundingBox())!
|
|
|
|
|
const targetBox = (await target.boundingBox())!
|
|
|
|
|
if (!sourceBox || !targetBox) {
|
|
|
|
|
throw new Error('Drag source or target has no bounding box')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const fromX = sourceBox.x + sourceBox.width / 2
|
|
|
|
|
const fromY = sourceBox.y + sourceBox.height / 2
|
|
|
|
|
const toX = targetBox.x + targetBox.width / 2
|
|
|
|
|
const toY = targetBox.y + targetBox.height / 2
|
|
|
|
|
|
|
|
|
|
await page.mouse.move(fromX, fromY)
|
|
|
|
|
await page.mouse.down()
|
|
|
|
|
|
2026-09-03 21:01:14 +08:00
|
|
|
const dx = toX - fromX
|
|
|
|
|
const dy = toY - fromY
|
|
|
|
|
const dist = Math.hypot(dx, dy)
|
|
|
|
|
|
|
|
|
|
// Step past the 10px activation constraint to trigger drag activation
|
|
|
|
|
// and give the browser a brief moment for status to become 'dragging'
|
|
|
|
|
const activationRatio = dist > 0 ? Math.min(1, 15 / dist) : 1
|
|
|
|
|
await page.mouse.move(fromX + dx * activationRatio, fromY + dy * activationRatio)
|
|
|
|
|
await page.waitForTimeout(50)
|
|
|
|
|
|
|
|
|
|
// Move in several chunks so the intermediate dragmove handlers run
|
2026-08-03 18:04:19 +08:00
|
|
|
const chunks = 8
|
|
|
|
|
for (let i = 1; i <= chunks; i++) {
|
2026-09-03 21:01:14 +08:00
|
|
|
const x = fromX + (dx * i) / chunks
|
|
|
|
|
const y = fromY + (dy * i) / chunks
|
2026-08-03 18:04:19 +08:00
|
|
|
await page.mouse.move(x, y)
|
2026-09-03 21:01:14 +08:00
|
|
|
await page.waitForTimeout(20)
|
2026-08-03 18:04:19 +08:00
|
|
|
}
|
|
|
|
|
|
2026-09-03 21:01:14 +08:00
|
|
|
// Ensure pointer is directly over target center and allow requestAnimationFrame
|
|
|
|
|
// and collision microtasks to settle before triggering drop
|
|
|
|
|
await page.mouse.move(toX, toY)
|
|
|
|
|
await page.evaluate(
|
|
|
|
|
() => new Promise((resolve) => requestAnimationFrame(() => requestAnimationFrame(resolve))),
|
|
|
|
|
)
|
|
|
|
|
await page.waitForTimeout(100)
|
|
|
|
|
|
2026-08-03 18:04:19 +08:00
|
|
|
await page.mouse.up()
|
|
|
|
|
}
|