Files
novalon-website/e2e/src/utils/GestureSimulator.ts
T

72 lines
1.6 KiB
TypeScript

import { Page, Locator } from '@playwright/test';
export interface SwipeOptions {
startX: number;
startY: number;
endX: number;
endY: number;
duration: number;
}
export interface PinchOptions {
centerX: number;
centerY: number;
startDistance: number;
endDistance: number;
duration: number;
}
export interface DragOptions {
source: Locator;
target: Locator;
duration: number;
}
export class GestureSimulator {
constructor(private page: Page) {}
async swipe(options: SwipeOptions): Promise<void> {
const { startX, startY, endX, endY, duration } = options;
await this.page.touchscreen.tap(startX, startY);
const steps = 10;
const stepDuration = duration / steps;
for (let i = 1; i <= steps; i++) {
const x = startX + (endX - startX) * (i / steps);
const y = startY + (endY - startY) * (i / steps);
await this.page.touchscreen.touchMove(x, y);
await this.page.waitForTimeout(stepDuration);
}
await this.page.touchscreen.touchEnd();
}
async quickSwipeDown(): Promise<void> {
const viewport = this.page.viewportSize();
if (!viewport) return;
await this.swipe({
startX: viewport.width / 2,
startY: viewport.height * 0.3,
endX: viewport.width / 2,
endY: viewport.height * 0.7,
duration: 300,
});
}
async slowSwipeUp(): Promise<void> {
const viewport = this.page.viewportSize();
if (!viewport) return;
await this.swipe({
startX: viewport.width / 2,
startY: viewport.height * 0.7,
endX: viewport.width / 2,
endY: viewport.height * 0.3,
duration: 800,
});
}
}