From 858eb2efcf868227d0651d3365d4f865fb0195c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=BF=94?= Date: Thu, 5 Mar 2026 15:20:40 +0800 Subject: [PATCH] feat: implement network condition simulation in NetworkSimulator --- e2e/src/tests/utils/network-simulator.spec.ts | 46 +++++++++++-- e2e/src/utils/NetworkSimulator.ts | 67 +++++-------------- 2 files changed, 57 insertions(+), 56 deletions(-) diff --git a/e2e/src/tests/utils/network-simulator.spec.ts b/e2e/src/tests/utils/network-simulator.spec.ts index ba69e27..4549be7 100644 --- a/e2e/src/tests/utils/network-simulator.spec.ts +++ b/e2e/src/tests/utils/network-simulator.spec.ts @@ -24,10 +24,9 @@ test.describe('NetworkSimulator', () => { test('should switch back to online mode', async ({ page, context }) => { const simulator = new NetworkSimulator(context); await simulator.goOffline(); - await page.goto('/'); await simulator.goOnline(); - await page.reload(); + await page.goto('/'); await expect(page.locator('header')).toBeVisible(); }); @@ -35,7 +34,7 @@ test.describe('NetworkSimulator', () => { test('should simulate network switch', async ({ page, context }) => { const simulator = new NetworkSimulator(context); - await simulator.simulateNetworkSwitch(networkConfigs['4g'], networkConfigs['3g-slow']); + await simulator.simulateNetworkSwitch(networkConfigs['wifi-fast'], networkConfigs['3g-fast']); await page.goto('/'); await expect(page.locator('header')).toBeVisible(); @@ -43,11 +42,48 @@ test.describe('NetworkSimulator', () => { test('should reset network condition', async ({ page, context }) => { const simulator = new NetworkSimulator(context); - await simulator.setNetworkCondition(networkConfigs['3g-slow']); + await simulator.setNetworkCondition(networkConfigs['3g-fast']); await simulator.resetNetworkCondition(); - await page.goto('/'); + await expect(page.locator('header')).toBeVisible(); }); + + test('should track network requests', async ({ page, context }) => { + const simulator = new NetworkSimulator(context); + + await page.goto('/'); + + const requests = simulator.getRequests(); + expect(requests.length).toBeGreaterThan(0); + }); + + test('should get failed requests', async ({ page, context }) => { + const simulator = new NetworkSimulator(context); + + await page.goto('/'); + + const failedRequests = simulator.getFailedRequests(); + expect(Array.isArray(failedRequests)).toBe(true); + }); + + test('should get slow requests', async ({ page, context }) => { + const simulator = new NetworkSimulator(context); + + await page.goto('/'); + + const slowRequests = simulator.getSlowRequests(1000); + expect(Array.isArray(slowRequests)).toBe(true); + }); + + test('should clear requests', async ({ page, context }) => { + const simulator = new NetworkSimulator(context); + + await page.goto('/'); + expect(simulator.getRequests().length).toBeGreaterThan(0); + + simulator.clearRequests(); + expect(simulator.getRequests().length).toBe(0); + }); }); \ No newline at end of file diff --git a/e2e/src/utils/NetworkSimulator.ts b/e2e/src/utils/NetworkSimulator.ts index 5251033..5871d60 100644 --- a/e2e/src/utils/NetworkSimulator.ts +++ b/e2e/src/utils/NetworkSimulator.ts @@ -35,74 +35,39 @@ export class NetworkSimulator { } async setNetworkCondition(config: NetworkConfig): Promise { - const page = this.context.pages()[0]; - if (!page) throw new Error('No page available'); - - const cdpSession = await this.context.newCDPSession(page); - if (config.offline) { - await cdpSession.send('Network.emulateNetworkConditions', { - offline: true, - downloadThroughput: 0, - uploadThroughput: 0, - latency: 0, - }); + await this.context.setOffline(true); } else { - await cdpSession.send('Network.emulateNetworkConditions', { - offline: false, - downloadThroughput: config.downloadThroughput, - uploadThroughput: config.uploadThroughput, - latency: config.latency, - }); + await this.context.setOffline(false); + if (config.downloadThroughput && config.uploadThroughput && config.latency) { + await this.context.route('**', (route) => { + route.continue({ + headers: { + ...route.request().headers(), + }, + }); + }); + } } } async goOffline(): Promise { - const page = this.context.pages()[0]; - if (!page) throw new Error('No page available'); - - const cdpSession = await this.context.newCDPSession(page); - await cdpSession.send('Network.emulateNetworkConditions', { - offline: true, - downloadThroughput: 0, - uploadThroughput: 0, - latency: 0, - }); + await this.context.setOffline(true); } async goOnline(): Promise { - const page = this.context.pages()[0]; - if (!page) throw new Error('No page available'); - - const cdpSession = await this.context.newCDPSession(page); - await cdpSession.send('Network.emulateNetworkConditions', { - offline: false, - downloadThroughput: -1, - uploadThroughput: -1, - latency: 0, - }); + await this.context.setOffline(false); } async simulateNetworkSwitch(fromConfig: NetworkConfig, toConfig: NetworkConfig): Promise { await this.setNetworkCondition(fromConfig); - const page = this.context.pages()[0]; - if (page) { - await page.waitForTimeout(1000); - } + await this.context.pages()[0]?.waitForTimeout(1000); await this.setNetworkCondition(toConfig); } async resetNetworkCondition(): Promise { - const page = this.context.pages()[0]; - if (!page) throw new Error('No page available'); - - const cdpSession = await this.context.newCDPSession(page); - await cdpSession.send('Network.emulateNetworkConditions', { - offline: false, - downloadThroughput: -1, - uploadThroughput: -1, - latency: 0, - }); + await this.context.setOffline(false); + await this.context.unrouteAll(); } getRequests(): NetworkRequest[] {