puppeteer:官方出品的chrome浏览器自动化测试工具

puppeteer发布应该有一段时间了,这两天正好基于该工具写了一些自动化解决方案,在这里抛砖引给大家介绍一下。

官方描述:

Puppeteer is a Node library which provides a high-level API to control Chrome or Chromium over the DevTools Protocol. Puppeteer runs headless by default, but can be configured to run full (non-headless) Chrome or Chromium.

简单来说,puppeteer的特点如下

  • 是node的库
  • 基于DevTools Protocol协议
  • 默认是无界面模式运行

安装

npm i puppeteer
# or "yarn add puppeteer"

基本使用方式

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://example.com');
  await page.screenshot({path: 'example.png'});

  await browser.close();
})();

上面代码的作用是打开一个页面,然后给这个页面截图,最后关闭浏览器。

想象空间

  • 可以做一些界面的自动化工作
  • 可以做爬虫
  • 可以在服务器上稳定运行,方便容器化

更多例子

将页面保存成pdf的例子

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://news.ycombinator.com', {waitUntil: 'networkidle2'});
  await page.pdf({path: 'hn.pdf', format: 'A4'});

  await browser.close();
})();

在页面上下文执行js的例子

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://example.com');

  // Get the "viewport" of the page, as reported by the page.
  const dimensions = await page.evaluate(() => {
    return {
      width: document.documentElement.clientWidth,
      height: document.documentElement.clientHeight,
      deviceScaleFactor: window.devicePixelRatio
    };
  });

  console.log('Dimensions:', dimensions);

  await browser.close();
})();

在亚马逊搜索商品的例子

/**
 * @name Amazon search
 *
 * @desc Looks for a "nyan cat pullover" on amazon.com, goes two page two clicks the third one.
 */
const puppeteer = require('puppeteer')
const screenshot = 'amazon_nyan_cat_pullover.png'
try {
  (async () => {
    const browser = await puppeteer.launch()
    const page = await browser.newPage()
    await page.setViewport({ width: 1280, height: 800 })
    await page.goto('https://www.amazon.com')
    await page.type('#twotabsearchtextbox', 'nyan cat pullover')
    await page.click('input.nav-input')
    await page.waitForSelector('#resultsCol')
    await page.screenshot({path: 'amazon_nyan_cat_pullovers_list.png'})
    await page.click('#pagnNextString')
    await page.waitForSelector('#resultsCol')
    const pullovers = await page.$$('a.a-link-normal.a-text-normal')
    await pullovers[2].click()
    await page.waitForSelector('#ppd')
    await page.screenshot({path: screenshot})
    await browser.close()
    console.log('See screenshot: ' + screenshot)
  })()
} catch (err) {
  console.error(err)
}

登陆github的例子

/**
 * @name Github
 *
 * @desc Logs into Github. Provide your username and password as environment variables when running the script, i.e:
 * `GITHUB_USER=myuser GITHUB_PWD=mypassword node github.js`
 *
 */
const puppeteer = require('puppeteer')
const screenshot = 'github.png';
(async () => {
  const browser = await puppeteer.launch({headless: true})
  const page = await browser.newPage()
  await page.goto('https://github.com/login')
  await page.type('#login_field', process.env.GITHUB_USER)
  await page.type('#password', process.env.GITHUB_PWD)
  await page.click('[name="commit"]')
  await page.waitForNavigation()
  await page.screenshot({ path: screenshot })
  browser.close()
  console.log('See screenshot: ' + screenshot)
})()

常见问题

谁在维护puppeteer?

Chrome DevTools 团队

Puppeteer可以替换selenium/webdriver吗?

不可以。这2个工具的目的是不一样的。

selenium的目的是一套脚本运行在不同浏览器上,可以做兼容性测试;

puppeteer专注于Chromium的功能测试。

相关资料

原文地址:https://www.cnblogs.com/nbkhic/p/9372640.html

时间: 2024-08-30 16:33:40

puppeteer:官方出品的chrome浏览器自动化测试工具的相关文章

谷歌Chrome浏览器开发者工具的基础功能

上一篇我们学习了谷歌Chrome浏览器开发者工具的基础功能,下面介绍的是Chrome开发工具中最有用的面板Sources.Sources面板几乎是最常用到的Chrome功能面板,也是解决一般问题的主要功能面板.通常只要是开发遇到了js报错或者其他代码问题,在审视一遍代码而一无所获之后打开Sources进行js断点调试,几乎能解决8成的代码问题. js断点功能让人兴奋不已,以前只能在IE中靠alert弹出窗口调试js代码,那样的开发环境对于前端程序员来说简直是一场噩梦.本篇介绍Sources的具体

