Simple utility to go to a URL and wait for the HTTP response
puppeteer-browser-ready is a helper utility to reduce the amount of boilerplate code needed to tell Puppeteer to visit a web page and and retrieve the HTML.Β It's primarily intended for use within Mocha test cases.Β In addition to the raw HTML, you get a node-html-parsed root so you can immediately run queries on the DOM.
Install packages:
$ npm install --save-dev puppeteer puppeteer-browser-ready
Import packages:
import puppeteer from 'puppeteer';
import { browserReady } from 'puppeteer-browser-ready';
Use the browserReady.goto(url, options)
function to tell Puppeteer which page to open.
The Promise will resolve with a Web object containing a title
field and a html
field.
Pass the Web object to the browserReady.close(web)
function to disconnect the page.
const url = 'https://pretty-print-json.js.org/';
let web; //fields: browser, page, response, status, location, title, html, root
before(async () => web = await puppeteer.launch().then(browserReady.goto(url));
after(async () => await browserReady.close(web));
Name (key) | Type | Default | Description |
---|---|---|---|
parseHtml |
boolean | true |
Return the DOM root as an HTMLElement (node-html-parsed). |
verbose |
boolean | false |
Output HTTP connection debug messages. |
Name (key) | Type | Default | Description |
---|---|---|---|
autoCleanup |
boolean | true |
Terminate connection on interruption (SIGINT ). |
folder |
string | '.' |
Document root for the static web server. |
port |
number | 0 |
Port number for server (0 find open port). |
verbose |
boolean | true |
Output informational messages. |
See the TypeScript declarations at the top of the puppeteer-browser-ready.ts file.
The browserReady.goto(url, options)
function returns a function that takes a Puppeteer Browser
object and returns a Promise that resolves with a Web object:
type Web = {
browser: Puppeteer.Browser,
page: Puppeteer.Page,
response: HTTPResponse | null,
location: Location,
title: string,
html: string,
root: HTMLElement | null, //see node-html-parsed library
};
The optional browserReady.startWebServer(options)
function starts a static web server and returns
a Promise for when the server is ready:
export type Http = {
server: Server,
terminator: httpTerminator.HttpTerminator,
folder: string,
url: string,
port: number,
verbose: boolean,
};
Code:
import puppeteer from 'puppeteer';
import { browserReady } from 'puppeteer-browser-ready';
const handleResponse = (web) => {
console.log('Hello, World!');
console.log('web fields:', Object.keys(web).join(', '));
console.log(`The HTML from ${web.location.href} is ${web.html.length} characters`,
`long and contains ${web.root.querySelectorAll('p').length} <p> tags.`);
return web;
};
puppeteer.launch()
.then(browserReady.goto('https://pretty-print-json.js.org/'))
.then(handleResponse)
.then(browserReady.close);
Output:
Hello, World!
web fields: browser, page, response, status, location, title, html, root
The HTML from https://pretty-print-json.js.org/ is 8200 characters
long and contains 7 <p> tags.