ARTICLE DETAIL

资讯详情

深耕网站建设与运营推广的一线实战洞察。

Playwright 自动化测试 - 初始化 Playwright 项目的过程

Playwright 自动化测试 - 初始化 Playwright 项目的过程 初始化 Playwright 项目的过程1、演示运行npm init playwrightlatest初始化 Playwright 项目的过程Getting started with writing end-to-end tests with Playwright: Initializing project in . √ Do you want to use TypeScript or JavaScript? · TypeScript √ Where to put your end-to-end tests? · tests √ Add a GitHub Actions workflow? (Y/n) · true √ Install Playwright browsers (can be done manually via npx playwright install)? (Y/n) · true Initializing NPM project (npm init -y)… Wrote to D:\WEB\PlaywrightTest\package.json: { name: playwrighttest, version: 1.0.0, description: , main: index.js, scripts: { test: echo \Error: no test specified\ exit 1 }, keywords: [], author: , license: ISC } Installing Playwright Test (npm install --save-dev playwright/test)… added 3 packages, and audited 4 packages in 5s found 0 vulnerabilities Installing Types (npm install --save-dev types/node)… added 2 packages, and audited 6 packages in 2s found 0 vulnerabilities Writing playwright.config.ts. Writing .github\workflows\playwright.yml. Writing tests\example.spec.ts. Writing package.json. Downloading browsers (npx playwright install)… Downloading Chrome for Testing 153.0.8010.12 (playwright chromium v1243) from https://cdn.playwright.dev/builds/cft/153.0.8010.12/win64/chrome-win64.zip Error: read ECONNRESET at TLSWrap.onStreamRead (node:internal/stream_base_commons:216:20) { errno: -4077, code: ECONNRESET, syscall: read } Downloading Chrome for Testing 153.0.8010.12 (playwright chromium v1243) from https://cdn.playwright.dev/builds/cft/153.0.8010.12/win64/chrome-win64.zip Error: read ECONNRESET at TLSWrap.onStreamRead (node:internal/stream_base_commons:216:20) { errno: -4077, code: ECONNRESET, syscall: read } Downloading Chrome for Testing 153.0.8010.12 (playwright chromium v1243) from https://cdn.playwright.dev/builds/cft/153.0.8010.12/win64/chrome-win64.zip Error: Request to https://storage.googleapis.com/chrome-for-testing-public/153.0.8010.12/win64/chrome-win64.zip timed out after 30000ms at ClientRequest.anonymous (D:\WEB\PlaywrightTest\node_modules\playwright-core\lib\coreBundle.js:8793:15) at Object.onceWrapper (node:events:633:28) at ClientRequest.emit (node:events:519:28) at TLSSocket.emitRequestTimeout (node:_http_client:931:9) at Object.onceWrapper (node:events:633:28) at TLSSocket.emit (node:events:531:35) at Socket._onTimeout (node:net:605:8) at listOnTimeout (node:internal/timers:585:17) at process.processTimers (node:internal/timers:521:7) Downloading Chrome for Testing 153.0.8010.12 (playwright chromium v1243) from https://cdn.playwright.dev/builds/cft/153.0.8010.12/win64/chrome-win64.zip Error: Request to https://storage.googleapis.com/chrome-for-testing-public/153.0.8010.12/win64/chrome-win64.zip timed out after 30000ms at ClientRequest.anonymous (D:\WEB\PlaywrightTest\node_modules\playwright-core\lib\coreBundle.js:8793:15) at Object.onceWrapper (node:events:633:28) at ClientRequest.emit (node:events:519:28) at TLSSocket.emitRequestTimeout (node:_http_client:931:9) at Object.onceWrapper (node:events:633:28) at TLSSocket.emit (node:events:531:35) at Socket._onTimeout (node:net:605:8) at listOnTimeout (node:internal/timers:585:17) at process.processTimers (node:internal/timers:521:7) Downloading Chrome for Testing 153.0.8010.12 (playwright chromium v1243) from https://cdn.playwright.dev/builds/cft/153.0.8010.12/win64/chrome-win64.zip Error: read ECONNRESET at TLSWrap.onStreamRead (node:internal/stream_base_commons:216:20) { errno: -4077, code: ECONNRESET, syscall: read } Failed to install browsers Error: Failed to download Chrome for Testing 153.0.8010.12 (playwright chromium v1243), caused by Error: Download failure, code1 at ChildProcess.anonymous (D:\WEB\PlaywrightTest\node_modules\playwright-core\lib\coreBundle.js:32428:32) at ChildProcess.emit (node:events:519:28) at ChildProcess._handle.onexit (node:internal/child_process:293:12) Error: Command failed: npx playwright install at genericNodeError (node:internal/errors:983:15) at wrappedFn (node:internal/errors:537:14) at checkExecSyncError (node:child_process:916:11) at execSync (node:child_process:988:15) at executeCommands (C:\Users\32123\AppData\Local\npm-cache\_npx\d352e76cc6b4974c\node_modules\create-playwright\lib\index.js:8272:39) at Generator.run (C:\Users\32123\AppData\Local\npm-cache\_npx\d352e76cc6b4974c\node_modules\create-playwright\lib\index.js:8356:5) at process.processTicksAndRejections (node:internal/process/task_queues:103:5) at async _Command.anonymous (C:\Users\32123\AppData\Local\npm-cache\_npx\d352e76cc6b4974c\node_modules\create-playwright\lib\index.js:8661:3) at async _Command.parseAsync (C:\Users\32123\AppData\Local\npm-cache\_npx\d352e76cc6b4974c\node_modules\create-playwright\lib\index.js:2148:9) { status: 1, signal: null, output: [ null, null, null ], pid: 21668, stdout: null, stderr: null }2、解读1初始化配置项Getting started with writing end-to-end tests with Playwright: Initializing project in . √ Do you want to use TypeScript or JavaScript? · TypeScript √ Where to put your end-to-end tests? · tests √ Add a GitHub Actions workflow? (Y/n) · true √ Install Playwright browsers (can be done manually via npx playwright install)? (Y/n) · true指令执行后Playwright 的脚手架向开发者询问了几个配置项配置项当前配置语言TypeScript测试目录tests添加 GitHub Actions 工作流true安装 Playwright 浏览器true2初始化骨架和依赖Initializing NPM project (npm init -y)… Wrote to D:\WEB\PlaywrightTest\package.json: { name: playwrighttest, version: 1.0.0, description: , main: index.js, scripts: { test: echo \Error: no test specified\ exit 1 }, keywords: [], author: , license: ISC }运行npm init -y生成package.json文件。此时scripts.test还是默认的报错占位Installing Playwright Test (npm install --save-dev playwright/test)… added 3 packages, and audited 4 packages in 5s found 0 vulnerabilities Installing Types (npm install --save-dev types/node)… added 2 packages, and audited 6 packages in 2s found 0 vulnerabilities安装playwright/test3 个包和types/node2 个包Writing playwright.config.ts. Writing .github\workflows\playwright.yml. Writing tests\example.spec.ts. Writing package.json.写入以下文件playwright.config.ts 文件这是配置文件 .github\workflows\playwright.yml 文件这是 CI 工作流 tests\example.spec.ts 文件这是示例测试 更新后的 package.json 文件3下载浏览器Downloading browsers (npx playwright install)… Downloading Chrome for Testing 153.0.8010.12 (playwright chromium v1243) from https://cdn.playwright.dev/builds/cft/153.0.8010.12/win64/chrome-win64.zip Error: read ECONNRESET at TLSWrap.onStreamRead (node:internal/stream_base_commons:216:20) { errno: -4077, code: ECONNRESET, syscall: read } Downloading Chrome for Testing 153.0.8010.12 (playwright chromium v1243) from https://cdn.playwright.dev/builds/cft/153.0.8010.12/win64/chrome-win64.zip Error: read ECONNRESET at TLSWrap.onStreamRead (node:internal/stream_base_commons:216:20) { errno: -4077, code: ECONNRESET, syscall: read } Downloading Chrome for Testing 153.0.8010.12 (playwright chromium v1243) from https://cdn.playwright.dev/builds/cft/153.0.8010.12/win64/chrome-win64.zip Error: Request to https://storage.googleapis.com/chrome-for-testing-public/153.0.8010.12/win64/chrome-win64.zip timed out after 30000ms at ClientRequest.anonymous (D:\WEB\PlaywrightTest\node_modules\playwright-core\lib\coreBundle.js:8793:15) at Object.onceWrapper (node:events:633:28) at ClientRequest.emit (node:events:519:28) at TLSSocket.emitRequestTimeout (node:_http_client:931:9) at Object.onceWrapper (node:events:633:28) at TLSSocket.emit (node:events:531:35) at Socket._onTimeout (node:net:605:8) at listOnTimeout (node:internal/timers:585:17) at process.processTimers (node:internal/timers:521:7) Downloading Chrome for Testing 153.0.8010.12 (playwright chromium v1243) from https://cdn.playwright.dev/builds/cft/153.0.8010.12/win64/chrome-win64.zip Error: Request to https://storage.googleapis.com/chrome-for-testing-public/153.0.8010.12/win64/chrome-win64.zip timed out after 30000ms at ClientRequest.anonymous (D:\WEB\PlaywrightTest\node_modules\playwright-core\lib\coreBundle.js:8793:15) at Object.onceWrapper (node:events:633:28) at ClientRequest.emit (node:events:519:28) at TLSSocket.emitRequestTimeout (node:_http_client:931:9) at Object.onceWrapper (node:events:633:28) at TLSSocket.emit (node:events:531:35) at Socket._onTimeout (node:net:605:8) at listOnTimeout (node:internal/timers:585:17) at process.processTimers (node:internal/timers:521:7) Downloading Chrome for Testing 153.0.8010.12 (playwright chromium v1243) from https://cdn.playwright.dev/builds/cft/153.0.8010.12/win64/chrome-win64.zip Error: read ECONNRESET at TLSWrap.onStreamRead (node:internal/stream_base_commons:216:20) { errno: -4077, code: ECONNRESET, syscall: read } Failed to install browsers Error: Failed to download Chrome for Testing 153.0.8010.12 (playwright chromium v1243), caused by Error: Download failure, code1 at ChildProcess.anonymous (D:\WEB\PlaywrightTest\node_modules\playwright-core\lib\coreBundle.js:32428:32) at ChildProcess.emit (node:events:519:28) at ChildProcess._handle.onexit (node:internal/child_process:293:12) Error: Command failed: npx playwright install at genericNodeError (node:internal/errors:983:15) at wrappedFn (node:internal/errors:537:14) at checkExecSyncError (node:child_process:916:11) at execSync (node:child_process:988:15) at executeCommands (C:\Users\32123\AppData\Local\npm-cache\_npx\d352e76cc6b4974c\node_modules\create-playwright\lib\index.js:8272:39) at Generator.run (C:\Users\32123\AppData\Local\npm-cache\_npx\d352e76cc6b4974c\node_modules\create-playwright\lib\index.js:8356:5) at process.processTicksAndRejections (node:internal/process/task_queues:103:5) at async _Command.anonymous (C:\Users\32123\AppData\Local\npm-cache\_npx\d352e76cc6b4974c\node_modules\create-playwright\lib\index.js:8661:3) at async _Command.parseAsync (C:\Users\32123\AppData\Local\npm-cache\_npx\d352e76cc6b4974c\node_modules\create-playwright\lib\index.js:2148:9) { status: 1, signal: null, output: [ null, null, null ], pid: 21668, stdout: null, stderr: null }运行npx playwright install下载浏览器3、关于下载浏览器的问题1问题原因下载 Chromium 时连续报了 5 次错最终失败。错误类型如下Error: read ECONNRESET连接被对方重置通常是网络中断、被防火墙 / 代理拦截、CDN 连接不稳定Request to https://storage.googleapis.com/... timed out after 30000ms网络无法稳定访问2处理策略Playwright 可以使用本机已装浏览器免下载。在playwright.config.ts文件添加如下配置use:{channel:chrome,// 或 msedge},
返回列表