playwright学习-I

如何安装

首次安装

shell 复制代码
cd 某一个vue项目根目录
# 根据提示选择,不建议安装浏览器
npm init playwright@latest

# 单独安装浏览器,只安装chromium, 使用国内的源下载安装
$env:PLAYWRIGHT_DOWNLOAD_HOST="https://npmmirror.com/mirrors/playwright"; npx playwright install --with-deps chromium

npx playwright install --with-deps chromium


# `--` 是必须的,它告诉 npm 将后面的参数传递给 `playwright` 脚本而不是 npm 本身。
npm init playwright@latest -- --browser=chromium
npm init playwright@latest -- --yes --browser=chromium

为了使用国内源,在项目根目录下创建.npmrc文件,内容如下

shell 复制代码
# npm 包下载源(国内镜像)  
registry=https://registry.npmmirror.com  
NPM_CONFIG_REGISTRY=https://registry.npmmirror.com  
  
# Playwright 浏览器二进制下载源  
PLAYWRIGHT_DOWNLOAD_HOST=https://npmmirror.com/mirrors/playwright  
# (可选)自定义浏览器缓存目录,方便团队共享  
# PLAYWRIGHT_BROWSERS_PATH=./node_modules/.cache/ms-playwright

升级

shell 复制代码
npm install -D @playwright/test@latest  

npx playwright install --with-deps chromium

查看版本

shell 复制代码
npx playwright --version
# 查看当前已安装的浏览器及版本
npx playwright install --list

系统要求

  • Node.js: latest 22.x, 24.x or 26.x.
  • Windows 11+, Windows Server 2019+ or Windows Subsystem for Linux (WSL).
  • macOS 14 (Sonoma) or later.
  • Debian 12 / 13, Ubuntu 22.04 / 24.04 / 26.04 (x86-64 or arm64).

测试命令

  • See the browser window: add --headed.
  • Run a single project/browser: --project=chromium.
  • Run one file: npx playwright test tests/example.spec.ts.
  • Open testing UI: --ui.
  • 查看报告: npx playwright show-report
  • 开启trace npx playwright test --trace on
ts 复制代码
npx playwright test
npx playwright test --ui

//只运行指定的文件
npx playwright test example

//使用Debug模式运行
npx playwright test --debug

//仅使用chromium测试
npx playwright test --project=chromium

npx playwright show-report

代码生成

ts 复制代码
npx playwright codegen demo.playwright.dev/todomvc

附录

参考

npm配置

配置优先级: 命令行参数 > 环境变量(NPM_CONFIG_REGISTRY) > 项目.npmrc > 用户.npmrc > 全局.npmrc > 默认值

shell 复制代码
# 查看当前实际使用的 
registry npm config get registry 
# 查看所有生效的配置来源 
npm config list 
# 查看完整配置(含默认值) 
npm config list -l
相关推荐
king王一帅8 小时前
思考了很久,QuireKit 还是支持了 20 年前的 .doc,浏览器/Node 通用,MIT 开源
前端·typescript·开源
Amumu1213818 小时前
TypeScript学习: 基础知识(二)
开发语言·typescript·ecmascript
Amumu1213820 小时前
TypeScript学习: 进阶知识
开发语言·学习·typescript
jason_renyu1 天前
Playwright 入门与实操:从安装、定位、断言到自动发博客,把浏览器自动化讲明白
运维·python·自动化·前端开发·浏览器自动化·playwright·e2e测试
用户302822530681 天前
AI 视频图片运镜为什么会抖?帧率拉高也救不了多次取整
前端·人工智能·typescript
JudithHuang1 天前
Vue3 Vitest 单元测试进阶
前端·单元测试·log4j·vue
htzyl2061 天前
TypeScript:tsconfig配置误区及详情介绍,让你清清楚楚
前端·typescript
疯华整猫1 天前
类型全对、tsc 零错误,上线却白屏:AI 时代学 TypeScript,学的是它替不了的那一层
前端·typescript
king王一帅1 天前
啃完几十份国标,QuireKit 实现了像 Word 一样流畅的浏览器 OFD 编辑器,支持完备电子签标准与 OFD-H
前端·typescript·产品
SEO_juper1 天前
Node.js Product 结构化数据生成器:给产品页一键生成 JSON-LD
开发语言·前端·javascript·人工智能·node.js·json·1024程序员节