playwright-harness 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +54 -2
- package/harness.js +408 -0
- package/index.html +411 -0
- package/package.json +24 -5
- package/server.mjs +96 -0
- package/specs.mjs +37 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 artesgo
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,55 @@
|
|
|
1
|
-
#
|
|
1
|
+
# playwright-harness
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A framework-agnostic Playwright test harness. It shows your running app in an
|
|
4
|
+
iframe, lets you click elements to build Playwright steps, and writes the test
|
|
5
|
+
code for you.
|
|
6
|
+
|
|
7
|
+
The harness serves its own UI and proxies your dev server, so the app iframe is
|
|
8
|
+
same-origin and can be inspected — no framework integration, no app changes, no
|
|
9
|
+
dependencies.
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
Start your app's dev server, then:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npx playwright-harness --app http://localhost:5173
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Open <http://localhost:5180/harness>.
|
|
20
|
+
|
|
21
|
+
| Option | Default | Meaning |
|
|
22
|
+
| --- | --- | --- |
|
|
23
|
+
| `--app` | `http://localhost:5173` | Your running dev server |
|
|
24
|
+
| `--port` | `5180` | Port for the harness and the proxy |
|
|
25
|
+
| `--tests` | `tests` | Directory searched (recursively) for `*.spec.ts` |
|
|
26
|
+
|
|
27
|
+
The iframe path is relative (`/tab`, not a full URL) because the proxy keeps the
|
|
28
|
+
app on the harness origin.
|
|
29
|
+
|
|
30
|
+
## What it does
|
|
31
|
+
|
|
32
|
+
- **Pick element** — click inside the app and the selector is filled in. It
|
|
33
|
+
prefers the nearest `data-testid`, then `id`, then `name`, then a CSS path.
|
|
34
|
+
- **Selector typeahead** — lists every `data-testid` in the current page and
|
|
35
|
+
filters as you type.
|
|
36
|
+
- **Test IDs** — outlines every `data-testid` element, stepping through a colour
|
|
37
|
+
ramp per nesting level so overlaps stay readable.
|
|
38
|
+
- **Steps** — `goto`, `click`, `fill`, `press`, `expect visible`, `expect text`,
|
|
39
|
+
page and element screenshots, shown as readable summaries.
|
|
40
|
+
- **Code** — the generated Playwright test, importable as `saveScreenshot` /
|
|
41
|
+
`saveElementScreenshot` from `./utils/screenshot`.
|
|
42
|
+
- **Tests** — every `*.spec.ts` under the tests directory, with its test titles
|
|
43
|
+
and source.
|
|
44
|
+
|
|
45
|
+
## Notes
|
|
46
|
+
|
|
47
|
+
- The generated code imports the screenshot helpers from `./utils/screenshot`,
|
|
48
|
+
which assumes the spec lives at the tests root.
|
|
49
|
+
- The proxy forwards HTTP only. The app's HMR websocket will not connect through
|
|
50
|
+
the harness, so edits reload only when you press **Reload**.
|
|
51
|
+
- Requires Node 18+.
|
|
52
|
+
|
|
53
|
+
## License
|
|
54
|
+
|
|
55
|
+
MIT
|
package/harness.js
ADDED
|
@@ -0,0 +1,408 @@
|
|
|
1
|
+
const $ = (id) => document.getElementById(id);
|
|
2
|
+
|
|
3
|
+
const OVERLAY_ID = 'harness-testid-overlay';
|
|
4
|
+
|
|
5
|
+
const app = $('app');
|
|
6
|
+
const stepsEl = $('steps');
|
|
7
|
+
const stepsHint = $('steps-hint');
|
|
8
|
+
const suggestionsEl = $('suggestions');
|
|
9
|
+
const stepError = $('step-error');
|
|
10
|
+
const addStepButton = $('add-step');
|
|
11
|
+
const clearButton = $('clear-steps');
|
|
12
|
+
const copyButton = $('copy');
|
|
13
|
+
|
|
14
|
+
let steps = [];
|
|
15
|
+
let picking = false;
|
|
16
|
+
let showTestIds = false;
|
|
17
|
+
let copied = false;
|
|
18
|
+
let testIdSelectors = [];
|
|
19
|
+
|
|
20
|
+
const action = () => $('action').value;
|
|
21
|
+
const selector = () => $('selector').value.trim();
|
|
22
|
+
const value = () => $('value').value.trim();
|
|
23
|
+
const shotPath = () => $('shot-path').value.trim();
|
|
24
|
+
|
|
25
|
+
const needsSelector = () => !['goto', 'screenshotPage'].includes(action());
|
|
26
|
+
const needsValue = () => ['fill', 'press', 'expectText', 'goto'].includes(action());
|
|
27
|
+
const needsPath = () => ['screenshotPage', 'screenshotElement'].includes(action());
|
|
28
|
+
|
|
29
|
+
function quote(text) {
|
|
30
|
+
return `'${text.replaceAll('\\', '\\\\').replaceAll("'", "\\'")}'`;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function stepCode(step) {
|
|
34
|
+
const target = `page.locator(${quote(step.selector)})`;
|
|
35
|
+
switch (step.action) {
|
|
36
|
+
case 'goto':
|
|
37
|
+
return `await page.goto(${quote(step.value)});`;
|
|
38
|
+
case 'click':
|
|
39
|
+
return `await ${target}.click();`;
|
|
40
|
+
case 'fill':
|
|
41
|
+
return `await ${target}.fill(${quote(step.value)});`;
|
|
42
|
+
case 'press':
|
|
43
|
+
return `await ${target}.press(${quote(step.value)});`;
|
|
44
|
+
case 'expectVisible':
|
|
45
|
+
return `await expect(${target}).toBeVisible();`;
|
|
46
|
+
case 'expectText':
|
|
47
|
+
return `await expect(${target}).toContainText(${quote(step.value)});`;
|
|
48
|
+
case 'screenshotPage':
|
|
49
|
+
return `await saveScreenshot(page, ${quote(step.path)});`;
|
|
50
|
+
case 'screenshotElement':
|
|
51
|
+
return `await saveElementScreenshot(page, ${quote(step.selector)}, ${quote(step.path)});`;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function describeStep(step) {
|
|
56
|
+
switch (step.action) {
|
|
57
|
+
case 'goto':
|
|
58
|
+
return { label: 'goto', detail: step.value };
|
|
59
|
+
case 'click':
|
|
60
|
+
return { label: 'click', detail: step.selector };
|
|
61
|
+
case 'fill':
|
|
62
|
+
return { label: 'fill', detail: `${step.selector} = ${step.value}` };
|
|
63
|
+
case 'press':
|
|
64
|
+
return { label: 'press', detail: `${step.value} on ${step.selector}` };
|
|
65
|
+
case 'expectVisible':
|
|
66
|
+
return { label: 'visible', detail: step.selector };
|
|
67
|
+
case 'expectText':
|
|
68
|
+
return { label: 'expect text', detail: `${step.value} in ${step.selector}` };
|
|
69
|
+
case 'screenshotPage':
|
|
70
|
+
return { label: 'screenshot page', detail: `to ${step.path}` };
|
|
71
|
+
case 'screenshotElement':
|
|
72
|
+
return { label: 'screenshot element', detail: `${step.selector} to ${step.path}` };
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function generatedCode() {
|
|
77
|
+
const pageShot = steps.some((step) => step.action === 'screenshotPage');
|
|
78
|
+
const elementShot = steps.some((step) => step.action === 'screenshotElement');
|
|
79
|
+
const shots = [pageShot && 'saveScreenshot', elementShot && 'saveElementScreenshot'].filter(
|
|
80
|
+
Boolean
|
|
81
|
+
);
|
|
82
|
+
const imports = [
|
|
83
|
+
`import { expect, test } from '@playwright/test';`,
|
|
84
|
+
...(shots.length ? [`import { ${shots.join(', ')} } from './utils/screenshot';`] : []),
|
|
85
|
+
''
|
|
86
|
+
];
|
|
87
|
+
|
|
88
|
+
if (!steps.length)
|
|
89
|
+
return [...imports, "test('harness test', async ({ page }) => {});", ''].join('\n');
|
|
90
|
+
|
|
91
|
+
return [
|
|
92
|
+
...imports,
|
|
93
|
+
`test('harness test', async ({ page }) => {`,
|
|
94
|
+
...steps.map((step) => `\t${stepCode(step)}`),
|
|
95
|
+
'});',
|
|
96
|
+
''
|
|
97
|
+
].join('\n');
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function errorMessage() {
|
|
101
|
+
if (action() === 'goto' && !value()) return 'Enter a URL path.';
|
|
102
|
+
if (needsSelector() && !selector()) return 'Pick an element or enter a selector.';
|
|
103
|
+
if (needsValue() && !value()) return 'Enter a value.';
|
|
104
|
+
if (needsPath() && !shotPath()) return 'Enter a screenshot path.';
|
|
105
|
+
return '';
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const VALUE_LABELS = { goto: 'Path', press: 'Key', expectText: 'Text' };
|
|
109
|
+
|
|
110
|
+
function renderFields() {
|
|
111
|
+
$('selector-field').hidden = !needsSelector();
|
|
112
|
+
$('value-field').hidden = !needsValue();
|
|
113
|
+
$('path-field').hidden = !needsPath();
|
|
114
|
+
$('value-label').textContent = VALUE_LABELS[action()] ?? 'Value';
|
|
115
|
+
$('value').placeholder = action() === 'goto' ? '/profile' : '';
|
|
116
|
+
|
|
117
|
+
const message = errorMessage();
|
|
118
|
+
stepError.textContent = message;
|
|
119
|
+
stepError.hidden = !message;
|
|
120
|
+
addStepButton.disabled = !!message;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function renderSteps() {
|
|
124
|
+
stepsEl.innerHTML = steps
|
|
125
|
+
.map((step, index) => {
|
|
126
|
+
const info = describeStep(step);
|
|
127
|
+
return `<li><span class="step-text"><strong>${escapeHtml(info.label)}</strong><span class="step-detail">${escapeHtml(info.detail)}</span></span><button class="icon" data-index="${index}" aria-label="Remove step">×</button></li>`;
|
|
128
|
+
})
|
|
129
|
+
.join('');
|
|
130
|
+
stepsEl.hidden = !steps.length;
|
|
131
|
+
stepsHint.hidden = !!steps.length;
|
|
132
|
+
clearButton.disabled = !steps.length;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function renderCode() {
|
|
136
|
+
$('code').textContent = generatedCode();
|
|
137
|
+
copyButton.textContent = copied ? 'Copied' : 'Copy';
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function addStep() {
|
|
141
|
+
if (errorMessage()) return;
|
|
142
|
+
steps = [...steps, { action: action(), selector: selector(), value: value(), path: shotPath() }];
|
|
143
|
+
renderSteps();
|
|
144
|
+
renderCode();
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function removeStep(index) {
|
|
148
|
+
steps = steps.filter((_, i) => i !== index);
|
|
149
|
+
renderSteps();
|
|
150
|
+
renderCode();
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function setTab(name) {
|
|
154
|
+
for (const button of document.querySelectorAll('[data-tab]')) {
|
|
155
|
+
button.classList.toggle('active', button.dataset.tab === name);
|
|
156
|
+
}
|
|
157
|
+
$('actions-panel').hidden = name !== 'actions';
|
|
158
|
+
$('code-panel').hidden = name !== 'code';
|
|
159
|
+
$('tests-panel').hidden = name !== 'tests';
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function loadPath() {
|
|
163
|
+
app.src = $('app-path').value.trim() || '/';
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function iframeDoc() {
|
|
167
|
+
try {
|
|
168
|
+
return app.contentDocument;
|
|
169
|
+
} catch {
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function refreshTestIds() {
|
|
175
|
+
const doc = iframeDoc();
|
|
176
|
+
if (!doc) return;
|
|
177
|
+
const ids = [...doc.querySelectorAll('[data-testid]')]
|
|
178
|
+
.map((element) => element.getAttribute('data-testid'))
|
|
179
|
+
.filter(Boolean);
|
|
180
|
+
testIdSelectors = [...new Set(ids)].sort().map((id) => `[data-testid="${cssString(id)}"]`);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function openSuggestions() {
|
|
184
|
+
refreshTestIds();
|
|
185
|
+
const query = selector().toLowerCase();
|
|
186
|
+
const matches = testIdSelectors.filter((item) => item.toLowerCase().includes(query)).slice(0, 30);
|
|
187
|
+
|
|
188
|
+
suggestionsEl.innerHTML = matches
|
|
189
|
+
.map((item) => `<li><button type="button">${escapeHtml(item)}</button></li>`)
|
|
190
|
+
.join('');
|
|
191
|
+
suggestionsEl.hidden = !matches.length;
|
|
192
|
+
$('selector').setAttribute('aria-expanded', String(!!matches.length));
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function closeSuggestions() {
|
|
196
|
+
suggestionsEl.hidden = true;
|
|
197
|
+
$('selector').setAttribute('aria-expanded', 'false');
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function togglePick() {
|
|
201
|
+
picking = !picking;
|
|
202
|
+
$('pick').textContent = picking ? 'Click an element…' : 'Pick element';
|
|
203
|
+
$('pick').classList.toggle('active', picking);
|
|
204
|
+
$('pick-inline').classList.toggle('active', picking);
|
|
205
|
+
const doc = iframeDoc();
|
|
206
|
+
if (doc?.body) doc.body.style.cursor = picking ? 'crosshair' : '';
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function onPick(event) {
|
|
210
|
+
if (!picking) return;
|
|
211
|
+
event.preventDefault();
|
|
212
|
+
event.stopImmediatePropagation();
|
|
213
|
+
$('selector').value = buildSelector(event.target);
|
|
214
|
+
togglePick();
|
|
215
|
+
renderFields();
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function toggleTestIds() {
|
|
219
|
+
showTestIds = !showTestIds;
|
|
220
|
+
$('toggle-testids').classList.toggle('active', showTestIds);
|
|
221
|
+
applyTestIdOverlay();
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function applyTestIdOverlay() {
|
|
225
|
+
const doc = iframeDoc();
|
|
226
|
+
if (!doc) return;
|
|
227
|
+
const existing = doc.getElementById(OVERLAY_ID);
|
|
228
|
+
if (!showTestIds) {
|
|
229
|
+
existing?.remove();
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
if (existing) return;
|
|
233
|
+
const style = doc.createElement('style');
|
|
234
|
+
style.id = OVERLAY_ID;
|
|
235
|
+
// shortcut: position: relative is forced on the host to anchor the label; drop the label if it ever breaks layout
|
|
236
|
+
// nested testids step through a colour ramp so overlapping outlines stay distinguishable
|
|
237
|
+
style.textContent = `
|
|
238
|
+
[data-testid] {
|
|
239
|
+
--testid-colour: #ff00ff;
|
|
240
|
+
position: relative;
|
|
241
|
+
outline: 2px dashed var(--testid-colour) !important;
|
|
242
|
+
outline-offset: -2px !important;
|
|
243
|
+
}
|
|
244
|
+
[data-testid] [data-testid] {
|
|
245
|
+
--testid-colour: #ffd400;
|
|
246
|
+
}
|
|
247
|
+
[data-testid] [data-testid] [data-testid] {
|
|
248
|
+
--testid-colour: #00e5ff;
|
|
249
|
+
}
|
|
250
|
+
[data-testid] [data-testid] [data-testid] [data-testid] {
|
|
251
|
+
--testid-colour: #7cff4f;
|
|
252
|
+
}
|
|
253
|
+
[data-testid] [data-testid] [data-testid] [data-testid] [data-testid] {
|
|
254
|
+
--testid-colour: #ff7a00;
|
|
255
|
+
}
|
|
256
|
+
[data-testid]::after {
|
|
257
|
+
content: attr(data-testid);
|
|
258
|
+
position: absolute;
|
|
259
|
+
top: 0;
|
|
260
|
+
left: 0;
|
|
261
|
+
transform: translateY(-100%);
|
|
262
|
+
padding: 0 4px;
|
|
263
|
+
border-radius: 2px 2px 0 0;
|
|
264
|
+
background: var(--testid-colour);
|
|
265
|
+
color: #111;
|
|
266
|
+
font: 10px/1.4 ui-monospace, monospace;
|
|
267
|
+
white-space: nowrap;
|
|
268
|
+
pointer-events: none;
|
|
269
|
+
z-index: 2147483647;
|
|
270
|
+
}
|
|
271
|
+
`;
|
|
272
|
+
doc.head.append(style);
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
function cssIdent(text) {
|
|
276
|
+
return typeof CSS !== 'undefined' && CSS.escape ? CSS.escape(text) : text;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function cssString(text) {
|
|
280
|
+
return text.replaceAll('\\', '\\\\').replaceAll('"', '\\"');
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function buildSelector(element) {
|
|
284
|
+
// data-testid wins over id so picked selectors match the ones the typeahead offers
|
|
285
|
+
const testIdHost = element.closest('[data-testid]');
|
|
286
|
+
const testid = testIdHost?.getAttribute('data-testid');
|
|
287
|
+
if (testid) return `[data-testid="${cssString(testid)}"]`;
|
|
288
|
+
if (element.id) return `#${cssIdent(element.id)}`;
|
|
289
|
+
const name = element.getAttribute('name');
|
|
290
|
+
if (name) return `${element.tagName.toLowerCase()}[name="${cssString(name)}"]`;
|
|
291
|
+
|
|
292
|
+
const parts = [];
|
|
293
|
+
let node = element;
|
|
294
|
+
while (node && node.tagName !== 'BODY' && node.tagName !== 'HTML' && parts.length < 6) {
|
|
295
|
+
let part = node.tagName.toLowerCase();
|
|
296
|
+
const parent = node.parentElement;
|
|
297
|
+
if (parent) {
|
|
298
|
+
const sameTag = [...parent.children].filter((child) => child.tagName === node.tagName);
|
|
299
|
+
if (sameTag.length > 1) part += `:nth-of-type(${sameTag.indexOf(node) + 1})`;
|
|
300
|
+
}
|
|
301
|
+
parts.unshift(part);
|
|
302
|
+
node = parent;
|
|
303
|
+
}
|
|
304
|
+
return parts.join(' > ');
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
function escapeHtml(text) {
|
|
308
|
+
return text
|
|
309
|
+
.replaceAll('&', '&')
|
|
310
|
+
.replaceAll('<', '<')
|
|
311
|
+
.replaceAll('>', '>')
|
|
312
|
+
.replaceAll('"', '"');
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
async function refreshSpecs() {
|
|
316
|
+
const response = await fetch('/__harness/specs');
|
|
317
|
+
if (!response.ok) return;
|
|
318
|
+
const specs = await response.json();
|
|
319
|
+
$('tests-tab').textContent = `Tests (${specs.length})`;
|
|
320
|
+
$('tests-panel').innerHTML = specs.length
|
|
321
|
+
? specs
|
|
322
|
+
.map(
|
|
323
|
+
(spec) =>
|
|
324
|
+
`<details><summary>${escapeHtml(spec.file)}<span class="count">${spec.tests.length} tests</span></summary><ul>${spec.tests.map((title) => `<li>${escapeHtml(title)}</li>`).join('')}</ul><pre>${escapeHtml(spec.source)}</pre></details>`
|
|
325
|
+
)
|
|
326
|
+
.join('')
|
|
327
|
+
: '<p class="hint">No *.spec.ts files under the tests directory.</p>';
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
$('load').addEventListener('click', loadPath);
|
|
331
|
+
$('app-path').addEventListener('keydown', (event) => {
|
|
332
|
+
if (event.key === 'Enter') loadPath();
|
|
333
|
+
});
|
|
334
|
+
$('reload').addEventListener('click', () => app.contentWindow?.location.reload());
|
|
335
|
+
$('pick').addEventListener('click', togglePick);
|
|
336
|
+
$('pick-inline').addEventListener('click', togglePick);
|
|
337
|
+
$('toggle-testids').addEventListener('click', toggleTestIds);
|
|
338
|
+
$('refresh-tests').addEventListener('click', refreshSpecs);
|
|
339
|
+
$('action').addEventListener('change', renderFields);
|
|
340
|
+
$('clear-steps').addEventListener('click', () => {
|
|
341
|
+
steps = [];
|
|
342
|
+
renderSteps();
|
|
343
|
+
renderCode();
|
|
344
|
+
});
|
|
345
|
+
addStepButton.addEventListener('click', addStep);
|
|
346
|
+
copyButton.addEventListener('click', async () => {
|
|
347
|
+
await navigator.clipboard.writeText(generatedCode());
|
|
348
|
+
copied = true;
|
|
349
|
+
renderCode();
|
|
350
|
+
setTimeout(() => {
|
|
351
|
+
copied = false;
|
|
352
|
+
renderCode();
|
|
353
|
+
}, 1500);
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
stepsEl.addEventListener('click', (event) => {
|
|
357
|
+
const button = event.target.closest('button[data-index]');
|
|
358
|
+
if (button) removeStep(Number(button.dataset.index));
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
for (const field of [$('value'), $('shot-path')]) {
|
|
362
|
+
field.addEventListener('input', renderFields);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
$('selector').addEventListener('focus', openSuggestions);
|
|
366
|
+
$('selector').addEventListener('input', () => {
|
|
367
|
+
openSuggestions();
|
|
368
|
+
renderFields();
|
|
369
|
+
});
|
|
370
|
+
$('selector').addEventListener('blur', closeSuggestions);
|
|
371
|
+
$('selector').addEventListener('keydown', (event) => {
|
|
372
|
+
if (event.key === 'Escape') closeSuggestions();
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
// mousedown beats blur, so the click lands before the list closes
|
|
376
|
+
suggestionsEl.addEventListener('mousedown', (event) => event.preventDefault());
|
|
377
|
+
suggestionsEl.addEventListener('click', (event) => {
|
|
378
|
+
const button = event.target.closest('button');
|
|
379
|
+
if (!button) return;
|
|
380
|
+
$('selector').value = button.textContent;
|
|
381
|
+
closeSuggestions();
|
|
382
|
+
renderFields();
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
$('collapse').addEventListener('click', () => {
|
|
386
|
+
const collapsed = $('panel').classList.toggle('collapsed');
|
|
387
|
+
$('collapse').textContent = collapsed ? '▴' : '▾';
|
|
388
|
+
});
|
|
389
|
+
|
|
390
|
+
for (const button of document.querySelectorAll('[data-tab]')) {
|
|
391
|
+
button.addEventListener('click', () => setTab(button.dataset.tab));
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
app.addEventListener('load', setupFrame);
|
|
395
|
+
|
|
396
|
+
function setupFrame() {
|
|
397
|
+
iframeDoc()?.addEventListener('click', onPick, true);
|
|
398
|
+
refreshTestIds();
|
|
399
|
+
applyTestIdOverlay();
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// the iframe can finish before this module runs, so set up once directly too
|
|
403
|
+
if (app.contentDocument?.readyState === 'complete') setupFrame();
|
|
404
|
+
|
|
405
|
+
renderFields();
|
|
406
|
+
renderSteps();
|
|
407
|
+
renderCode();
|
|
408
|
+
refreshSpecs();
|
package/index.html
ADDED
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="en">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8" />
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
6
|
+
<title>Test Harness</title>
|
|
7
|
+
<style>
|
|
8
|
+
:root {
|
|
9
|
+
--bg: #1d222b;
|
|
10
|
+
--panel: #272e3a;
|
|
11
|
+
--fg: #e7ebf3;
|
|
12
|
+
--muted: #9aa5b5;
|
|
13
|
+
--line: #3b4453;
|
|
14
|
+
--accent: #7c5cff;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
* {
|
|
18
|
+
box-sizing: border-box;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
[hidden] {
|
|
22
|
+
display: none !important;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
body {
|
|
26
|
+
margin: 0;
|
|
27
|
+
height: 100vh;
|
|
28
|
+
overflow: hidden;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
#app {
|
|
32
|
+
display: block;
|
|
33
|
+
width: 100%;
|
|
34
|
+
height: 100%;
|
|
35
|
+
border: 0;
|
|
36
|
+
background: #fff;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
.harness {
|
|
40
|
+
position: fixed;
|
|
41
|
+
inset: 0;
|
|
42
|
+
display: flex;
|
|
43
|
+
flex-direction: column;
|
|
44
|
+
background: var(--bg);
|
|
45
|
+
color: var(--fg);
|
|
46
|
+
font:
|
|
47
|
+
13px/1.45 ui-sans-serif,
|
|
48
|
+
system-ui,
|
|
49
|
+
sans-serif;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
.bar {
|
|
53
|
+
display: flex;
|
|
54
|
+
align-items: center;
|
|
55
|
+
gap: 0.5rem;
|
|
56
|
+
flex-wrap: wrap;
|
|
57
|
+
padding: 0.5rem 0.75rem;
|
|
58
|
+
background: var(--panel);
|
|
59
|
+
border-bottom: 1px solid var(--line);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
.bar strong {
|
|
63
|
+
margin-right: 0.5rem;
|
|
64
|
+
font-weight: 600;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
.path {
|
|
68
|
+
width: 10rem;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
.stage {
|
|
72
|
+
position: relative;
|
|
73
|
+
flex: 1;
|
|
74
|
+
min-height: 0;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
button,
|
|
78
|
+
input,
|
|
79
|
+
select {
|
|
80
|
+
font: inherit;
|
|
81
|
+
color: inherit;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
button {
|
|
85
|
+
padding: 0.25rem 0.6rem;
|
|
86
|
+
border: 1px solid var(--line);
|
|
87
|
+
border-radius: 0.35rem;
|
|
88
|
+
background: var(--panel);
|
|
89
|
+
cursor: pointer;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
button:hover:not(:disabled),
|
|
93
|
+
button.active {
|
|
94
|
+
background: var(--accent);
|
|
95
|
+
border-color: var(--accent);
|
|
96
|
+
color: #fff;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
button:disabled {
|
|
100
|
+
opacity: 0.45;
|
|
101
|
+
cursor: default;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
input,
|
|
105
|
+
select {
|
|
106
|
+
width: 100%;
|
|
107
|
+
padding: 0.3rem 0.45rem;
|
|
108
|
+
border: 1px solid var(--line);
|
|
109
|
+
border-radius: 0.35rem;
|
|
110
|
+
background: var(--panel);
|
|
111
|
+
min-width: 0;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
input:focus,
|
|
115
|
+
select:focus {
|
|
116
|
+
outline: 2px solid color-mix(in srgb, var(--accent) 60%, transparent);
|
|
117
|
+
outline-offset: 1px;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
.panel {
|
|
121
|
+
position: absolute;
|
|
122
|
+
right: 1rem;
|
|
123
|
+
bottom: 1rem;
|
|
124
|
+
width: min(30rem, calc(100vw - 2rem));
|
|
125
|
+
max-height: calc(100% - 4rem);
|
|
126
|
+
display: flex;
|
|
127
|
+
flex-direction: column;
|
|
128
|
+
background: var(--panel);
|
|
129
|
+
border: 1px solid var(--line);
|
|
130
|
+
border-radius: 0.75rem;
|
|
131
|
+
box-shadow: 0 10px 30px rgb(0 0 0 / 0.45);
|
|
132
|
+
overflow: hidden;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
.panel.collapsed {
|
|
136
|
+
width: auto;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
.panel-head {
|
|
140
|
+
display: flex;
|
|
141
|
+
align-items: center;
|
|
142
|
+
border-bottom: 1px solid var(--line);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
.tabs {
|
|
146
|
+
display: flex;
|
|
147
|
+
flex: 1;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
.tabs button {
|
|
151
|
+
flex: 1;
|
|
152
|
+
border: 0;
|
|
153
|
+
border-radius: 0;
|
|
154
|
+
padding: 0.55rem;
|
|
155
|
+
background: transparent;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
.tabs button.active {
|
|
159
|
+
border-bottom: 2px solid var(--accent);
|
|
160
|
+
background: transparent;
|
|
161
|
+
color: inherit;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
.icon {
|
|
165
|
+
border: 0;
|
|
166
|
+
background: transparent;
|
|
167
|
+
padding: 0.25rem 0.5rem;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
.panel-body {
|
|
171
|
+
overflow: auto;
|
|
172
|
+
padding: 0.75rem;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
.fields {
|
|
176
|
+
display: grid;
|
|
177
|
+
gap: 0.6rem;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
label {
|
|
181
|
+
display: grid;
|
|
182
|
+
gap: 0.3rem;
|
|
183
|
+
font-size: 0.75rem;
|
|
184
|
+
font-weight: 600;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
strong {
|
|
188
|
+
font-weight: 600;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
label .row {
|
|
192
|
+
margin-top: 0;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
.combo {
|
|
196
|
+
position: relative;
|
|
197
|
+
flex: 1;
|
|
198
|
+
min-width: 0;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
.suggestions {
|
|
202
|
+
position: absolute;
|
|
203
|
+
top: calc(100% + 2px);
|
|
204
|
+
left: 0;
|
|
205
|
+
right: 0;
|
|
206
|
+
z-index: 10;
|
|
207
|
+
max-height: 12rem;
|
|
208
|
+
overflow: auto;
|
|
209
|
+
margin: 0;
|
|
210
|
+
padding: 0.2rem;
|
|
211
|
+
list-style: none;
|
|
212
|
+
background: var(--panel);
|
|
213
|
+
border: 1px solid var(--line);
|
|
214
|
+
border-radius: 0.4rem;
|
|
215
|
+
box-shadow: 0 6px 18px rgb(0 0 0 / 0.35);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
.suggestions button {
|
|
219
|
+
display: block;
|
|
220
|
+
width: 100%;
|
|
221
|
+
padding: 0.3rem 0.4rem;
|
|
222
|
+
border: 0;
|
|
223
|
+
border-radius: 0.25rem;
|
|
224
|
+
background: transparent;
|
|
225
|
+
text-align: left;
|
|
226
|
+
font-size: 0.75rem;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
.suggestions button:hover {
|
|
230
|
+
background: var(--accent);
|
|
231
|
+
color: #fff;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
.row {
|
|
235
|
+
display: flex;
|
|
236
|
+
align-items: center;
|
|
237
|
+
gap: 0.4rem;
|
|
238
|
+
margin-top: 0.6rem;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
.row input {
|
|
242
|
+
flex: 1;
|
|
243
|
+
min-width: 0;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
.primary {
|
|
247
|
+
background: var(--accent);
|
|
248
|
+
border-color: var(--accent);
|
|
249
|
+
color: #fff;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
.hint {
|
|
253
|
+
margin: 0.6rem 0 0;
|
|
254
|
+
opacity: 0.7;
|
|
255
|
+
font-size: 0.8rem;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
.step-list {
|
|
259
|
+
margin: 0.6rem 0 0;
|
|
260
|
+
padding-left: 1.25rem;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
.step-list li {
|
|
264
|
+
display: flex;
|
|
265
|
+
align-items: flex-start;
|
|
266
|
+
gap: 0.4rem;
|
|
267
|
+
padding: 0.15rem 0;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
.step-text {
|
|
271
|
+
flex: 1;
|
|
272
|
+
min-width: 0;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
.step-detail {
|
|
276
|
+
margin-left: 0.35rem;
|
|
277
|
+
opacity: 0.7;
|
|
278
|
+
word-break: break-all;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
.code-head {
|
|
282
|
+
margin-top: 0;
|
|
283
|
+
justify-content: space-between;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
pre {
|
|
287
|
+
margin: 0.5rem 0 0;
|
|
288
|
+
padding: 0.6rem;
|
|
289
|
+
max-height: 16rem;
|
|
290
|
+
overflow: auto;
|
|
291
|
+
font-size: 0.75rem;
|
|
292
|
+
background: var(--bg);
|
|
293
|
+
border-radius: 0.4rem;
|
|
294
|
+
white-space: pre;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
details {
|
|
298
|
+
margin-bottom: 0.5rem;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
summary {
|
|
302
|
+
cursor: pointer;
|
|
303
|
+
padding: 0.25rem 0;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
.count {
|
|
307
|
+
margin-left: 0.4rem;
|
|
308
|
+
color: var(--muted);
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
ul {
|
|
312
|
+
margin: 0.25rem 0;
|
|
313
|
+
padding-left: 1.25rem;
|
|
314
|
+
}
|
|
315
|
+
</style>
|
|
316
|
+
</head>
|
|
317
|
+
<body>
|
|
318
|
+
<div class="harness">
|
|
319
|
+
<div class="bar">
|
|
320
|
+
<strong>Test Harness</strong>
|
|
321
|
+
<input id="app-path" class="path" aria-label="App path" value="/" />
|
|
322
|
+
<button id="load">Load</button>
|
|
323
|
+
<button id="reload">Reload</button>
|
|
324
|
+
<button id="pick">Pick element</button>
|
|
325
|
+
<button id="toggle-testids">Test IDs</button>
|
|
326
|
+
<button id="refresh-tests">Refresh tests</button>
|
|
327
|
+
</div>
|
|
328
|
+
|
|
329
|
+
<div class="stage">
|
|
330
|
+
<iframe id="app" src="/" title="Running app"></iframe>
|
|
331
|
+
|
|
332
|
+
<aside class="panel" id="panel">
|
|
333
|
+
<div class="panel-head">
|
|
334
|
+
<div class="tabs">
|
|
335
|
+
<button data-tab="actions" class="active">Actions</button>
|
|
336
|
+
<button data-tab="code">Code</button>
|
|
337
|
+
<button data-tab="tests" id="tests-tab">Tests (0)</button>
|
|
338
|
+
</div>
|
|
339
|
+
<button class="icon" id="collapse" aria-label="Collapse harness">▾</button>
|
|
340
|
+
</div>
|
|
341
|
+
|
|
342
|
+
<div class="panel-body" id="actions-panel">
|
|
343
|
+
<div class="fields">
|
|
344
|
+
<label>
|
|
345
|
+
Action
|
|
346
|
+
<select id="action">
|
|
347
|
+
<option value="goto">page.goto</option>
|
|
348
|
+
<option value="click" selected>click</option>
|
|
349
|
+
<option value="fill">fill</option>
|
|
350
|
+
<option value="press">press</option>
|
|
351
|
+
<option value="expectVisible">expect visible</option>
|
|
352
|
+
<option value="expectText">expect text</option>
|
|
353
|
+
<option value="screenshotPage">screenshot page</option>
|
|
354
|
+
<option value="screenshotElement">screenshot element</option>
|
|
355
|
+
</select>
|
|
356
|
+
</label>
|
|
357
|
+
|
|
358
|
+
<label id="selector-field">
|
|
359
|
+
Selector
|
|
360
|
+
<span class="row">
|
|
361
|
+
<span class="combo">
|
|
362
|
+
<input
|
|
363
|
+
id="selector"
|
|
364
|
+
placeholder="button.submit"
|
|
365
|
+
autocomplete="off"
|
|
366
|
+
aria-autocomplete="list"
|
|
367
|
+
/>
|
|
368
|
+
<ul class="suggestions" id="suggestions" hidden></ul>
|
|
369
|
+
</span>
|
|
370
|
+
<button id="pick-inline">Pick</button>
|
|
371
|
+
</span>
|
|
372
|
+
</label>
|
|
373
|
+
|
|
374
|
+
<label id="value-field">
|
|
375
|
+
<span id="value-label">Value</span>
|
|
376
|
+
<input id="value" />
|
|
377
|
+
</label>
|
|
378
|
+
|
|
379
|
+
<label id="path-field" hidden>
|
|
380
|
+
Screenshot path
|
|
381
|
+
<input id="shot-path" value="test-results/screenshots/harness.png" />
|
|
382
|
+
</label>
|
|
383
|
+
</div>
|
|
384
|
+
|
|
385
|
+
<p class="hint" id="step-error" hidden></p>
|
|
386
|
+
|
|
387
|
+
<div class="row">
|
|
388
|
+
<button class="primary" id="add-step">Add step</button>
|
|
389
|
+
<button id="clear-steps" disabled>Clear</button>
|
|
390
|
+
</div>
|
|
391
|
+
|
|
392
|
+
<ol class="step-list" id="steps" hidden></ol>
|
|
393
|
+
<p class="hint" id="steps-hint">Add actions to build a test.</p>
|
|
394
|
+
</div>
|
|
395
|
+
|
|
396
|
+
<div class="panel-body" id="code-panel" hidden>
|
|
397
|
+
<div class="row code-head">
|
|
398
|
+
<strong>Generated test</strong>
|
|
399
|
+
<button id="copy">Copy</button>
|
|
400
|
+
</div>
|
|
401
|
+
<pre id="code"></pre>
|
|
402
|
+
</div>
|
|
403
|
+
|
|
404
|
+
<div class="panel-body" id="tests-panel" hidden></div>
|
|
405
|
+
</aside>
|
|
406
|
+
</div>
|
|
407
|
+
</div>
|
|
408
|
+
|
|
409
|
+
<script type="module" src="./harness.js"></script>
|
|
410
|
+
</body>
|
|
411
|
+
</html>
|
package/package.json
CHANGED
|
@@ -1,6 +1,25 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "playwright-harness",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Framework-agnostic Playwright harness: browse your app in an iframe, pick elements, and generate test steps.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"playwright-harness": "./server.mjs"
|
|
8
|
+
},
|
|
9
|
+
"files": ["index.html", "harness.js", "server.mjs", "specs.mjs"],
|
|
10
|
+
"engines": {
|
|
11
|
+
"node": ">=18"
|
|
12
|
+
},
|
|
13
|
+
"keywords": ["playwright", "e2e", "testing", "harness", "data-testid"],
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/artesgo/kalimba.git",
|
|
18
|
+
"directory": "packages/playwright-harness"
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"start": "node server.mjs",
|
|
22
|
+
"test": "node --test specs.test.mjs",
|
|
23
|
+
"prepublishOnly": "npm test"
|
|
24
|
+
}
|
|
25
|
+
}
|
package/server.mjs
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Framework-agnostic Playwright harness: serves the UI and proxies your dev server,
|
|
3
|
+
// so the harness and the app share an origin and the iframe can be inspected.
|
|
4
|
+
import { existsSync } from 'node:fs';
|
|
5
|
+
import { readFile, stat } from 'node:fs/promises';
|
|
6
|
+
import { createServer, request } from 'node:http';
|
|
7
|
+
import { dirname, extname, join, resolve } from 'node:path';
|
|
8
|
+
import { fileURLToPath } from 'node:url';
|
|
9
|
+
import { readSpecFiles } from './specs.mjs';
|
|
10
|
+
|
|
11
|
+
const here = dirname(fileURLToPath(import.meta.url));
|
|
12
|
+
|
|
13
|
+
const options = {};
|
|
14
|
+
for (let i = 2; i < process.argv.length; i += 2) {
|
|
15
|
+
options[process.argv[i].replace(/^--/, '')] = process.argv[i + 1];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const app = (options.app ?? 'http://localhost:5173').replace(/\/$/, '');
|
|
19
|
+
const port = Number(options.port ?? 5180);
|
|
20
|
+
const tests = resolve(options.tests ?? 'tests');
|
|
21
|
+
|
|
22
|
+
const TYPES = {
|
|
23
|
+
'.html': 'text/html; charset=utf-8',
|
|
24
|
+
'.js': 'text/javascript; charset=utf-8',
|
|
25
|
+
'.css': 'text/css; charset=utf-8',
|
|
26
|
+
'.svg': 'image/svg+xml',
|
|
27
|
+
'.png': 'image/png'
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const server = createServer(async (req, res) => {
|
|
31
|
+
const { pathname } = new URL(req.url, 'http://localhost');
|
|
32
|
+
|
|
33
|
+
if (pathname === '/__harness/specs') {
|
|
34
|
+
res.writeHead(200, { 'content-type': 'application/json', 'cache-control': 'no-store' });
|
|
35
|
+
res.end(JSON.stringify(await readSpecFiles(tests)));
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
if (pathname === '/harness' || pathname === '/harness/') {
|
|
40
|
+
return send(res, join(here, 'index.html'));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// harness assets sit next to this file; every other path belongs to the app
|
|
44
|
+
const asset = resolve(here, `.${pathname}`);
|
|
45
|
+
if (asset.startsWith(here) && existsSync(asset) && (await stat(asset)).isFile()) {
|
|
46
|
+
return send(res, asset);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
proxy(req, res);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
// shortcut: no websocket upgrade handling, so the app's HMR socket will fail behind the proxy
|
|
53
|
+
function proxy(req, res) {
|
|
54
|
+
const target = new URL(req.url, app);
|
|
55
|
+
const upstream = request(
|
|
56
|
+
{
|
|
57
|
+
hostname: target.hostname,
|
|
58
|
+
port: target.port,
|
|
59
|
+
path: target.pathname + target.search,
|
|
60
|
+
method: req.method,
|
|
61
|
+
headers: { ...req.headers, host: target.host }
|
|
62
|
+
},
|
|
63
|
+
(response) => {
|
|
64
|
+
res.writeHead(response.statusCode ?? 502, response.headers);
|
|
65
|
+
response.pipe(res);
|
|
66
|
+
}
|
|
67
|
+
);
|
|
68
|
+
upstream.on('error', () => {
|
|
69
|
+
res.writeHead(502, { 'content-type': 'text/plain; charset=utf-8' });
|
|
70
|
+
res.end(`Cannot reach ${app}. Start the app's dev server first.`);
|
|
71
|
+
});
|
|
72
|
+
req.pipe(upstream);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async function send(res, file) {
|
|
76
|
+
res.writeHead(200, {
|
|
77
|
+
'content-type': TYPES[extname(file)] ?? 'application/octet-stream',
|
|
78
|
+
'cache-control': 'no-store'
|
|
79
|
+
});
|
|
80
|
+
res.end(await readFile(file));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
server.on('error', (error) => {
|
|
84
|
+
console.error(
|
|
85
|
+
error.code === 'EADDRINUSE'
|
|
86
|
+
? `Port ${port} is in use. Pass --port <port> to use another one.`
|
|
87
|
+
: error
|
|
88
|
+
);
|
|
89
|
+
process.exit(1);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
server.listen(port, () => {
|
|
93
|
+
console.log(`Harness: http://localhost:${port}/harness`);
|
|
94
|
+
console.log(`App: ${app} (proxied)`);
|
|
95
|
+
console.log(`Specs: ${tests}`);
|
|
96
|
+
});
|
package/specs.mjs
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { readdir, readFile } from 'node:fs/promises';
|
|
2
|
+
import { join, relative } from 'node:path';
|
|
3
|
+
|
|
4
|
+
// shortcut: regex title extraction, switch to an AST if titles start containing quotes
|
|
5
|
+
const TEST_TITLE = /(?:^|\n)\s*test(?!\.describe)(?:\.\w+)?\s*\(\s*(['"`])([^'"`\n]+)\1/g;
|
|
6
|
+
|
|
7
|
+
async function findSpecs(dir) {
|
|
8
|
+
let entries;
|
|
9
|
+
try {
|
|
10
|
+
entries = await readdir(dir, { withFileTypes: true });
|
|
11
|
+
} catch {
|
|
12
|
+
return [];
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const files = await Promise.all(
|
|
16
|
+
entries.map((entry) => {
|
|
17
|
+
const full = join(dir, entry.name);
|
|
18
|
+
if (entry.isDirectory()) return findSpecs(full);
|
|
19
|
+
return entry.name.endsWith('.spec.ts') ? [full] : [];
|
|
20
|
+
})
|
|
21
|
+
);
|
|
22
|
+
return files.flat();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export async function readSpecFiles(testsDir) {
|
|
26
|
+
const files = (await findSpecs(testsDir)).sort();
|
|
27
|
+
return Promise.all(
|
|
28
|
+
files.map(async (file) => {
|
|
29
|
+
const source = await readFile(file, 'utf8');
|
|
30
|
+
return {
|
|
31
|
+
file: relative(testsDir, file).replaceAll('\\', '/'),
|
|
32
|
+
source,
|
|
33
|
+
tests: [...source.matchAll(TEST_TITLE)].map((match) => match[2])
|
|
34
|
+
};
|
|
35
|
+
})
|
|
36
|
+
);
|
|
37
|
+
}
|