@guanzhu.me/pw-cli 0.0.11 → 0.0.12
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 +49 -2
- package/bin/pw-cli.js +27 -2
- package/package.json +1 -1
- package/src/cli.js +28 -3
package/README.md
CHANGED
|
@@ -74,6 +74,52 @@ Run a local script:
|
|
|
74
74
|
pw-cli run-script ./scrape.js --url https://example.com
|
|
75
75
|
```
|
|
76
76
|
|
|
77
|
+
`run-script` is intended for multi-step automation. Your script can use:
|
|
78
|
+
|
|
79
|
+
- Playwright globals: `page`, `context`, `browser`, `playwright`
|
|
80
|
+
- Script args: `args`
|
|
81
|
+
- CommonJS globals: `require`, `module`, `exports`, `__filename`, `__dirname`
|
|
82
|
+
|
|
83
|
+
More complete example:
|
|
84
|
+
|
|
85
|
+
```javascript
|
|
86
|
+
// scripts/extract-links.js
|
|
87
|
+
const fs = require('fs');
|
|
88
|
+
|
|
89
|
+
const url = args[args.indexOf('--url') + 1] || 'https://example.com';
|
|
90
|
+
const output = args[args.indexOf('--output') + 1] || 'links.json';
|
|
91
|
+
|
|
92
|
+
await page.goto(url, { waitUntil: 'networkidle' });
|
|
93
|
+
|
|
94
|
+
const links = await page.locator('a').evaluateAll(nodes =>
|
|
95
|
+
nodes
|
|
96
|
+
.map(a => ({
|
|
97
|
+
text: a.textContent.trim(),
|
|
98
|
+
href: a.href,
|
|
99
|
+
}))
|
|
100
|
+
.filter(item => item.href)
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
fs.writeFileSync(
|
|
104
|
+
output,
|
|
105
|
+
JSON.stringify(
|
|
106
|
+
{
|
|
107
|
+
url,
|
|
108
|
+
count: links.length,
|
|
109
|
+
links,
|
|
110
|
+
},
|
|
111
|
+
null,
|
|
112
|
+
2
|
|
113
|
+
)
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
return `saved ${links.length} links to ${output}`;
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
```bash
|
|
120
|
+
pw-cli run-script ./scripts/extract-links.js --url https://example.com --output links.json
|
|
121
|
+
```
|
|
122
|
+
|
|
77
123
|
Reuse a page that was opened through `pw-cli open`:
|
|
78
124
|
|
|
79
125
|
```bash
|
|
@@ -108,7 +154,7 @@ pw-cli list
|
|
|
108
154
|
- `open` injects headed and persistent defaults
|
|
109
155
|
- Browser-backed commands can auto-open a browser session if needed
|
|
110
156
|
- `run-code` accepts stdin and plain inline statements
|
|
111
|
-
- `run-script` executes a local `.js` file with Playwright globals
|
|
157
|
+
- `run-script` executes a local `.js` file with Playwright globals, CommonJS globals, and `args`
|
|
112
158
|
- Common element commands accept XPath refs
|
|
113
159
|
- `queue` lets you batch multiple commands and run them in order
|
|
114
160
|
|
|
@@ -235,7 +281,7 @@ console [min-level] list console messages
|
|
|
235
281
|
run-code <code> run playwright code snippet
|
|
236
282
|
pw-cli: reads code from stdin when <code> is omitted
|
|
237
283
|
pw-cli: wraps plain statements in an async function
|
|
238
|
-
run-script <file> [...] run a local JavaScript file with
|
|
284
|
+
run-script <file> [...] run a local JavaScript file with Playwright globals and script args
|
|
239
285
|
network list all network requests since loading the page
|
|
240
286
|
tracing-start start trace recording
|
|
241
287
|
tracing-stop stop trace recording
|
|
@@ -289,6 +335,7 @@ pw-cli open https://example.com
|
|
|
289
335
|
pw-cli run-code "await page.goto('https://example.com'); return await page.title()"
|
|
290
336
|
echo "return await page.url()" | pw-cli run-code
|
|
291
337
|
pw-cli run-script ./scripts/smoke.js --env prod
|
|
338
|
+
pw-cli run-script ./scripts/extract-links.js --url https://example.com --output links.json
|
|
292
339
|
pw-cli click "//button[contains(., 'Submit')]"
|
|
293
340
|
pw-cli queue add goto https://example.com
|
|
294
341
|
pw-cli queue add snapshot
|
package/bin/pw-cli.js
CHANGED
|
@@ -229,7 +229,7 @@ DevTools:
|
|
|
229
229
|
run-code <code> run playwright code snippet
|
|
230
230
|
pw-cli: reads code from stdin when <code> is omitted
|
|
231
231
|
pw-cli: wraps plain statements in an async function
|
|
232
|
-
run-script <file> [...] run a local JavaScript file with
|
|
232
|
+
run-script <file> [...] run a local JavaScript file with Playwright globals and script args
|
|
233
233
|
network list all network requests since loading the page
|
|
234
234
|
tracing-start start trace recording
|
|
235
235
|
tracing-stop stop trace recording
|
|
@@ -271,10 +271,23 @@ Examples:
|
|
|
271
271
|
pw-cli run-code "await page.goto('https://example.com'); return await page.title()"
|
|
272
272
|
echo "return await page.url()" | pw-cli run-code
|
|
273
273
|
pw-cli run-script .\\scripts\\smoke.js --env prod
|
|
274
|
+
pw-cli run-script .\\scripts\\extract-links.js --url https://example.com --output links.json
|
|
274
275
|
pw-cli click "//button[contains(., 'Submit')]"
|
|
275
276
|
pw-cli queue add goto https://example.com
|
|
276
277
|
pw-cli queue add snapshot
|
|
277
278
|
pw-cli queue run
|
|
279
|
+
|
|
280
|
+
run-script example:
|
|
281
|
+
// scripts/extract-links.js
|
|
282
|
+
const fs = require('fs');
|
|
283
|
+
const url = args[args.indexOf('--url') + 1] || 'https://example.com';
|
|
284
|
+
const output = args[args.indexOf('--output') + 1] || 'links.json';
|
|
285
|
+
await page.goto(url, { waitUntil: 'networkidle' });
|
|
286
|
+
const links = await page.locator('a').evaluateAll(nodes =>
|
|
287
|
+
nodes.map(a => ({ text: a.textContent.trim(), href: a.href })).filter(x => x.href)
|
|
288
|
+
);
|
|
289
|
+
fs.writeFileSync(output, JSON.stringify({ url, count: links.length, links }, null, 2));
|
|
290
|
+
return \`saved \${links.length} links to \${output}\`;
|
|
278
291
|
`.trim() + '\n');
|
|
279
292
|
}
|
|
280
293
|
|
|
@@ -631,7 +644,19 @@ async function handleRunScript(rawArgv) {
|
|
|
631
644
|
const [scriptPath, ...scriptArgs] = positionals;
|
|
632
645
|
|
|
633
646
|
if (!scriptPath) {
|
|
634
|
-
process.stderr.write(
|
|
647
|
+
process.stderr.write(`pw-cli: run-script requires a script path
|
|
648
|
+
|
|
649
|
+
Usage:
|
|
650
|
+
pw-cli run-script <file.js> [args...]
|
|
651
|
+
|
|
652
|
+
What the script receives:
|
|
653
|
+
- Playwright globals: page, context, browser, playwright
|
|
654
|
+
- Script args array: args
|
|
655
|
+
- CommonJS globals: require, module, exports, __filename, __dirname
|
|
656
|
+
|
|
657
|
+
Example:
|
|
658
|
+
pw-cli run-script ./scripts/extract-links.js --url https://example.com --output links.json
|
|
659
|
+
`);
|
|
635
660
|
process.exit(1);
|
|
636
661
|
}
|
|
637
662
|
if (!fs.existsSync(path.resolve(scriptPath))) {
|
package/package.json
CHANGED
package/src/cli.js
CHANGED
|
@@ -54,7 +54,18 @@ async function cmdRunCode(rest, opts) {
|
|
|
54
54
|
async function cmdRunScript(rest, opts) {
|
|
55
55
|
const [scriptPath, ...scriptArgs] = rest;
|
|
56
56
|
if (!scriptPath) {
|
|
57
|
-
die(
|
|
57
|
+
die(`No script path provided.
|
|
58
|
+
|
|
59
|
+
Usage:
|
|
60
|
+
pw-cli run-script <file.js> [args...]
|
|
61
|
+
|
|
62
|
+
What the script receives:
|
|
63
|
+
- Playwright globals: page, context, browser, playwright
|
|
64
|
+
- Script args array: args
|
|
65
|
+
- CommonJS globals: require, module, exports, __filename, __dirname
|
|
66
|
+
|
|
67
|
+
Example:
|
|
68
|
+
pw-cli run-script ./scripts/extract-links.js --url https://example.com --output links.json`);
|
|
58
69
|
}
|
|
59
70
|
|
|
60
71
|
const conn = await getConnection(opts);
|
|
@@ -105,20 +116,34 @@ GLOBAL OPTIONS
|
|
|
105
116
|
|
|
106
117
|
COMMANDS
|
|
107
118
|
run-code [code] Execute inline JS (reads stdin if omitted)
|
|
108
|
-
run-script <file> [...] Execute a .js
|
|
119
|
+
run-script <file> [...] Execute a local .js file with Playwright globals and script args
|
|
109
120
|
kill Stop the running browser
|
|
110
121
|
status Show browser status
|
|
111
122
|
|
|
112
123
|
SCRIPT GLOBALS
|
|
113
|
-
page, context, browser, playwright, args
|
|
124
|
+
page, context, browser, playwright, args
|
|
125
|
+
require, module, exports, __filename, __dirname
|
|
114
126
|
|
|
115
127
|
EXAMPLES
|
|
116
128
|
pw-cli run-code "await page.goto('https://example.com'); console.log(await page.title())"
|
|
117
129
|
echo "await page.screenshot({ path: 'out.png' })" | pw-cli run-code
|
|
118
130
|
pw-cli run-script ./scrape.js --url https://example.com
|
|
131
|
+
pw-cli run-script ./scripts/extract-links.js --url https://example.com --output links.json
|
|
119
132
|
pw-cli --headless run-code "await page.goto('https://example.com')"
|
|
120
133
|
pw-cli --profile work status
|
|
121
134
|
pw-cli kill
|
|
135
|
+
|
|
136
|
+
RUN-SCRIPT EXAMPLE
|
|
137
|
+
// scripts/extract-links.js
|
|
138
|
+
const fs = require('fs');
|
|
139
|
+
const url = args[args.indexOf('--url') + 1] || 'https://example.com';
|
|
140
|
+
const output = args[args.indexOf('--output') + 1] || 'links.json';
|
|
141
|
+
await page.goto(url, { waitUntil: 'networkidle' });
|
|
142
|
+
const links = await page.locator('a').evaluateAll(nodes =>
|
|
143
|
+
nodes.map(a => ({ text: a.textContent.trim(), href: a.href })).filter(x => x.href)
|
|
144
|
+
);
|
|
145
|
+
fs.writeFileSync(output, JSON.stringify({ url, count: links.length, links }, null, 2));
|
|
146
|
+
return \`saved \${links.length} links to \${output}\`;
|
|
122
147
|
`.trim());
|
|
123
148
|
}
|
|
124
149
|
|