30 lines
859 B
TypeScript
30 lines
859 B
TypeScript
import { Locator, Page } from 'playwright';
|
|
|
|
/**
|
|
*
|
|
* @param page - The page to simulate the drag on
|
|
* @param locator - The locator of the element to perform the drag on
|
|
*/
|
|
|
|
export async function simulateDrag(page: Page, locator: Locator) {
|
|
const box = await locator.boundingBox();
|
|
if (!box) {
|
|
throw new Error('Element is not visible');
|
|
}
|
|
const { x, y, width, height } = box;
|
|
const centerX = x + width / 2;
|
|
const centerY = y + height / 2;
|
|
|
|
// Calculate the maximum possible movement distances within the element's bounds
|
|
const maxMoveX = Math.min(100, x + width - centerX);
|
|
const maxMoveY = Math.min(100, y + height - centerY);
|
|
|
|
const newX = centerX + maxMoveX;
|
|
const newY = centerY + maxMoveY;
|
|
|
|
await page.mouse.move(centerX, centerY);
|
|
await page.mouse.down();
|
|
await page.mouse.move(newX, newY);
|
|
await page.mouse.up();
|
|
}
|