Puppeteer 简明教程

Puppeteer - Introduction

Puppeteer 用于自动化和简化前端开发和测试。它由 Google 推出。Puppeteer 基于 Node.js 库,并且是开源的。

Puppeteer is used for automation and streamlining of the frontend development and testing respectively. It was introduced by Google. Puppeteer is based on the Node.js library and is open-source.

Puppeteer 包含用于以无头模式或 Chromium(遵循 DevTools 中的协议)交互和管理 Chrome 浏览器的 API。但是,它还可用于在 Chrome/Chromium/Edge/Firefox 等浏览器上执行非无头操作。

Puppeteer contains APIs to interact and manage Chrome browser in headless mode or Chromium (following the protocols in DevTools). However, it can also be used for non-headless execution on browsers like Chrome/Chromium/Edge/Firefox.

Puppeteer 可用于自动执行大部分 UI 测试、键盘、鼠标移动等操作。它可用于测试使用 Angular 和 Angularjs 开发的应用程序。使用 Puppeteer 可以执行网页抓取和抓取等操作。

Puppeteer can be used for the automating majority of UI testing, keyboards, mouse movements, and so on. It can be used to test applications developed in Angular and Angularjs. The actions like web page crawling and scraping can be performed with Puppeteer.

Puppeteer 不被视为 Selenium、Cypress、Protractor 等自动化工具。它主要用于管理 Chromium 浏览器的内部功能。我们可以通过按 F12 或 Command+Option+C(在 MacOS 中)在 Chrome 浏览器中打开 DevTools。

Puppeteer is not considered as an automation tool like Selenium, Cypress, Protractor, and so on. It is mostly used to manage the internal features of the Chromium browser. We can open DevTools in the Chrome browser, by pressing F12 or Command+Option+C(in MacOS).

automation and streamlining

Puppeteer 类似于开发工具,因为它能够执行开发人员执行的大部分任务,如处理请求和响应、查找元素、网络流量和性能等等。

Puppeteer is like a development tool as it is capable of performing a majority of tasks performed by a developer like handling requests and responses, locating elements, network traffic and performance, and so on.

如果追溯过去几年 Puppeteer 下载的 npm 趋势,我们将观察到 Puppeteer 使用的增长趋势(可从以下链接获得)−

If we follow the npm trends for Puppeteer download for the last few years, we shall observe an upward trend towards the use of Puppeteer (available from the below link) −

puppeteer downloads

Puppeteer Architecture

Puppeteer 利用 Node 库,该库提供了用于管理 Chromium 或 Chrome 浏览器的顶级 API。这可以通过遵循 DevTools 的协议来完成。

Puppeteer utilises the Node library that gives a top-class API for managing Chromium or Chrome browsers. This is done by following the protocols of DevTools.

Puppeteer 具有以下层级 −

Puppeteer has the below hierarchy −

  1. Browser(with/without headless mode) − The browser performs the actions to be executed on the browser engine.

  2. Chromium Development Project or CDP − The Chromium is the real place where all the operations are executed. The browsers - Microsoft Edge and Chrome utilise Chromium as browser engine.

  3. Puppeteer − This is actually a package based on the node module.

  4. Automation test code − This is also known as the Nodejs level. Here, the actual automation code is developed by the end-user using JavaScript.