feat: 新增playwright

This commit is contained in:
DEV_DSW
2025-09-29 17:00:51 +08:00
parent 950b85e1e9
commit 3aa8e85054
4 changed files with 151 additions and 2 deletions

65
package-lock.json generated
View File

@@ -23,6 +23,7 @@
"@electron-toolkit/eslint-config-prettier": "3.0.0", "@electron-toolkit/eslint-config-prettier": "3.0.0",
"@electron-toolkit/eslint-config-ts": "^3.1.0", "@electron-toolkit/eslint-config-ts": "^3.1.0",
"@electron-toolkit/tsconfig": "^2.0.0", "@electron-toolkit/tsconfig": "^2.0.0",
"@playwright/test": "^1.55.1",
"@tailwindcss/postcss": "^4.1.13", "@tailwindcss/postcss": "^4.1.13",
"@types/node": "^22.18.6", "@types/node": "^22.18.6",
"@vitejs/plugin-vue": "^6.0.1", "@vitejs/plugin-vue": "^6.0.1",
@@ -32,6 +33,7 @@
"electron-vite": "^4.0.1", "electron-vite": "^4.0.1",
"eslint": "^9.36.0", "eslint": "^9.36.0",
"eslint-plugin-vue": "^10.4.0", "eslint-plugin-vue": "^10.4.0",
"playwright": "^1.55.1",
"postcss": "^8.5.6", "postcss": "^8.5.6",
"prettier": "^3.6.2", "prettier": "^3.6.2",
"sass": "^1.93.2", "sass": "^1.93.2",
@@ -2178,6 +2180,22 @@
"url": "https://opencollective.com/pkgr" "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": { "node_modules/@popperjs/core": {
"name": "@sxzz/popperjs-es", "name": "@sxzz/popperjs-es",
"version": "2.11.7", "version": "2.11.7",
@@ -8578,6 +8596,53 @@
"@vue/devtools-kit": "^7.7.7" "@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": { "node_modules/plist": {
"version": "3.1.0", "version": "3.1.0",
"resolved": "https://registry.npmmirror.com/plist/-/plist-3.1.0.tgz", "resolved": "https://registry.npmmirror.com/plist/-/plist-3.1.0.tgz",

View File

@@ -18,7 +18,11 @@
"build:unpack": "npm run build && electron-builder --dir", "build:unpack": "npm run build && electron-builder --dir",
"build:win": "npm run build && electron-builder --win", "build:win": "npm run build && electron-builder --win",
"build:mac": "npm run build && electron-builder --mac", "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": { "dependencies": {
"@electron-toolkit/preload": "^3.0.2", "@electron-toolkit/preload": "^3.0.2",
@@ -35,6 +39,7 @@
"@electron-toolkit/eslint-config-prettier": "3.0.0", "@electron-toolkit/eslint-config-prettier": "3.0.0",
"@electron-toolkit/eslint-config-ts": "^3.1.0", "@electron-toolkit/eslint-config-ts": "^3.1.0",
"@electron-toolkit/tsconfig": "^2.0.0", "@electron-toolkit/tsconfig": "^2.0.0",
"@playwright/test": "^1.55.1",
"@tailwindcss/postcss": "^4.1.13", "@tailwindcss/postcss": "^4.1.13",
"@types/node": "^22.18.6", "@types/node": "^22.18.6",
"@vitejs/plugin-vue": "^6.0.1", "@vitejs/plugin-vue": "^6.0.1",
@@ -44,6 +49,7 @@
"electron-vite": "^4.0.1", "electron-vite": "^4.0.1",
"eslint": "^9.36.0", "eslint": "^9.36.0",
"eslint-plugin-vue": "^10.4.0", "eslint-plugin-vue": "^10.4.0",
"playwright": "^1.55.1",
"postcss": "^8.5.6", "postcss": "^8.5.6",
"prettier": "^3.6.2", "prettier": "^3.6.2",
"sass": "^1.93.2", "sass": "^1.93.2",

44
playwright.config.ts Normal file
View File

@@ -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
}
}
]
})

View File

@@ -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)
}
})