From ee58de3146b894ac83956393ed13e49411b019d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=A5=E6=B5=A9?= Date: Fri, 15 Aug 2025 16:33:27 +0800 Subject: [PATCH] =?UTF-8?q?test(config):=20=E6=9B=B4=E6=96=B0=20Playwright?= =?UTF-8?q?=20=E9=85=8D=E7=BD=AE=E4=BB=A5=E9=80=82=E5=BA=94=20VSCode=20?= =?UTF-8?q?=E7=8E=AF=E5=A2=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加代码判断是否在 VSCode 中运行,并设置相应的 baseURL - 修改 webServer 配置,仅在 VSCode 环境中启动本地服务器 - 优化配置文件结构,提高可读性和维护性 --- playwright.config.ts | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/playwright.config.ts b/playwright.config.ts index cab93b4..e412d1e 100644 --- a/playwright.config.ts +++ b/playwright.config.ts @@ -1,6 +1,11 @@ import { defineConfig, devices } from '@playwright/test'; import process from 'node:process'; +const runningInVSCode = process.env.TERM_PROGRAM === 'vscode'; +const baseURL = runningInVSCode + ? 'http://localhost:4173' + : process.env.BASE_URL || 'https://vue-ts-example.oo1.dev'; + /** * Read environment variables from file. * https://github.com/motdotla/dotenv @@ -66,7 +71,7 @@ export default defineConfig({ /* Maximum time each action such as `click()` can take. Defaults to 0 (no limit). */ actionTimeout: 0, /* Base URL to use in actions like `await page.goto('/')`. */ - baseURL: process.env.BASE_URL || 'https://vue-ts-example.oo1.dev', + baseURL, /* Only on CI systems run the tests headless */ headless: !!process.env.CI || process.env.HEADLESS === 'true', @@ -76,16 +81,13 @@ export default defineConfig({ }, // /* Run your local dev server before starting the tests */ - // webServer: { - // /** - // * Use the dev server by default for faster feedback loop. - // * Use the preview server on CI for more realistic testing. - // * Playwright will re-use the local server if there is already a dev-server running. - // */ - // command: process.env.CI ? 'npm run preview' : 'npm run dev', - // port: process.env.CI ? 4173 : 5173, - // reuseExistingServer: !process.env.CI, - // }, + webServer: runningInVSCode + ? { + command: 'npm run build-only; npm run preview', + port: 4173, + reuseExistingServer: true, + } + : undefined, /* Folder for test artifacts such as screenshots, videos, traces, etc. */ // outputDir: 'test-results/',