puppeteer-browser-ready 1.1.0 → 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -10
- package/dist/puppeteer-browser-ready.d.ts +1 -1
- package/dist/puppeteer-browser-ready.js +16 -27
- package/package.json +16 -19
- package/dist/puppeteer-browser-ready.umd.cjs +0 -110
package/README.md
CHANGED
|
@@ -5,11 +5,10 @@ _Simple utility to go to a URL and wait for the HTTP response_
|
|
|
5
5
|
|
|
6
6
|
[](https://github.com/center-key/puppeteer-browser-ready/blob/main/LICENSE.txt)
|
|
7
7
|
[](https://www.npmjs.com/package/puppeteer-browser-ready)
|
|
8
|
-
[](https://snyk.io/test/github/center-key/puppeteer-browser-ready)
|
|
9
8
|
[](https://github.com/center-key/puppeteer-browser-ready/actions/workflows/run-spec-on-push.yaml)
|
|
10
9
|
|
|
11
|
-
**puppeteer-browser-ready** is a
|
|
12
|
-
|
|
10
|
+
**puppeteer-browser-ready** is a helper utility to reduce the amount of boilerplate code needed
|
|
11
|
+
to tell Puppeteer to visit a web page and and retrieve the HTML.
|
|
13
12
|
It's primarily intended for use within [Mocha](https://mochajs.org) test cases.
|
|
14
13
|
In addition to the raw HTML, you get a [cheerio](https://cheerio.js.org) reference so you can
|
|
15
14
|
immediately run queries on the DOM.
|
|
@@ -63,7 +62,7 @@ type Web = {
|
|
|
63
62
|
location: Location,
|
|
64
63
|
title: string,
|
|
65
64
|
html: string,
|
|
66
|
-
$: cheerio.Root | null, //
|
|
65
|
+
$: cheerio.Root | null, //library for parsing and manipulating HTML
|
|
67
66
|
};
|
|
68
67
|
```
|
|
69
68
|
|
|
@@ -138,12 +137,8 @@ describe('The web page', () => {
|
|
|
138
137
|
});
|
|
139
138
|
|
|
140
139
|
it('has exactly one header, main, and footer', () => {
|
|
141
|
-
const actual =
|
|
142
|
-
|
|
143
|
-
main: web.$('body >main').length,
|
|
144
|
-
footer: web.$('body >footer').length,
|
|
145
|
-
};
|
|
146
|
-
const expected = { header: 1, main: 1, footer: 1 };
|
|
140
|
+
const actual = web.$('body >*').toArray().map(elem => elem.name);
|
|
141
|
+
const expected = ['header', 'main', 'footer'];
|
|
147
142
|
assertDeepStrictEqual(actual, expected);
|
|
148
143
|
});
|
|
149
144
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//! puppeteer-browser-ready v1.1
|
|
1
|
+
//! puppeteer-browser-ready v1.2.1 ~~ https://github.com/center-key/puppeteer-browser-ready ~~ MIT License
|
|
2
2
|
|
|
3
3
|
/// <reference types="cheerio" />
|
|
4
4
|
import { Browser, HTTPResponse, Page } from 'puppeteer';
|
|
@@ -1,14 +1,5 @@
|
|
|
1
|
-
//! puppeteer-browser-ready v1.1
|
|
1
|
+
//! puppeteer-browser-ready v1.2.1 ~~ https://github.com/center-key/puppeteer-browser-ready ~~ MIT License
|
|
2
2
|
|
|
3
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
4
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
5
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
6
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
7
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
8
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
9
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
10
|
-
});
|
|
11
|
-
};
|
|
12
3
|
import cheerio from 'cheerio';
|
|
13
4
|
import express from 'express';
|
|
14
5
|
import httpTerminator from 'http-terminator';
|
|
@@ -20,7 +11,7 @@ const browserReady = {
|
|
|
20
11
|
},
|
|
21
12
|
startWebServer(options) {
|
|
22
13
|
const defaults = { folder: '.', port: 0, verbose: true, autoCleanup: true };
|
|
23
|
-
const settings =
|
|
14
|
+
const settings = { ...defaults, ...options };
|
|
24
15
|
const server = express().use(express.static(settings.folder)).listen(settings.port);
|
|
25
16
|
const terminator = httpTerminator.createHttpTerminator({ server });
|
|
26
17
|
const port = () => server.address().port;
|
|
@@ -52,24 +43,24 @@ const browserReady = {
|
|
|
52
43
|
},
|
|
53
44
|
goto(url, options) {
|
|
54
45
|
const defaults = { addCheerio: true, verbose: false };
|
|
55
|
-
const settings =
|
|
46
|
+
const settings = { ...defaults, ...options };
|
|
56
47
|
const log = (label, msg) => settings.verbose &&
|
|
57
48
|
console.log(' ', Date.now() % 100000, label + ':', msg);
|
|
58
|
-
const web = (browser) =>
|
|
49
|
+
const web = async (browser) => {
|
|
59
50
|
log('Connected', browser.isConnected());
|
|
60
51
|
try {
|
|
61
|
-
const page =
|
|
52
|
+
const page = await browser.newPage();
|
|
62
53
|
log('Page....', url);
|
|
63
|
-
const response =
|
|
64
|
-
log('Response', response
|
|
54
|
+
const response = await page.goto(url);
|
|
55
|
+
log('Response', response?.url());
|
|
65
56
|
const status = response && response.status();
|
|
66
57
|
log('Status', status);
|
|
67
|
-
const location =
|
|
58
|
+
const location = await page.evaluate(() => globalThis.location);
|
|
68
59
|
log('Host', location.host);
|
|
69
|
-
const title = response &&
|
|
60
|
+
const title = response && await page.title();
|
|
70
61
|
log('Title', title);
|
|
71
|
-
const html = response &&
|
|
72
|
-
log('Bytes', html
|
|
62
|
+
const html = response && await response.text();
|
|
63
|
+
log('Bytes', html?.length);
|
|
73
64
|
const $ = html && settings.addCheerio ? cheerio.load(html) : null;
|
|
74
65
|
log('$', $ && $['fn'].constructor.name);
|
|
75
66
|
return { browser, page, response, status, location, title, html, $ };
|
|
@@ -80,15 +71,13 @@ const browserReady = {
|
|
|
80
71
|
console.log(error);
|
|
81
72
|
throw error;
|
|
82
73
|
}
|
|
83
|
-
}
|
|
74
|
+
};
|
|
84
75
|
return web;
|
|
85
76
|
},
|
|
86
|
-
close(web) {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
return web;
|
|
91
|
-
});
|
|
77
|
+
async close(web) {
|
|
78
|
+
if (web && web.browser)
|
|
79
|
+
await web.browser.close();
|
|
80
|
+
return web;
|
|
92
81
|
},
|
|
93
82
|
};
|
|
94
83
|
export { browserReady };
|
package/package.json
CHANGED
|
@@ -1,19 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "puppeteer-browser-ready",
|
|
3
|
-
"version": "1.1
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "Simple utility to go to a URL and wait for the HTTP response",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"module": "dist/puppeteer-browser-ready.js",
|
|
8
|
-
"main": "dist/puppeteer-browser-ready.
|
|
8
|
+
"main": "dist/puppeteer-browser-ready.js",
|
|
9
9
|
"types": "dist/puppeteer-browser-ready.d.ts",
|
|
10
10
|
"files": [
|
|
11
11
|
"dist"
|
|
12
12
|
],
|
|
13
13
|
"exports": {
|
|
14
14
|
".": {
|
|
15
|
-
"import": "./dist/puppeteer-browser-ready.js"
|
|
16
|
-
"require": "./dist/puppeteer-browser-ready.umd.cjs"
|
|
15
|
+
"import": "./dist/puppeteer-browser-ready.js"
|
|
17
16
|
},
|
|
18
17
|
"./": "./dist/"
|
|
19
18
|
},
|
|
@@ -72,8 +71,6 @@
|
|
|
72
71
|
"jshint . --exclude-path .gitignore",
|
|
73
72
|
"eslint --max-warnings 0 . --ext .ts",
|
|
74
73
|
"tsc",
|
|
75
|
-
"tsc --module UMD --outDir build/umd",
|
|
76
|
-
"copy-file build/umd/puppeteer-browser-ready.js build/puppeteer-browser-ready.umd.cjs",
|
|
77
74
|
"add-dist-header build dist",
|
|
78
75
|
"html-validator"
|
|
79
76
|
]
|
|
@@ -83,7 +80,7 @@
|
|
|
83
80
|
"test": "mocha spec/*.spec.js --timeout 5000 --retries 1"
|
|
84
81
|
},
|
|
85
82
|
"peerDependencies": {
|
|
86
|
-
"puppeteer": "^
|
|
83
|
+
"puppeteer": "^15 || ^16 || ^17 || ^18 || ^19.7 || ^20 || ^21"
|
|
87
84
|
},
|
|
88
85
|
"dependencies": {
|
|
89
86
|
"cheerio": "~1.0.0-rc.12",
|
|
@@ -94,22 +91,22 @@
|
|
|
94
91
|
"@types/cheerio": "~0.22",
|
|
95
92
|
"@types/express": "~4.17",
|
|
96
93
|
"@types/mocha": "~10.0",
|
|
97
|
-
"@types/node": "~
|
|
94
|
+
"@types/node": "~20.4",
|
|
98
95
|
"@types/ws": "~8.5",
|
|
99
|
-
"@typescript-eslint/eslint-plugin": "~
|
|
100
|
-
"@typescript-eslint/parser": "~
|
|
101
|
-
"add-dist-header": "~1.
|
|
102
|
-
"assert-deep-strict-equal": "~1.
|
|
103
|
-
"copy-file-util": "~1.
|
|
104
|
-
"copy-folder-util": "~1.
|
|
105
|
-
"eslint": "~8.
|
|
96
|
+
"@typescript-eslint/eslint-plugin": "~6.2",
|
|
97
|
+
"@typescript-eslint/parser": "~6.2",
|
|
98
|
+
"add-dist-header": "~1.1",
|
|
99
|
+
"assert-deep-strict-equal": "~1.1",
|
|
100
|
+
"copy-file-util": "~1.1",
|
|
101
|
+
"copy-folder-util": "~1.1",
|
|
102
|
+
"eslint": "~8.46",
|
|
106
103
|
"jshint": "~2.13",
|
|
107
104
|
"mocha": "~10.2",
|
|
108
105
|
"open": "~9.1",
|
|
109
|
-
"puppeteer": "~
|
|
106
|
+
"puppeteer": "~21.0",
|
|
110
107
|
"rimraf": "~5.0",
|
|
111
|
-
"run-scripts-util": "~1.
|
|
112
|
-
"typescript": "~5.
|
|
113
|
-
"w3c-html-validator": "~1.
|
|
108
|
+
"run-scripts-util": "~1.2",
|
|
109
|
+
"typescript": "~5.1",
|
|
110
|
+
"w3c-html-validator": "~1.4"
|
|
114
111
|
}
|
|
115
112
|
}
|
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
//! puppeteer-browser-ready v1.1.0 ~~ https://github.com/center-key/puppeteer-browser-ready ~~ MIT License
|
|
2
|
-
|
|
3
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
4
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
5
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
6
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
7
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
8
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
9
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
10
|
-
});
|
|
11
|
-
};
|
|
12
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
13
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
14
|
-
};
|
|
15
|
-
(function (factory) {
|
|
16
|
-
if (typeof module === "object" && typeof module.exports === "object") {
|
|
17
|
-
var v = factory(require, exports);
|
|
18
|
-
if (v !== undefined) module.exports = v;
|
|
19
|
-
}
|
|
20
|
-
else if (typeof define === "function" && define.amd) {
|
|
21
|
-
define(["require", "exports", "cheerio", "express", "http-terminator"], factory);
|
|
22
|
-
}
|
|
23
|
-
})(function (require, exports) {
|
|
24
|
-
"use strict";
|
|
25
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.browserReady = void 0;
|
|
27
|
-
const cheerio_1 = __importDefault(require("cheerio"));
|
|
28
|
-
const express_1 = __importDefault(require("express"));
|
|
29
|
-
const http_terminator_1 = __importDefault(require("http-terminator"));
|
|
30
|
-
// Package
|
|
31
|
-
const browserReady = {
|
|
32
|
-
log(...args) {
|
|
33
|
-
const indent = typeof globalThis.describe === 'function' ? ' [' : '[';
|
|
34
|
-
console.log(indent + new Date().toISOString() + ']', ...args);
|
|
35
|
-
},
|
|
36
|
-
startWebServer(options) {
|
|
37
|
-
const defaults = { folder: '.', port: 0, verbose: true, autoCleanup: true };
|
|
38
|
-
const settings = Object.assign(Object.assign({}, defaults), options);
|
|
39
|
-
const server = (0, express_1.default)().use(express_1.default.static(settings.folder)).listen(settings.port);
|
|
40
|
-
const terminator = http_terminator_1.default.createHttpTerminator({ server });
|
|
41
|
-
const port = () => server.address().port;
|
|
42
|
-
const url = () => 'http://localhost:' + String(port()) + '/';
|
|
43
|
-
const logListening = () => this.log('Web Server - listening:', server.listening, port(), url());
|
|
44
|
-
const logClose = () => this.log('Web Server - shutdown:', !server.listening);
|
|
45
|
-
const http = () => ({
|
|
46
|
-
server: server,
|
|
47
|
-
terminator: terminator,
|
|
48
|
-
folder: settings.folder,
|
|
49
|
-
url: url(),
|
|
50
|
-
port: port(),
|
|
51
|
-
verbose: settings.verbose,
|
|
52
|
-
});
|
|
53
|
-
let done;
|
|
54
|
-
server.on('listening', () => done(http()));
|
|
55
|
-
if (settings.verbose)
|
|
56
|
-
server.on('listening', logListening).on('close', logClose);
|
|
57
|
-
const cleanup = () => {
|
|
58
|
-
console.log('[SIGINT]');
|
|
59
|
-
terminator.terminate();
|
|
60
|
-
};
|
|
61
|
-
if (settings.autoCleanup)
|
|
62
|
-
process.on('SIGINT', cleanup);
|
|
63
|
-
return new Promise(resolve => done = resolve);
|
|
64
|
-
},
|
|
65
|
-
shutdownWebServer(http) {
|
|
66
|
-
return http.terminator.terminate();
|
|
67
|
-
},
|
|
68
|
-
goto(url, options) {
|
|
69
|
-
const defaults = { addCheerio: true, verbose: false };
|
|
70
|
-
const settings = Object.assign(Object.assign({}, defaults), options);
|
|
71
|
-
const log = (label, msg) => settings.verbose &&
|
|
72
|
-
console.log(' ', Date.now() % 100000, label + ':', msg);
|
|
73
|
-
const web = (browser) => __awaiter(this, void 0, void 0, function* () {
|
|
74
|
-
log('Connected', browser.isConnected());
|
|
75
|
-
try {
|
|
76
|
-
const page = yield browser.newPage();
|
|
77
|
-
log('Page....', url);
|
|
78
|
-
const response = yield page.goto(url);
|
|
79
|
-
log('Response', response === null || response === void 0 ? void 0 : response.url());
|
|
80
|
-
const status = response && response.status();
|
|
81
|
-
log('Status', status);
|
|
82
|
-
const location = yield page.evaluate(() => globalThis.location);
|
|
83
|
-
log('Host', location.host);
|
|
84
|
-
const title = response && (yield page.title());
|
|
85
|
-
log('Title', title);
|
|
86
|
-
const html = response && (yield response.text());
|
|
87
|
-
log('Bytes', html === null || html === void 0 ? void 0 : html.length);
|
|
88
|
-
const $ = html && settings.addCheerio ? cheerio_1.default.load(html) : null;
|
|
89
|
-
log('$', $ && $['fn'].constructor.name);
|
|
90
|
-
return { browser, page, response, status, location, title, html, $ };
|
|
91
|
-
}
|
|
92
|
-
catch (error) {
|
|
93
|
-
const status = browser.isConnected() ? 'connected' : 'not connected';
|
|
94
|
-
console.log('[puppeteer-browser-ready]', settings, status);
|
|
95
|
-
console.log(error);
|
|
96
|
-
throw error;
|
|
97
|
-
}
|
|
98
|
-
});
|
|
99
|
-
return web;
|
|
100
|
-
},
|
|
101
|
-
close(web) {
|
|
102
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
103
|
-
if (web && web.browser)
|
|
104
|
-
yield web.browser.close();
|
|
105
|
-
return web;
|
|
106
|
-
});
|
|
107
|
-
},
|
|
108
|
-
};
|
|
109
|
-
exports.browserReady = browserReady;
|
|
110
|
-
});
|