当前位置: 首页 > news >正文

自动化浏览器的测试框架playwright 支持多种浏览器Chromium、Firefox 和 WebKit

Playwright 是一个用于自动化浏览器的测试框架,支持多种浏览器,包括 Chromium、Firefox 和 WebKit。在使用 Playwright 进行测试时,可能需要安装特定的浏览器,例如 Chrome。

如何安装Playwright Chrome

在使用 Playwright 时,可以通过以下步骤来安装 Chromium(Chrome 的开源版本):

  • 1 安装 Playwright

首先,如果你还没有安装 Playwright,可以通过 npm 或 pip 在 Node.js 或 Python 环境中安装它。

  • 对于 Node.js:
npm install playwright

  • 对于 Python:
pip install playwright

  • 2 安装浏览器

一旦 Playwright 安装完成,你可以使用以下命令安装 Chromium(Chrome 的一个分支)以及其他支持的浏览器(如果需要)。

npx playwright install

或者,如果你只想安装特定浏览器(比如 Chromium),可以执行:

npx playwright install chromium

运行测试

安装完成后,你可以开始编写和运行 Playwright 测试。以下是一个使用 Chromium 的简单示例:

const { chromium } = require('playwright');

(async () => {
  // 启动浏览器
  const browser = await chromium.launch();
  const page = await browser.newPage();
  
  // 访问网站
  await page.goto('https://example.com');
  
  // 截图
  await page.screenshot({ path: 'example.png' });

  // 关闭浏览器
  await browser.close();
})();

将代码保存到example.js文件中,然后执行
node example.js

按照代码,在当前目录生成了一副图片

2025/03/27  13:45            17,117 example.png

调试通过

其他信息

  • 其他浏览器:除了 Chromium,Playwright 还支持 Firefox 和 WebKit(Safari 的引擎),可以通过相似的方式安装。
  • Headless 模式:Playwright 可以在无头模式下运行,即不显示浏览器界面的情况下执行测试。你可以使用 headless: true 属性来启用这个特性。
const browser = await chromium.launch({ headless: true });
Copy

总结

playwright install chrome 是用于安装 Chrome(具体来说是 Chromium)浏览器的过程的一部分。通过 Playwright,你可以方便地进行跨浏览器的自动化测试,支持多种常见的浏览器以及无头模式,极大地提高了测试的灵活性和效率。

调试

playwright test测试的时候显示没playwright 

感觉需要这样执行

npx playwright test

npx playwright install
╔═══════════════════════════════════════════════════════════════════════════════╗
║ WARNING: It looks like you are running 'npx playwright install' without first ║
║ installing your project's dependencies.                                       ║
║                                                                               ║
║ To avoid unexpected behavior, please install your dependencies first, and     ║
║ then run Playwright's install command:                                        ║
║                                                                               ║
║     npm install                                                               ║
║     npx playwright install                                                    ║
║                                                                               ║
║ If your project does not yet depend on Playwright, first install the          ║
║ applicable npm package (most commonly @playwright/test), and                  ║
║ then run Playwright's install command to download the browsers:               ║
║                                                                               ║
║     npm install @playwright/test                                              ║
║     npx playwright install                                                    ║
║                                                                               ║

npx playwright test的时候显示没有test

这样安装

npm install @playwright/test

然后

(py311) E:\work\playwright>npx playwright test
Error: No tests found

不管怎么说,在经过这几步操作后,有playwright这个命令了

playwright
Usage: playwright [options] [command]

Options:
  -V, --version                          output the version number
  -h, --help                             display help for command

Commands:
  open [options] [url]                   open page in browser specified via -b, --browser
  codegen [options] [url]                open page and generate code for user actions
  install [options] [browser...]         ensure browsers necessary for this version of Playwright are installed
  uninstall [options]                    Removes browsers used by this installation of Playwright from the system
                                         (chromium, firefox, webkit, ffmpeg). This does not include branded channels.
  install-deps [options] [browser...]    install dependencies necessary to run browsers (will ask for sudo permissions)
  cr [options] [url]                     open page in Chromium
  ff [options] [url]                     open page in Firefox
  wk [options] [url]                     open page in WebKit
  screenshot [options] <url> <filename>  capture a page screenshot
  pdf [options] <url> <filename>         save page as pdf
  run-server [options]
  show-trace [options] [trace...]        show trace viewer
  help [command]                         display help for command

这时候再执行测试:

node example.js

哈哈,生成了一副图片呢:

2025/03/27  13:45            17,117 example.png

相关文章:

  • OpenManus和OWL如何选?
  • 刷题记录(LeetCode 994.腐烂的橘子)
  • 使用ProcessBuilder执行FFmpeg命令,进程一直处于阻塞状态,一直没有返回执行结果
  • Pytorch学习笔记(十二)Learning PyTorch - NLP from Scratch
  • Springboot读取nacos配置
  • 从tensorflow导入keras失败?
  • deepseek本地部署并搭建个人知识库
  • AIP-192 文档
  • ROS2软件架构全面解析-rmw软件框架
  • 使用系统自带的磁盘清理工具清理c盘空间 以及手动删除临时文件
  • leetcode141.环形链表
  • 数据分析中的基线校正算法全解析:原理、实现与应用
  • 什么是Giteki认证?认证流程详解
  • 盾牌图鉴(死亡细胞)
  • Go 语言常见错误——控制结构
  • 前端知识点 --- border-Radius的四个角(css)
  • MSYS2:Windows 开发环境的技术革新
  • react + css 实现 椭圆布局
  • 赛逸展2025年重磅回归,科技盛宴再启新篇
  • Manus的开源替代者之一:OpenManus通用AI智能体框架解析及产品试用
  • 网站分辨率兼容怎么做/网站推广软件哪个最好
  • 网站如何做宣传推广/线上营销策略
  • 帮人做网站被派出所抓到徐州/网页模板之家
  • 小程序有什么用/深圳防疫措施优化
  • 购物网站css模板/免费域名解析网站
  • 常用网站后缀/常见的网络营销手段