playwright.config.js 2.41 KB
/**
 * Playwright E2E 测试配置
 *
 * 使用方法:
 * 1. 安装 Playwright: npm install -D @playwright/test
 * 2. 安装浏览器:npx playwright install
 * 3. 运行测试:npx playwright test
 * 4. 带 UI 运行:npx playwright test --ui
 * 5. 运行特定测试:npx playwright test e2e/xxx.e2e.js
 * 6. 生成报告:npx playwright show-report
 */

import process from "node:process";
import { defineConfig, devices } from "@playwright/test";
import dayjs from "dayjs";

// 生成时间戳,用于报告和结果目录
// Windows 路径不能含冒号;报告/结果目录用安全时间戳
const timestamp = dayjs().format("YYYY-MM-DD_HH-mm-ss");

export default defineConfig({
	// 测试文件位置
	testDir: "./e2e",

	// 测试文件匹配模式
	testMatch: "**/*.e2e.js",

	// 超时时间
	timeout: 30 * 1000,

	// 每个测试的期望时间
	expect: {
		timeout: 5000,
	},

	// 失败重试次数
	retries: 1,

	// 并行执行配置
	workers: 1,

	// 是否失败即停止
	failOnFlakyTests: false,

	// 报告配置
	// 产出到 playwright/report/index.YYYY-MM-DD hh-mm.html
	reporter: [
		["list"],
		[
			"html",
			{
				outputFolder: `playwright/report/${timestamp}`,
				open: "never",
			},
		],
	],

	// 共享配置
	use: {
		// 基础 URL - 使用当前 IP 访问
		baseURL: process.env.BASE_URL || "http://192.168.1.146:8080/dj-carlife-web/",

		// 截图配置
		screenshot: "only-on-failure",

		// 录屏配置
		video: process.env.PLAYWRIGHT_VIDEO === "true" ? "retain-on-failure" : "off",

		// 追踪配置
		trace: "retain-on-failure",

		// 浏览器上下文选项
		actionTimeout: 10000,
		navigationTimeout: 30000,
	},

	// 测试结果目录(截图、视频、trace)
	// 产出到 playwright/result/YYYY-MM-DD hh-mm/
	outputDir: `playwright/result/${timestamp}`,

	// 浏览器配置
	projects: [
		{
			name: "chromium",
			use: {
				...devices["Desktop Chrome"],
				viewport: { width: 1920, height: 1080 },
			},
		},
	],

	// Web 服务器配置 (用于自动启动开发服务器)
	// 如果在远程机器运行,请设置 SKIP_WEBSERVER=true 并手动启动服务
	webServer: process.env.SKIP_WEBSERVER
		? undefined
		: {
				command: "npm run serve",
				timeout: 120 * 1000,
				reuseExistingServer: true,
				// 等待 URL,确保服务器准备好
				url: process.env.BASE_URL
					? process.env.BASE_URL.replace(/\/$/, "") + "/#/"
					: "http://192.168.1.146:8080/dj-carlife-web/#/",
			},
});