feat: implement drag gesture in GestureSimulator

This commit is contained in:
张翔
2026-03-05 15:04:29 +08:00
parent 12ff5fc1f3
commit 531cd1567a
2 changed files with 46 additions and 0 deletions
@@ -74,4 +74,26 @@ test.describe('GestureSimulator - Swipe', () => {
await expect(image).toBeVisible();
});
test('should perform drag', async ({ page }) => {
await page.goto('/products');
const simulator = new GestureSimulator(page);
const firstCard = page.locator('.card').first();
const secondCard = page.locator('.card').nth(1);
const firstCardInitialPosition = await firstCard.boundingBox();
await simulator.drag({
source: firstCard,
target: secondCard,
duration: 500,
});
const firstCardFinalPosition = await firstCard.boundingBox();
if (firstCardInitialPosition && firstCardFinalPosition) {
expect(firstCardFinalPosition.y).toBeGreaterThan(firstCardInitialPosition.y);
}
});
});
+24
View File
@@ -121,4 +121,28 @@ export class GestureSimulator {
await this.page.waitForTimeout(100);
await this.page.touchscreen.tap(x, y);
}
async drag(options: DragOptions): Promise<void> {
const { source, target, duration } = options;
const sourceBox = await source.boundingBox();
const targetBox = await target.boundingBox();
if (!sourceBox || !targetBox) {
throw new Error('Source or target element not visible');
}
const startX = sourceBox.x + sourceBox.width / 2;
const startY = sourceBox.y + sourceBox.height / 2;
const endX = targetBox.x + targetBox.width / 2;
const endY = targetBox.y + targetBox.height / 2;
await this.swipe({
startX,
startY,
endX,
endY,
duration,
});
}
}