puppeteer-browser-ready 1.1.0 → 1.2.0
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
CHANGED
|
@@ -8,8 +8,8 @@ _Simple utility to go to a URL and wait for the HTTP response_
|
|
|
8
8
|
[](https://snyk.io/test/github/center-key/puppeteer-browser-ready)
|
|
9
9
|
[](https://github.com/center-key/puppeteer-browser-ready/actions/workflows/run-spec-on-push.yaml)
|
|
10
10
|
|
|
11
|
-
**puppeteer-browser-ready** is a
|
|
12
|
-
|
|
11
|
+
**puppeteer-browser-ready** is a helper utility to reduce the amount of boilerplate code needed
|
|
12
|
+
to tell Puppeteer to visit a web page and and retrieve the HTML.
|
|
13
13
|
It's primarily intended for use within [Mocha](https://mochajs.org) test cases.
|
|
14
14
|
In addition to the raw HTML, you get a [cheerio](https://cheerio.js.org) reference so you can
|
|
15
15
|
immediately run queries on the DOM.
|
|
@@ -63,7 +63,7 @@ type Web = {
|
|
|
63
63
|
location: Location,
|
|
64
64
|
title: string,
|
|
65
65
|
html: string,
|
|
66
|
-
$: cheerio.Root | null, //
|
|
66
|
+
$: cheerio.Root | null, //library for parsing and manipulating HTML
|
|
67
67
|
};
|
|
68
68
|
```
|
|
69
69
|
|
|
@@ -138,12 +138,8 @@ describe('The web page', () => {
|
|
|
138
138
|
});
|
|
139
139
|
|
|
140
140
|
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 };
|
|
141
|
+
const actual = web.$('body >*').toArray().map(elem => elem.name);
|
|
142
|
+
const expected = ['header', 'main', 'footer'];
|
|
147
143
|
assertDeepStrictEqual(actual, expected);
|
|
148
144
|
});
|
|
149
145
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
//! puppeteer-browser-ready v1.
|
|
1
|
+
//! puppeteer-browser-ready v1.2.0 ~~ 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,4 +1,4 @@
|
|
|
1
|
-
//! puppeteer-browser-ready v1.
|
|
1
|
+
//! puppeteer-browser-ready v1.2.0 ~~ https://github.com/center-key/puppeteer-browser-ready ~~ MIT License
|
|
2
2
|
|
|
3
3
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
4
4
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
package/package.json
CHANGED
|
@@ -1,19 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "puppeteer-browser-ready",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
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
|
]
|
|
@@ -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.3",
|
|
98
95
|
"@types/ws": "~8.5",
|
|
99
|
-
"@typescript-eslint/eslint-plugin": "~5.
|
|
100
|
-
"@typescript-eslint/parser": "~5.
|
|
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": "~5.60",
|
|
97
|
+
"@typescript-eslint/parser": "~5.60",
|
|
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.43",
|
|
106
103
|
"jshint": "~2.13",
|
|
107
104
|
"mocha": "~10.2",
|
|
108
105
|
"open": "~9.1",
|
|
109
|
-
"puppeteer": "~20.
|
|
106
|
+
"puppeteer": "~20.7",
|
|
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
|
-
});
|