feat: add mobile network environment test suite

This commit is contained in:
张翔
2026-03-05 15:50:05 +08:00
parent 0074a9aab1
commit 9e5d61cfed
@@ -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();
});
});