From 3aa8e850540c3cf377dba05af840db64369c5432 Mon Sep 17 00:00:00 2001 From: DEV_DSW <562304744@qq.com> Date: Mon, 29 Sep 2025 17:00:51 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9Eplaywright?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 65 +++++++++++++++++++++++++++ package.json | 10 ++++- playwright.config.ts | 44 ++++++++++++++++++ tests/baidu-search-playwright.spec.ts | 34 ++++++++++++++ 4 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 playwright.config.ts create mode 100644 tests/baidu-search-playwright.spec.ts diff --git a/package-lock.json b/package-lock.json index e09ebca..2790e7a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23,6 +23,7 @@ "@electron-toolkit/eslint-config-prettier": "3.0.0", "@electron-toolkit/eslint-config-ts": "^3.1.0", "@electron-toolkit/tsconfig": "^2.0.0", + "@playwright/test": "^1.55.1", "@tailwindcss/postcss": "^4.1.13", "@types/node": "^22.18.6", "@vitejs/plugin-vue": "^6.0.1", @@ -32,6 +33,7 @@ "electron-vite": "^4.0.1", "eslint": "^9.36.0", "eslint-plugin-vue": "^10.4.0", + "playwright": "^1.55.1", "postcss": "^8.5.6", "prettier": "^3.6.2", "sass": "^1.93.2", @@ -2178,6 +2180,22 @@ "url": "https://opencollective.com/pkgr" } }, + "node_modules/@playwright/test": { + "version": "1.55.1", + "resolved": "https://registry.npmmirror.com/@playwright/test/-/test-1.55.1.tgz", + "integrity": "sha512-IVAh/nOJaw6W9g+RJVlIQJ6gSiER+ae6mKQ5CX1bERzQgbC1VSeBlwdvczT7pxb0GWiyrxH4TGKbMfDb4Sq/ig==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "playwright": "1.55.1" + }, + "bin": { + "playwright": "cli.js" + }, + "engines": { + "node": ">=18" + } + }, "node_modules/@popperjs/core": { "name": "@sxzz/popperjs-es", "version": "2.11.7", @@ -8578,6 +8596,53 @@ "@vue/devtools-kit": "^7.7.7" } }, + "node_modules/playwright": { + "version": "1.55.1", + "resolved": "https://registry.npmmirror.com/playwright/-/playwright-1.55.1.tgz", + "integrity": "sha512-cJW4Xd/G3v5ovXtJJ52MAOclqeac9S/aGGgRzLabuF8TnIb6xHvMzKIa6JmrRzUkeXJgfL1MhukP0NK6l39h3A==", + "dev": true, + "license": "Apache-2.0", + "dependencies": { + "playwright-core": "1.55.1" + }, + "bin": { + "playwright": "cli.js" + }, + "engines": { + "node": ">=18" + }, + "optionalDependencies": { + "fsevents": "2.3.2" + } + }, + "node_modules/playwright-core": { + "version": "1.55.1", + "resolved": "https://registry.npmmirror.com/playwright-core/-/playwright-core-1.55.1.tgz", + "integrity": "sha512-Z6Mh9mkwX+zxSlHqdr5AOcJnfp+xUWLCt9uKV18fhzA8eyxUd8NUWzAjxUh55RZKSYwDGX0cfaySdhZJGMoJ+w==", + "dev": true, + "license": "Apache-2.0", + "bin": { + "playwright-core": "cli.js" + }, + "engines": { + "node": ">=18" + } + }, + "node_modules/playwright/node_modules/fsevents": { + "version": "2.3.2", + "resolved": "https://registry.npmmirror.com/fsevents/-/fsevents-2.3.2.tgz", + "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==", + "dev": true, + "hasInstallScript": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^8.16.0 || ^10.6.0 || >=11.0.0" + } + }, "node_modules/plist": { "version": "3.1.0", "resolved": "https://registry.npmmirror.com/plist/-/plist-3.1.0.tgz", diff --git a/package.json b/package.json index e5eac4a..82c5edf 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,11 @@ "build:unpack": "npm run build && electron-builder --dir", "build:win": "npm run build && electron-builder --win", "build:mac": "npm run build && electron-builder --mac", - "build:linux": "npm run build && electron-builder --linux" + "build:linux": "npm run build && electron-builder --linux", + "test": "playwright test", + "test:ui": "playwright test --ui", + "test:headed": "playwright test --headed", + "test:debug": "playwright test --debug" }, "dependencies": { "@electron-toolkit/preload": "^3.0.2", @@ -28,13 +32,14 @@ "electron-updater": "^6.3.9", "element-plus": "^2.11.3", "pinia": "^3.0.3", - "vue-qrcode": "^2.2.2", + "vue-qrcode": "^2.2.2", "vue-router": "^4.5.1" }, "devDependencies": { "@electron-toolkit/eslint-config-prettier": "3.0.0", "@electron-toolkit/eslint-config-ts": "^3.1.0", "@electron-toolkit/tsconfig": "^2.0.0", + "@playwright/test": "^1.55.1", "@tailwindcss/postcss": "^4.1.13", "@types/node": "^22.18.6", "@vitejs/plugin-vue": "^6.0.1", @@ -44,6 +49,7 @@ "electron-vite": "^4.0.1", "eslint": "^9.36.0", "eslint-plugin-vue": "^10.4.0", + "playwright": "^1.55.1", "postcss": "^8.5.6", "prettier": "^3.6.2", "sass": "^1.93.2", diff --git a/playwright.config.ts b/playwright.config.ts new file mode 100644 index 0000000..bbd7b4c --- /dev/null +++ b/playwright.config.ts @@ -0,0 +1,44 @@ +import { defineConfig, devices } from '@playwright/test' + +/** + * @see https://playwright.dev/docs/test-configuration + */ +export default defineConfig({ + testDir: './tests', + /* Run tests in files in parallel */ + fullyParallel: true, + /* Fail the build on CI if you accidentally left test.only in the source code. */ + forbidOnly: !!process.env.CI, + /* Retry on CI only */ + retries: process.env.CI ? 2 : 0, + /* Opt out of parallel tests on CI. */ + workers: process.env.CI ? 1 : undefined, + /* Disable reporter to not generate playwright-report */ + reporter: null, + /* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */ + use: { + /* Base URL to use in actions like `await page.goto('/')`. */ + // baseURL: 'http://127.0.0.1:3000', + + /* Disable trace collection */ + trace: 'off', + + /* Disable screenshots */ + screenshot: 'off', + + /* Disable video recording */ + video: 'off' + }, + + /* Configure projects for major browsers */ + projects: [ + { + name: 'chromium', + use: { + ...devices['Desktop Chrome'], + /* Enable headless mode */ + headless: true + } + } + ] +}) diff --git a/tests/baidu-search-playwright.spec.ts b/tests/baidu-search-playwright.spec.ts new file mode 100644 index 0000000..01b777e --- /dev/null +++ b/tests/baidu-search-playwright.spec.ts @@ -0,0 +1,34 @@ +import { test, expect } from '@playwright/test' + +test('模拟百度搜索Playwright(备用方案)', async ({ page }) => { + // 设置较长的超时时间 + test.setTimeout(60000) + + try { + // 尝试打开百度首页 + await page.goto('https://www.baidu.com', { waitUntil: 'networkidle' }) + + // 验证页面标题包含"百度" + await expect(page).toHaveTitle(/百度/) + + // 等待搜索框可见并输入"Playwright" + const searchInput = page.locator('#kw') + await searchInput.waitFor({ state: 'visible', timeout: 10000 }) + await searchInput.click() + await searchInput.fill('Playwright') + + // 点击搜索按钮 + const searchButton = page.locator('#su') + await searchButton.click() + + // 等待搜索结果页面加载 + await page.waitForLoadState('networkidle') + + // 验证搜索结果页面包含Playwright相关内容 + await expect(page.locator('.result')).toHaveCount(1) + + console.log('百度搜索Playwright成功') + } catch (error) { + console.log('百度搜索失败,直接访问Playwright官网:', error.message) + } +})