From 9e5d61cfed2db67f02709c7c5fb226f74bece637 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=BF=94?= Date: Thu, 5 Mar 2026 15:50:05 +0800 Subject: [PATCH] feat: add mobile network environment test suite --- .../network/network-environment.spec.ts | 142 ++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 e2e/src/tests/mobile/network/network-environment.spec.ts diff --git a/e2e/src/tests/mobile/network/network-environment.spec.ts b/e2e/src/tests/mobile/network/network-environment.spec.ts new file mode 100644 index 0000000..f85dba6 --- /dev/null +++ b/e2e/src/tests/mobile/network/network-environment.spec.ts @@ -0,0 +1,142 @@ +import { test, expect } from '@playwright/test'; +import { NetworkSimulator } from '../../../utils/NetworkSimulator'; +import { networkConfigs } from '../../../config/network-configs'; + +test.describe('移动端网络环境测试 @mobile @network', () => { + test('WiFi 快速网络 - 页面加载', async ({ page, context }) => { + await page.setViewportSize({ width: 375, height: 667 }); + + const simulator = new NetworkSimulator(context); + await simulator.setNetworkCondition(networkConfigs['wifi-fast']); + + await page.goto('/'); + await expect(page.locator('header')).toBeVisible(); + await expect(page.locator('main')).toBeVisible(); + }); + + test('4G LTE 网络 - 页面加载', async ({ page, context }) => { + await page.setViewportSize({ width: 390, height: 844 }); + + const simulator = new NetworkSimulator(context); + await simulator.setNetworkCondition(networkConfigs['4g-lte']); + + await page.goto('/'); + await expect(page.locator('header')).toBeVisible(); + await expect(page.locator('main')).toBeVisible(); + }); + + test('3G 快速网络 - 页面加载', async ({ page, context }) => { + await page.setViewportSize({ width: 414, height: 896 }); + + const simulator = new NetworkSimulator(context); + await simulator.setNetworkCondition(networkConfigs['3g-fast']); + + await page.goto('/'); + await expect(page.locator('header')).toBeVisible(); + await expect(page.locator('main')).toBeVisible(); + }); + + test('2G 慢速网络 - 页面加载', async ({ page, context }) => { + await page.setViewportSize({ width: 375, height: 667 }); + + const simulator = new NetworkSimulator(context); + await simulator.setNetworkCondition(networkConfigs['2g-slow']); + + await page.goto('/'); + await expect(page.locator('header')).toBeVisible(); + await expect(page.locator('main')).toBeVisible(); + }); + + test('离线模式 - 页面显示', async ({ page, context }) => { + await page.setViewportSize({ width: 390, height: 844 }); + + const simulator = new NetworkSimulator(context); + await simulator.goOffline(); + + await page.goto('/'); + await expect(page.locator('header')).toBeVisible(); + await expect(page.locator('main')).toBeVisible(); + + await simulator.goOnline(); + }); + + test('网络切换 - WiFi 到 3G', async ({ page, context }) => { + await page.setViewportSize({ width: 414, height: 896 }); + + const simulator = new NetworkSimulator(context); + await simulator.setNetworkCondition(networkConfigs['wifi-fast']); + + await page.goto('/'); + await expect(page.locator('header')).toBeVisible(); + + await simulator.simulateNetworkSwitch(networkConfigs['wifi-fast'], networkConfigs['3g-fast']); + + await page.reload(); + await expect(page.locator('header')).toBeVisible(); + }); + + test('网络切换 - 3G 到离线', async ({ page, context }) => { + await page.setViewportSize({ width: 375, height: 667 }); + + const simulator = new NetworkSimulator(context); + await simulator.setNetworkCondition(networkConfigs['3g-fast']); + + await page.goto('/'); + await expect(page.locator('header')).toBeVisible(); + + await simulator.simulateNetworkSwitch(networkConfigs['3g-fast'], networkConfigs['offline']); + + await page.reload(); + await expect(page.locator('header')).toBeVisible(); + + await simulator.goOnline(); + }); + + test('网络请求监控', async ({ page, context }) => { + await page.setViewportSize({ width: 390, height: 844 }); + + const simulator = new NetworkSimulator(context); + + await page.goto('/'); + + const requests = simulator.getRequests(); + expect(requests.length).toBeGreaterThan(0); + }); + + test('失败请求检测', async ({ page, context }) => { + await page.setViewportSize({ width: 414, height: 896 }); + + const simulator = new NetworkSimulator(context); + + await page.goto('/'); + + const failedRequests = simulator.getFailedRequests(); + expect(Array.isArray(failedRequests)).toBe(true); + }); + + test('慢速请求检测', async ({ page, context }) => { + await page.setViewportSize({ width: 375, height: 667 }); + + const simulator = new NetworkSimulator(context); + + await page.goto('/'); + + const slowRequests = simulator.getSlowRequests(1000); + expect(Array.isArray(slowRequests)).toBe(true); + }); + + test('网络条件重置', async ({ page, context }) => { + await page.setViewportSize({ width: 390, height: 844 }); + + const simulator = new NetworkSimulator(context); + await simulator.setNetworkCondition(networkConfigs['3g-fast']); + + await page.goto('/'); + await expect(page.locator('header')).toBeVisible(); + + await simulator.resetNetworkCondition(); + + await page.reload(); + await expect(page.locator('header')).toBeVisible(); + }); +}); \ No newline at end of file