From 656480a95d70cfb2d66828db1a6b104a233c05c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=BF=94?= Date: Thu, 5 Mar 2026 14:51:33 +0800 Subject: [PATCH] feat: implement swipe gesture in GestureSimulator --- e2e/src/tests/utils/gesture-simulator.spec.ts | 34 ++++++++++++++ e2e/src/utils/GestureSimulator.ts | 45 +++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 e2e/src/tests/utils/gesture-simulator.spec.ts diff --git a/e2e/src/tests/utils/gesture-simulator.spec.ts b/e2e/src/tests/utils/gesture-simulator.spec.ts new file mode 100644 index 0000000..f787d80 --- /dev/null +++ b/e2e/src/tests/utils/gesture-simulator.spec.ts @@ -0,0 +1,34 @@ +import { test, expect } from '@playwright/test'; +import { GestureSimulator } from '../../utils/GestureSimulator'; + +test.describe('GestureSimulator - Swipe', () => { + test('should perform swipe up', async ({ page }) => { + await page.goto('/'); + const simulator = new GestureSimulator(page); + + const initialScrollY = await page.evaluate(() => window.scrollY); + expect(initialScrollY).toBe(0); + + await simulator.slowSwipeUp(); + + const afterScrollY = await page.evaluate(() => window.scrollY); + expect(afterScrollY).toBeGreaterThan(0); + }); + + test('should perform swipe down', async ({ page }) => { + await page.goto('/'); + const simulator = new GestureSimulator(page); + + await page.evaluate(() => { + window.scrollTo(0, 1000); + }); + + const initialScrollY = await page.evaluate(() => window.scrollY); + expect(initialScrollY).toBeGreaterThan(0); + + await simulator.quickSwipeDown(); + + const afterScrollY = await page.evaluate(() => window.scrollY); + expect(afterScrollY).toBeLessThan(initialScrollY); + }); +}); \ No newline at end of file diff --git a/e2e/src/utils/GestureSimulator.ts b/e2e/src/utils/GestureSimulator.ts index b3b889c..c809255 100644 --- a/e2e/src/utils/GestureSimulator.ts +++ b/e2e/src/utils/GestureSimulator.ts @@ -24,4 +24,49 @@ export interface DragOptions { export class GestureSimulator { constructor(private page: Page) {} + + async swipe(options: SwipeOptions): Promise { + 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 { + 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 { + 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, + }); + } } \ No newline at end of file