puppeteer-browser-ready 1.2.1 → 1.2.3
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 +14 -13
- package/dist/puppeteer-browser-ready.d.ts +4 -1
- package/dist/puppeteer-browser-ready.js +10 -5
- package/package.json +16 -13
package/README.md
CHANGED
|
@@ -10,8 +10,8 @@ _Simple utility to go to a URL and wait for the HTTP response_
|
|
|
10
10
|
**puppeteer-browser-ready** is a helper utility to reduce the amount of boilerplate code needed
|
|
11
11
|
to tell Puppeteer to visit a web page and and retrieve the HTML.
|
|
12
12
|
It's primarily intended for use within [Mocha](https://mochajs.org) test cases.
|
|
13
|
-
In addition to the raw HTML, you get a [
|
|
14
|
-
immediately run queries on the DOM.
|
|
13
|
+
In addition to the raw HTML, you get a [node-html-parsed](https://github.com/taoqf/node-html-parser)
|
|
14
|
+
root so you can immediately run queries on the DOM.
|
|
15
15
|
|
|
16
16
|
## A) Setup
|
|
17
17
|
**Install packages:**
|
|
@@ -30,15 +30,15 @@ The **Promise** will resolve with a **Web** object containing a `title` field an
|
|
|
30
30
|
Pass the **Web** object to the `browserReady.close(web)` function to disconnect the page.
|
|
31
31
|
```javascript
|
|
32
32
|
const url = 'https://pretty-print-json.js.org/';
|
|
33
|
-
let web; //fields: browser, page, response, status, location, title, html,
|
|
33
|
+
let web; //fields: browser, page, response, status, location, title, html, root
|
|
34
34
|
before(async () => web = await puppeteer.launch().then(browserReady.goto(url));
|
|
35
35
|
after(async () => await browserReady.close(web));
|
|
36
36
|
```
|
|
37
37
|
### `goto()` Options
|
|
38
|
-
| Name (key) | Type | Default | Description
|
|
39
|
-
| :----------- | :---------- | :------ |
|
|
40
|
-
| `
|
|
41
|
-
| `verbose` | **boolean** | `false` | Output HTTP connection debug messages.
|
|
38
|
+
| Name (key) | Type | Default | Description |
|
|
39
|
+
| :----------- | :---------- | :------ | :-------------------------------------------------------- |
|
|
40
|
+
| `parseHtml` | **boolean** | `true` | Return the DOM root as an HTMLElement (node-html-parsed). |
|
|
41
|
+
| `verbose` | **boolean** | `false` | Output HTTP connection debug messages. |
|
|
42
42
|
|
|
43
43
|
### `startWebServer()` Options
|
|
44
44
|
| Name (key) | Type | Default| Description |
|
|
@@ -62,7 +62,7 @@ type Web = {
|
|
|
62
62
|
location: Location,
|
|
63
63
|
title: string,
|
|
64
64
|
html: string,
|
|
65
|
-
|
|
65
|
+
root: HTMLElement | null, //see node-html-parsed library
|
|
66
66
|
};
|
|
67
67
|
```
|
|
68
68
|
|
|
@@ -91,7 +91,7 @@ const handleResponse = (web) => {
|
|
|
91
91
|
console.log('Hello, World!');
|
|
92
92
|
console.log('web fields:', Object.keys(web).join(', '));
|
|
93
93
|
console.log(`The HTML from ${web.location.href} is ${web.html.length} characters`,
|
|
94
|
-
`long and contains ${web
|
|
94
|
+
`long and contains ${web.root.querySelectorAll('p').length} <p> tags.`);
|
|
95
95
|
return web;
|
|
96
96
|
};
|
|
97
97
|
puppeteer.launch()
|
|
@@ -102,7 +102,7 @@ puppeteer.launch()
|
|
|
102
102
|
**Output:**
|
|
103
103
|
```
|
|
104
104
|
Hello, World!
|
|
105
|
-
web fields: browser, page, response, title, html,
|
|
105
|
+
web fields: browser, page, response, status, location, title, html, root
|
|
106
106
|
The HTML from https://pretty-print-json.js.org/ is 8200 characters
|
|
107
107
|
long and contains 7 <p> tags.
|
|
108
108
|
```
|
|
@@ -119,7 +119,7 @@ import { browserReady } from 'puppeteer-browser-ready';
|
|
|
119
119
|
|
|
120
120
|
// Setup
|
|
121
121
|
const url = 'https://pretty-print-json.js.org/';
|
|
122
|
-
let web; //fields: browser, page, response, status, location, title, html,
|
|
122
|
+
let web; //fields: browser, page, response, status, location, title, html, root
|
|
123
123
|
const loadWebPage = async () =>
|
|
124
124
|
web = await puppeteer.launch().then(browserReady.goto(url));
|
|
125
125
|
const closeWebPage = async () =>
|
|
@@ -137,7 +137,7 @@ describe('The web page', () => {
|
|
|
137
137
|
});
|
|
138
138
|
|
|
139
139
|
it('has exactly one header, main, and footer', () => {
|
|
140
|
-
const actual =
|
|
140
|
+
const actual = getTags('body >*');
|
|
141
141
|
const expected = ['header', 'main', 'footer'];
|
|
142
142
|
assertDeepStrictEqual(actual, expected);
|
|
143
143
|
});
|
|
@@ -211,7 +211,7 @@ Example configuration in **package.json** to allow 5,000 ms:
|
|
|
211
211
|
```json
|
|
212
212
|
"scripts": {
|
|
213
213
|
"pretest": "run-scripts clean build",
|
|
214
|
-
"test": "mocha spec/*.spec.js --timeout
|
|
214
|
+
"test": "mocha spec/*.spec.js --timeout 7000"
|
|
215
215
|
},
|
|
216
216
|
```
|
|
217
217
|
|
|
@@ -222,6 +222,7 @@ Example configuration in **package.json** to allow 5,000 ms:
|
|
|
222
222
|
- 🎋 [add-dist-header](https://github.com/center-key/add-dist-header): _Prepend a one-line banner comment (with license notice) to distribution files_
|
|
223
223
|
- 📄 [copy-file-util](https://github.com/center-key/copy-file-util): _Copy or rename a file with optional package version number_
|
|
224
224
|
- 📂 [copy-folder-util](https://github.com/center-key/copy-folder-util): _Recursively copy files from one folder to another folder_
|
|
225
|
+
- 🪺 [recursive-exec](https://github.com/center-key/recursive-exec): _Run a command on each file in a folder and its subfolders_
|
|
225
226
|
- 🔍 [replacer-util](https://github.com/center-key/replacer-util): _Find and replace strings or template outputs in text files_
|
|
226
227
|
- 🔢 [rev-web-assets](https://github.com/center-key/rev-web-assets): _Revision web asset filenames with cache busting content hash fingerprints_
|
|
227
228
|
- 🚆 [run-scripts-util](https://github.com/center-key/run-scripts-util): _Organize npm scripts into named groups of easy to manage commands_
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
//! puppeteer-browser-ready v1.2.
|
|
1
|
+
//! puppeteer-browser-ready v1.2.3 ~~ 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';
|
|
5
|
+
import { HTMLElement } from 'node-html-parser';
|
|
5
6
|
import { Server } from 'http';
|
|
6
7
|
import { SuiteFunction } from 'mocha';
|
|
7
8
|
import httpTerminator from 'http-terminator';
|
|
@@ -29,9 +30,11 @@ export type Web = {
|
|
|
29
30
|
title: string | null;
|
|
30
31
|
html: string | null;
|
|
31
32
|
$: cheerio.Root | null;
|
|
33
|
+
root: HTMLElement | null;
|
|
32
34
|
};
|
|
33
35
|
export type BrowserReadySettings = {
|
|
34
36
|
addCheerio: boolean;
|
|
37
|
+
parseHtml: boolean;
|
|
35
38
|
verbose: boolean;
|
|
36
39
|
};
|
|
37
40
|
export type BrowserReadyOptions = Partial<BrowserReadySettings>;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
//! puppeteer-browser-ready v1.2.
|
|
1
|
+
//! puppeteer-browser-ready v1.2.3 ~~ https://github.com/center-key/puppeteer-browser-ready ~~ MIT License
|
|
2
2
|
|
|
3
|
+
import { parse } from 'node-html-parser';
|
|
3
4
|
import cheerio from 'cheerio';
|
|
4
5
|
import express from 'express';
|
|
5
6
|
import httpTerminator from 'http-terminator';
|
|
@@ -42,10 +43,13 @@ const browserReady = {
|
|
|
42
43
|
return http.terminator.terminate();
|
|
43
44
|
},
|
|
44
45
|
goto(url, options) {
|
|
45
|
-
const defaults = { addCheerio: true, verbose: false };
|
|
46
|
+
const defaults = { addCheerio: false, parseHtml: true, verbose: false };
|
|
46
47
|
const settings = { ...defaults, ...options };
|
|
48
|
+
if (settings.addCheerio)
|
|
49
|
+
console.log('puppeteer-browser-ready: Option "addCheerio" is deprecated, use "parseHtml" instead.');
|
|
47
50
|
const log = (label, msg) => settings.verbose &&
|
|
48
51
|
console.log(' ', Date.now() % 100000, label + ':', msg);
|
|
52
|
+
const rootInfo = (root) => root.constructor.name + '/' + root.firstChild.toString();
|
|
49
53
|
const web = async (browser) => {
|
|
50
54
|
log('Connected', browser.isConnected());
|
|
51
55
|
try {
|
|
@@ -61,9 +65,10 @@ const browserReady = {
|
|
|
61
65
|
log('Title', title);
|
|
62
66
|
const html = response && await response.text();
|
|
63
67
|
log('Bytes', html?.length);
|
|
64
|
-
const $ = html && settings.addCheerio ? cheerio.load(html) : null;
|
|
65
|
-
|
|
66
|
-
|
|
68
|
+
const $ = html && settings.addCheerio ? cheerio.load(html) : null; //deprecated
|
|
69
|
+
const root = html && settings.parseHtml ? parse(html) : null;
|
|
70
|
+
log('DOM root', root ? rootInfo(root) : null);
|
|
71
|
+
return { browser, page, response, status, location, title, html, $, root };
|
|
67
72
|
}
|
|
68
73
|
catch (error) {
|
|
69
74
|
const status = browser.isConnected() ? 'connected' : 'not connected';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "puppeteer-browser-ready",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.3",
|
|
4
4
|
"description": "Simple utility to go to a URL and wait for the HTTP response",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -67,17 +67,19 @@
|
|
|
67
67
|
"clean": [
|
|
68
68
|
"rimraf build dist"
|
|
69
69
|
],
|
|
70
|
-
"
|
|
70
|
+
"lint": [
|
|
71
71
|
"jshint . --exclude-path .gitignore",
|
|
72
|
-
"eslint --max-warnings 0 . --ext .ts"
|
|
72
|
+
"eslint --max-warnings 0 . --ext .ts"
|
|
73
|
+
],
|
|
74
|
+
"build": [
|
|
73
75
|
"tsc",
|
|
74
76
|
"add-dist-header build dist",
|
|
75
77
|
"html-validator"
|
|
76
78
|
]
|
|
77
79
|
},
|
|
78
80
|
"scripts": {
|
|
79
|
-
"pretest": "run-scripts clean build",
|
|
80
|
-
"test": "mocha spec/*.spec.js --timeout
|
|
81
|
+
"pretest": "run-scripts clean lint build",
|
|
82
|
+
"test": "mocha spec/*.spec.js --timeout 7000 --retries 1"
|
|
81
83
|
},
|
|
82
84
|
"peerDependencies": {
|
|
83
85
|
"puppeteer": "^15 || ^16 || ^17 || ^18 || ^19.7 || ^20 || ^21"
|
|
@@ -85,28 +87,29 @@
|
|
|
85
87
|
"dependencies": {
|
|
86
88
|
"cheerio": "~1.0.0-rc.12",
|
|
87
89
|
"express": "~4.18",
|
|
88
|
-
"http-terminator": "~3.2"
|
|
90
|
+
"http-terminator": "~3.2",
|
|
91
|
+
"node-html-parser": "~6.1"
|
|
89
92
|
},
|
|
90
93
|
"devDependencies": {
|
|
91
94
|
"@types/cheerio": "~0.22",
|
|
92
95
|
"@types/express": "~4.17",
|
|
93
96
|
"@types/mocha": "~10.0",
|
|
94
|
-
"@types/node": "~20.
|
|
97
|
+
"@types/node": "~20.6",
|
|
95
98
|
"@types/ws": "~8.5",
|
|
96
|
-
"@typescript-eslint/eslint-plugin": "~6.
|
|
97
|
-
"@typescript-eslint/parser": "~6.
|
|
98
|
-
"add-dist-header": "~1.
|
|
99
|
+
"@typescript-eslint/eslint-plugin": "~6.7",
|
|
100
|
+
"@typescript-eslint/parser": "~6.7",
|
|
101
|
+
"add-dist-header": "~1.3",
|
|
99
102
|
"assert-deep-strict-equal": "~1.1",
|
|
100
103
|
"copy-file-util": "~1.1",
|
|
101
104
|
"copy-folder-util": "~1.1",
|
|
102
|
-
"eslint": "~8.
|
|
105
|
+
"eslint": "~8.49",
|
|
103
106
|
"jshint": "~2.13",
|
|
104
107
|
"mocha": "~10.2",
|
|
105
108
|
"open": "~9.1",
|
|
106
|
-
"puppeteer": "~21.
|
|
109
|
+
"puppeteer": "~21.2",
|
|
107
110
|
"rimraf": "~5.0",
|
|
108
111
|
"run-scripts-util": "~1.2",
|
|
109
|
-
"typescript": "~5.
|
|
112
|
+
"typescript": "~5.2",
|
|
110
113
|
"w3c-html-validator": "~1.4"
|
|
111
114
|
}
|
|
112
115
|
}
|