谷歌 chrome 浏览器开发者工具打不开的解决方法

很长一段时间没有用Chrome浏览器 最近打开,发现开发者工具怎么都打不开 在网上也没有找到方法 各种打开方式都试了依然是没有动静 但是在隐身模式下开发者工具是可以的打开的 重新安装后还是一样 实在没有找到方法 就在浏览器里各种尝试 最后我打开扩展程序里 里面勾选了开发者模式 我把取消后开发者工具就可以正常使用了 不知道有没有其他人遇到开发者工具打不开的问题 如果有的话可以尝试看下扩展程序内是否勾选了开发者模式 如是取消就可以正常使用了

chrome浏览器 开发者工具简介

Chrome浏览器得益于其优秀的V8解释器,javascript执行速度和内存占有率表现非常优秀. 掌握了Chrome工具可提高学习效率和开发效率. 有如下功能面板,可以使用Ctrl+[和Ctrl+]快捷键在面板之间切换. 简单常用的就一笔带过 一.标签页 查看DOM tree内容 根据标签页的html内容找到对应元素 根据页面元素找到标签页的html 改变DOM内容 查看js动态生成的DOM 在标签页右侧查看style 查找 标签注册事件查看.[待扩展] 按钮绑定哪些功能,鼠标点击时执行了那个

Selenium浏览器自动化测试工具

selenium简介 介绍 Selenium [1]  是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE(7, 8, 9, 10, 11),Mozilla Firefox,Safari,Google Chrome,Opera等.这个工具的主要功能包括:测试与浏览器的兼容性——测试你的应用程序看是否能够很好得工作在不同浏览器和操作系统之上.测试系统功能——创建回归测试检验软件功能和用户需求.支持自动录制动作和自动生成 .Ne

chrome 浏览器 开发者工具 性能检测 参数解释

Sending is time spent uploading the data/request to the server. It occurs between blocking and waiting. For example, if I post back an ASPX page this would indicate the amount of time it took to upload the request (including the values of the forms a

SocketLog-微信调试、API调试和AJAX的调试的工具,能将日志通过WebSocket输出到Chrome浏览器的console中

说明 SocketLog适合Ajax调试和API调试, 举一个常见的场景,用SocketLog来做微信调试, 我们在做微信API开发的时候,如果API有bug,微信只提示"改公众账号暂时无法提供服务,请稍候再试" ,我们根本不知道API出来什么问题. 有了SocketLog就不一样了, 我们可以知道微信给API传递了哪些参数, 程序有错误我们也能看见错误信息(下方有张图片,可能加载慢,请耐心等待一下) 正在运行的API有bug,不能var_dump进行调试,因为会影响client的调用

Chrome浏览器F12开发者工具使用教程博客汇总

CSDN 卜小娴 <Chrome开发者工具使用小技巧> CSDN csdnligao <chrome开发者工具的使用> CSDN 菜菜格子 <谷歌Chrome浏览器开发者工具教程-JS调试篇> CnBlogs 逆天妖精 <Chrome浏览器F12开发者工具简单使用> 原文地址:https://www.cnblogs.com/hyeTi/p/10337643.html

Chrome浏览器Network面板http请求时间分析

Chrome浏览器开发者工具Network窗口下,可以查看下载各组件所需的具体时间 根据上表进行简要分析—— Stalled(阻塞) 浏览器对同一个主机域名的并发连接数有限制,因此如果当前的连接数已经超过上限,那么其余请求就会被阻塞,等待新的可用连接:此外脚本也会阻塞其他组件的下载: 优化措施: 1.将资源合理分布到多台主机上,可以提高并发数,但是增加并行下载数量也会增大开销,这取决于带宽和CPU速度,过多的并行下载会降低性能: 2.脚本置于页面底部: DNS Lookup(域名解析) 请求某域

Google Chrome浏览器扩展的调试

1)      查看扩展程序的详细信息和ID 通过Chrome 浏览器的“ 工具->更多工具->扩展程序”,打开chrome://extensions页面,选中右上角的“开发者模式”,可以查看扩展程序的详细信息(如名称.概述.占用空间大小.版本和权限等)和ID. 2)      查看扩展程序的popup页面 通过Chrome 浏览器的“工具 ->更多工具->扩展程序”,打开chrome://extensions页面,选中右上角的“开发者模式”(必须在开发者模式下才能够看到调试菜单项