nightmare 是一个高层次的浏览器自动化库,旨在提供简单的 API 以便于快速开发和测试。它使用 Electron 作为基础,适合于需要快速构建和测试的项目。Nightmare 的 API 直观易用,适合进行简单的网页抓取和自动化任务。如果你的需求不复杂,Nightmare 可能是一个不错的选择。
playwright 是一个由 Microsoft 开发的现代浏览器自动化库,支持多种浏览器(包括 Chromium、Firefox 和 WebKit)。Playwright 提供了强大的功能,如跨浏览器测试、自动等待和内置的网络拦截等。它的设计目标是提供更高的灵活性和更强的功能,适合需要复杂测试和多浏览器支持的应用程序。如果你需要更强大的功能和更广泛的浏览器支持,Playwright 是一个理想的选择。
puppeteer 是一个由 Google 开发的 Node.js 库,提供了一个高级 API 来控制无头 Chrome 或 Chromium 浏览器。Puppeteer 适合需要进行网页抓取、生成 PDF 或截图等任务的场景。虽然它不专注于端到端测试,但可以用于编写自动化测试脚本,尤其是在需要与浏览器进行深度交互时。
testcafe 是一个开源的端到端测试框架,支持多种浏览器和平台。TestCafe 的特点是无需安装浏览器插件,支持并行测试和自动等待功能。它的 API 设计简单易用,适合快速上手和编写测试。
Puppeteer is a JavaScript library which provides a high-level API to control
Chrome or Firefox over the
DevTools Protocol or WebDriver BiDi.
Puppeteer runs in the headless (no visible UI) by default
npm i puppeteer # Downloads compatible Chrome during installation.
npm i puppeteer-core # Alternatively, install as a library, without downloading Chrome.
Example
import puppeteer from 'puppeteer';
// Or import puppeteer from 'puppeteer-core';
// Launch the browser and open a new blank page.
const browser = await puppeteer.launch();
const page = await browser.newPage();
// Navigate the page to a URL.
await page.goto('https://developer.chrome.com/');
// Set screen size.
await page.setViewport({width: 1080, height: 1024});
// Open the search menu using the keyboard.
await page.keyboard.press('/');
// Type into search box using accessible input name.
await page.locator('::-p-aria(Search)').fill('automate beyond recorder');
// Wait and click on first result.
await page.locator('.devsite-result-item-link').click();
// Locate the full title with a unique string.
const textSelector = await page
.locator('::-p-text(Customize and automate)')
.waitHandle();
const fullTitle = await textSelector?.evaluate(el => el.textContent);
// Print the full title.
console.log('The title of this blog post is "%s".', fullTitle);
await browser.close();