@rickcedwhat/playwright-smart-vision 0.3.0-next.01fec00 → 0.3.0-next.0563a0b
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/dist/author/apply.d.ts +2 -0
- package/dist/author/apply.d.ts.map +1 -1
- package/dist/author/apply.js +43 -2
- package/dist/author/apply.js.map +1 -1
- package/dist/author/author.test.js +35 -21
- package/dist/author/author.test.js.map +1 -1
- package/dist/author/catalog.d.ts +3 -6
- package/dist/author/catalog.d.ts.map +1 -1
- package/dist/author/catalog.js +61 -67
- package/dist/author/catalog.js.map +1 -1
- package/dist/author/detect.d.ts +4 -1
- package/dist/author/detect.d.ts.map +1 -1
- package/dist/author/detect.js +55 -6
- package/dist/author/detect.js.map +1 -1
- package/dist/author/geometry.d.ts +6 -0
- package/dist/author/geometry.d.ts.map +1 -1
- package/dist/author/geometry.js.map +1 -1
- package/dist/author/index.d.ts +2 -2
- package/dist/author/index.d.ts.map +1 -1
- package/dist/author/index.js +1 -1
- package/dist/author/index.js.map +1 -1
- package/dist/author/labels.d.ts +8 -0
- package/dist/author/labels.d.ts.map +1 -1
- package/dist/author/labels.js +41 -4
- package/dist/author/labels.js.map +1 -1
- package/dist/author/options.d.ts +2 -1
- package/dist/author/options.d.ts.map +1 -1
- package/dist/author/options.js +34 -10
- package/dist/author/options.js.map +1 -1
- package/dist/configure.d.ts +10 -0
- package/dist/configure.d.ts.map +1 -1
- package/dist/configure.js +4 -0
- package/dist/configure.js.map +1 -1
- package/dist/element.d.ts +79 -6
- package/dist/element.d.ts.map +1 -1
- package/dist/element.js +167 -34
- package/dist/element.js.map +1 -1
- package/dist/element.test.js +122 -1
- package/dist/element.test.js.map +1 -1
- package/dist/field-extractor.d.ts.map +1 -1
- package/dist/field-extractor.js +3 -2
- package/dist/field-extractor.js.map +1 -1
- package/dist/index.d.ts +14 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -3
- package/dist/index.js.map +1 -1
- package/dist/presets.d.ts +46 -0
- package/dist/presets.d.ts.map +1 -0
- package/dist/presets.js +57 -0
- package/dist/presets.js.map +1 -0
- package/dist/screen-api.d.ts +40 -0
- package/dist/screen-api.d.ts.map +1 -0
- package/dist/screen-api.js +22 -0
- package/dist/screen-api.js.map +1 -0
- package/dist/screen-api.test.d.ts +2 -0
- package/dist/screen-api.test.d.ts.map +1 -0
- package/dist/screen-api.test.js +42 -0
- package/dist/screen-api.test.js.map +1 -0
- package/dist/screen-handler.d.ts +31 -0
- package/dist/screen-handler.d.ts.map +1 -0
- package/dist/screen-handler.js +138 -0
- package/dist/screen-handler.js.map +1 -0
- package/dist/screen-handler.test.d.ts +2 -0
- package/dist/screen-handler.test.d.ts.map +1 -0
- package/dist/screen-handler.test.js +206 -0
- package/dist/screen-handler.test.js.map +1 -0
- package/dist/screen-result.d.ts +35 -0
- package/dist/screen-result.d.ts.map +1 -1
- package/dist/screen-result.js +103 -4
- package/dist/screen-result.js.map +1 -1
- package/dist/screen-result.test.d.ts +2 -0
- package/dist/screen-result.test.d.ts.map +1 -0
- package/dist/screen-result.test.js +71 -0
- package/dist/screen-result.test.js.map +1 -0
- package/dist/screen.d.ts +90 -9
- package/dist/screen.d.ts.map +1 -1
- package/dist/screen.js +148 -36
- package/dist/screen.js.map +1 -1
- package/dist/strategies.d.ts +83 -0
- package/dist/strategies.d.ts.map +1 -0
- package/dist/strategies.js +117 -0
- package/dist/strategies.js.map +1 -0
- package/dist/strategies.test.d.ts +2 -0
- package/dist/strategies.test.d.ts.map +1 -0
- package/dist/strategies.test.js +181 -0
- package/dist/strategies.test.js.map +1 -0
- package/dist/text-element.d.ts +70 -0
- package/dist/text-element.d.ts.map +1 -0
- package/dist/text-element.js +239 -0
- package/dist/text-element.js.map +1 -0
- package/dist/text-element.test.d.ts +2 -0
- package/dist/text-element.test.d.ts.map +1 -0
- package/dist/text-element.test.js +162 -0
- package/dist/text-element.test.js.map +1 -0
- package/dist/typed-screen.d.ts +6 -13
- package/dist/typed-screen.d.ts.map +1 -1
- package/dist/typed-screen.js +5 -9
- package/dist/typed-screen.js.map +1 -1
- package/dist/unhover.d.ts +10 -2
- package/dist/unhover.d.ts.map +1 -1
- package/dist/unhover.js +11 -2
- package/dist/unhover.js.map +1 -1
- package/dist/unhover.test.js +32 -2
- package/dist/unhover.test.js.map +1 -1
- package/dist/utils/ocr.d.ts +40 -0
- package/dist/utils/ocr.d.ts.map +1 -1
- package/dist/utils/ocr.js +56 -2
- package/dist/utils/ocr.js.map +1 -1
- package/dist/utils/ocr.test.js +53 -2
- package/dist/utils/ocr.test.js.map +1 -1
- package/package.json +1 -1
- package/dist/ocr-test.d.ts +0 -11
- package/dist/ocr-test.d.ts.map +0 -1
- package/dist/ocr-test.js +0 -36
- package/dist/ocr-test.js.map +0 -1
- package/dist/playwright-helper.d.ts +0 -64
- package/dist/playwright-helper.d.ts.map +0 -1
- package/dist/playwright-helper.js +0 -142
- package/dist/playwright-helper.js.map +0 -1
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { ScreenResult } from './screen-result.js';
|
|
2
|
+
import { ScreenElement, VISIBLE_CONFIDENCE } from './element.js';
|
|
3
|
+
import { loadScreen } from './configure.js';
|
|
4
|
+
import * as path from 'node:path';
|
|
5
|
+
// ---------------------------------------------------------------------------
|
|
6
|
+
// Registry
|
|
7
|
+
// ---------------------------------------------------------------------------
|
|
8
|
+
const handlers = [];
|
|
9
|
+
const activeTargets = new Set();
|
|
10
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
11
|
+
export function addScreenHandler(target, fn, options = {}) {
|
|
12
|
+
const dot = target.indexOf('.');
|
|
13
|
+
const screenName = dot === -1 ? target : target.slice(0, dot);
|
|
14
|
+
const elementName = dot === -1 ? undefined : target.slice(dot + 1);
|
|
15
|
+
const screenConfig = loadScreen(screenName);
|
|
16
|
+
let detectionConfig;
|
|
17
|
+
if (elementName) {
|
|
18
|
+
detectionConfig = screenConfig.elementConfigs.find((c) => c.name === elementName);
|
|
19
|
+
if (!detectionConfig) {
|
|
20
|
+
throw new Error(`addScreenHandler: element "${elementName}" not found in screen "${screenName}"`);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
const ready = screenConfig.ready;
|
|
25
|
+
const readyName = typeof ready === 'string' ? ready
|
|
26
|
+
: Array.isArray(ready) ? ready[0]
|
|
27
|
+
: ready && 'any' in ready ? ready.any[0]
|
|
28
|
+
: undefined;
|
|
29
|
+
detectionConfig = readyName
|
|
30
|
+
? screenConfig.elementConfigs.find((c) => c.name === readyName)
|
|
31
|
+
: screenConfig.elementConfigs[0];
|
|
32
|
+
if (!detectionConfig) {
|
|
33
|
+
throw new Error(`addScreenHandler: screen "${screenName}" has no elements to detect`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
const entry = {
|
|
37
|
+
target,
|
|
38
|
+
screenName,
|
|
39
|
+
elementName,
|
|
40
|
+
screenConfig,
|
|
41
|
+
detectionConfig,
|
|
42
|
+
fn,
|
|
43
|
+
times: options.times ?? Infinity,
|
|
44
|
+
noWaitAfter: options.noWaitAfter ?? false,
|
|
45
|
+
fireCount: 0,
|
|
46
|
+
};
|
|
47
|
+
handlers.push(entry);
|
|
48
|
+
return () => {
|
|
49
|
+
const idx = handlers.indexOf(entry);
|
|
50
|
+
if (idx !== -1)
|
|
51
|
+
handlers.splice(idx, 1);
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/** Remove all handlers registered for the given target (screen or element). */
|
|
55
|
+
export function removeScreenHandler(target) {
|
|
56
|
+
for (let i = handlers.length - 1; i >= 0; i--) {
|
|
57
|
+
if (handlers[i].target === target)
|
|
58
|
+
handlers.splice(i, 1);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/** Remove all registered handlers (called by release()). */
|
|
62
|
+
export function clearScreenHandlers() {
|
|
63
|
+
handlers.length = 0;
|
|
64
|
+
activeTargets.clear();
|
|
65
|
+
}
|
|
66
|
+
// ---------------------------------------------------------------------------
|
|
67
|
+
// Detection + dispatch
|
|
68
|
+
// ---------------------------------------------------------------------------
|
|
69
|
+
/**
|
|
70
|
+
* Check all registered handlers against an already-taken screenshot.
|
|
71
|
+
* Called from ScreenResult.captureLive() after every live screenshot.
|
|
72
|
+
*/
|
|
73
|
+
export async function runHandlers(shot, host, page) {
|
|
74
|
+
for (const entry of [...handlers]) {
|
|
75
|
+
if (activeTargets.has(entry.target))
|
|
76
|
+
continue;
|
|
77
|
+
if (entry.fireCount >= entry.times)
|
|
78
|
+
continue;
|
|
79
|
+
let located;
|
|
80
|
+
try {
|
|
81
|
+
located = await host.extractor.locateOnScreenshot(shot, entry.detectionConfig);
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
if ((located.confidence ?? 0) < VISIBLE_CONFIDENCE)
|
|
87
|
+
continue;
|
|
88
|
+
activeTargets.add(entry.target);
|
|
89
|
+
entry.fireCount++;
|
|
90
|
+
try {
|
|
91
|
+
const handlerHost = {
|
|
92
|
+
extractor: host.extractor,
|
|
93
|
+
screen: entry.screenConfig,
|
|
94
|
+
shotDir: host.shotDir,
|
|
95
|
+
...(host.unhover !== undefined && { unhover: host.unhover }),
|
|
96
|
+
};
|
|
97
|
+
let arg;
|
|
98
|
+
if (entry.elementName) {
|
|
99
|
+
const bound = ScreenResult.bind(page, host.extractor, entry.screenConfig, host.shotDir, {
|
|
100
|
+
...(host.unhover !== undefined && { unhover: host.unhover }),
|
|
101
|
+
});
|
|
102
|
+
arg = new ScreenElement(located, page, bound);
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
arg = await ScreenResult.fromShot(shot, page, handlerHost);
|
|
106
|
+
}
|
|
107
|
+
await entry.fn(arg);
|
|
108
|
+
if (!entry.noWaitAfter) {
|
|
109
|
+
await waitUntilGone(page, host.shotDir, entry.screenName, host.extractor, entry.detectionConfig);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
finally {
|
|
113
|
+
activeTargets.delete(entry.target);
|
|
114
|
+
}
|
|
115
|
+
if (entry.fireCount >= entry.times) {
|
|
116
|
+
const idx = handlers.indexOf(entry);
|
|
117
|
+
if (idx !== -1)
|
|
118
|
+
handlers.splice(idx, 1);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
async function waitUntilGone(page, shotDir, screenName, extractor, detectionConfig) {
|
|
123
|
+
const pollShot = path.join(shotDir, `${screenName}-handler-gone.png`);
|
|
124
|
+
const deadline = Date.now() + 10_000;
|
|
125
|
+
while (Date.now() < deadline) {
|
|
126
|
+
await new Promise((r) => setTimeout(r, 150));
|
|
127
|
+
try {
|
|
128
|
+
await page.screenshot({ path: pollShot, timeout: 2_000 });
|
|
129
|
+
const located = await extractor.locateOnScreenshot(pollShot, detectionConfig);
|
|
130
|
+
if ((located.confidence ?? 0) < VISIBLE_CONFIDENCE)
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
catch {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
//# sourceMappingURL=screen-handler.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"screen-handler.js","sourceRoot":"","sources":["../src/screen-handler.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AACjE,OAAO,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAC5C,OAAO,KAAK,IAAI,MAAM,WAAW,CAAC;AA0BlC,8EAA8E;AAC9E,WAAW;AACX,8EAA8E;AAE9E,MAAM,QAAQ,GAAmB,EAAE,CAAC;AACpC,MAAM,aAAa,GAAG,IAAI,GAAG,EAAU,CAAC;AA2BxC,8DAA8D;AAC9D,MAAM,UAAU,gBAAgB,CAAC,MAAc,EAAE,EAAmC,E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C,CAAC;QACnD,IAAI,CAAC;YACH,MAAM,IAAI,CAAC,UAAU,CAAC,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,KAAK,EAAE,CAAC,CAAC;YAC1D,MAAM,OAAO,GAAG,MAAM,SAAS,CAAC,kBAAkB,CAAC,QAAQ,EAAE,eAAe,CAAC,CAAC;YAC9E,IAAI,CAAC,OAAO,CAAC,UAAU,IAAI,CAAC,CAAC,GAAG,kBAAkB;gBAAE,OAAO;QAC7D,CAAC;QAAC,MAAM,CAAC;YACP,OAAO;QACT,CAAC;IACH,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"screen-handler.test.d.ts","sourceRoot":"","sources":["../src/screen-handler.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, vi } from 'vitest';
|
|
2
|
+
import { addScreenHandler, removeScreenHandler, clearScreenHandlers, runHandlers } from './screen-handler.js';
|
|
3
|
+
import { ScreenResult } from './screen-result.js';
|
|
4
|
+
import { ScreenElement } from './element.js';
|
|
5
|
+
// ---------------------------------------------------------------------------
|
|
6
|
+
// Helpers
|
|
7
|
+
// ---------------------------------------------------------------------------
|
|
8
|
+
function makeExtractor(confidence = 0.95) {
|
|
9
|
+
return {
|
|
10
|
+
loadForms: vi.fn().mockResolvedValue(undefined),
|
|
11
|
+
locateOnScreenshot: vi.fn().mockResolvedValue({
|
|
12
|
+
name: 'ok',
|
|
13
|
+
value: '',
|
|
14
|
+
location: { x: 10, y: 20, width: 80, height: 30 },
|
|
15
|
+
confidence,
|
|
16
|
+
isEmpty: true,
|
|
17
|
+
}),
|
|
18
|
+
extractElements: vi.fn().mockResolvedValue({
|
|
19
|
+
elements: [{ name: 'ok', value: '', location: { x: 10, y: 20, width: 80, height: 30 }, isEmpty: false }],
|
|
20
|
+
totalElements: 1,
|
|
21
|
+
filledElements: 1,
|
|
22
|
+
emptyElements: 0,
|
|
23
|
+
}),
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
function makeHost(extractor = makeExtractor()) {
|
|
27
|
+
return {
|
|
28
|
+
extractor,
|
|
29
|
+
screen: {
|
|
30
|
+
name: 'license-warning',
|
|
31
|
+
blankScreenPath: '/tmp/blank.png',
|
|
32
|
+
ready: 'ok',
|
|
33
|
+
elementConfigs: [
|
|
34
|
+
{ name: 'ok', templatePath: '/tmp/ok.png' },
|
|
35
|
+
{ name: 'cancel', templatePath: '/tmp/cancel.png' },
|
|
36
|
+
],
|
|
37
|
+
},
|
|
38
|
+
shotDir: '/tmp/shots',
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function makePage() {
|
|
42
|
+
return {
|
|
43
|
+
screenshot: vi.fn().mockResolvedValue(Buffer.from('')),
|
|
44
|
+
mouse: { click: vi.fn() },
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
// Stub loadScreen so handlers can be added without touching disk
|
|
48
|
+
vi.mock('./configure.js', () => ({
|
|
49
|
+
loadScreen: vi.fn((name) => ({
|
|
50
|
+
name,
|
|
51
|
+
blankScreenPath: `/tmp/${name}/blank.png`,
|
|
52
|
+
ready: 'ok',
|
|
53
|
+
elementConfigs: [
|
|
54
|
+
{ name: 'ok', templatePath: `/tmp/${name}/ok.png` },
|
|
55
|
+
{ name: 'dismiss-btn', templatePath: `/tmp/${name}/dismiss-btn.png` },
|
|
56
|
+
],
|
|
57
|
+
})),
|
|
58
|
+
}));
|
|
59
|
+
// ---------------------------------------------------------------------------
|
|
60
|
+
// Tests
|
|
61
|
+
// ---------------------------------------------------------------------------
|
|
62
|
+
describe('addScreenHandler / removeScreenHandler', () => {
|
|
63
|
+
beforeEach(() => {
|
|
64
|
+
clearScreenHandlers();
|
|
65
|
+
});
|
|
66
|
+
it('returns a cleanup function that removes the handler', async () => {
|
|
67
|
+
const fn = vi.fn();
|
|
68
|
+
const cleanup = addScreenHandler('license-warning', fn);
|
|
69
|
+
cleanup();
|
|
70
|
+
const host = makeHost();
|
|
71
|
+
await runHandlers('/tmp/shot.png', host, makePage());
|
|
72
|
+
expect(fn).not.toHaveBeenCalled();
|
|
73
|
+
});
|
|
74
|
+
it('removeScreenHandler removes by target string', async () => {
|
|
75
|
+
const fn = vi.fn();
|
|
76
|
+
addScreenHandler('license-warning', fn);
|
|
77
|
+
removeScreenHandler('license-warning');
|
|
78
|
+
const host = makeHost();
|
|
79
|
+
await runHandlers('/tmp/shot.png', host, makePage());
|
|
80
|
+
expect(fn).not.toHaveBeenCalled();
|
|
81
|
+
});
|
|
82
|
+
it('multiple handlers for the same target are independently removable', async () => {
|
|
83
|
+
const fn1 = vi.fn();
|
|
84
|
+
const fn2 = vi.fn();
|
|
85
|
+
const cleanup1 = addScreenHandler('license-warning', fn1, { noWaitAfter: true });
|
|
86
|
+
addScreenHandler('license-warning', fn2, { noWaitAfter: true });
|
|
87
|
+
cleanup1();
|
|
88
|
+
const host = makeHost();
|
|
89
|
+
await runHandlers('/tmp/shot.png', host, makePage());
|
|
90
|
+
expect(fn1).not.toHaveBeenCalled();
|
|
91
|
+
expect(fn2).toHaveBeenCalledOnce();
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
describe('runHandlers — screen form', () => {
|
|
95
|
+
beforeEach(() => {
|
|
96
|
+
clearScreenHandlers();
|
|
97
|
+
});
|
|
98
|
+
it('fires when detection confidence is above threshold', async () => {
|
|
99
|
+
const fn = vi.fn();
|
|
100
|
+
addScreenHandler('license-warning', fn, { noWaitAfter: true });
|
|
101
|
+
const host = makeHost(makeExtractor(0.95));
|
|
102
|
+
await runHandlers('/tmp/shot.png', host, makePage());
|
|
103
|
+
expect(fn).toHaveBeenCalledOnce();
|
|
104
|
+
});
|
|
105
|
+
it('does not fire when confidence is below threshold', async () => {
|
|
106
|
+
const fn = vi.fn();
|
|
107
|
+
addScreenHandler('license-warning', fn, { noWaitAfter: true });
|
|
108
|
+
const host = makeHost(makeExtractor(0.3));
|
|
109
|
+
await runHandlers('/tmp/shot.png', host, makePage());
|
|
110
|
+
expect(fn).not.toHaveBeenCalled();
|
|
111
|
+
});
|
|
112
|
+
it('passes a ScreenResult to the callback', async () => {
|
|
113
|
+
let received;
|
|
114
|
+
addScreenHandler('license-warning', async (s) => { received = s; }, { noWaitAfter: true });
|
|
115
|
+
const host = makeHost();
|
|
116
|
+
await runHandlers('/tmp/shot.png', host, makePage());
|
|
117
|
+
expect(received).toBeInstanceOf(ScreenResult);
|
|
118
|
+
});
|
|
119
|
+
it('respects times: 1 — fires once then removes itself', async () => {
|
|
120
|
+
const fn = vi.fn();
|
|
121
|
+
addScreenHandler('license-warning', fn, { times: 1, noWaitAfter: true });
|
|
122
|
+
const host = makeHost();
|
|
123
|
+
await runHandlers('/tmp/shot.png', host, makePage());
|
|
124
|
+
await runHandlers('/tmp/shot.png', host, makePage());
|
|
125
|
+
expect(fn).toHaveBeenCalledOnce();
|
|
126
|
+
});
|
|
127
|
+
it('fires up to times: 2', async () => {
|
|
128
|
+
const fn = vi.fn();
|
|
129
|
+
addScreenHandler('license-warning', fn, { times: 2, noWaitAfter: true });
|
|
130
|
+
const host = makeHost();
|
|
131
|
+
await runHandlers('/tmp/shot.png', host, makePage());
|
|
132
|
+
await runHandlers('/tmp/shot.png', host, makePage());
|
|
133
|
+
await runHandlers('/tmp/shot.png', host, makePage());
|
|
134
|
+
expect(fn).toHaveBeenCalledTimes(2);
|
|
135
|
+
});
|
|
136
|
+
it('does not re-enter while the handler is active', async () => {
|
|
137
|
+
const calls = [];
|
|
138
|
+
addScreenHandler('license-warning', async () => {
|
|
139
|
+
calls.push('enter');
|
|
140
|
+
const host = makeHost();
|
|
141
|
+
// simulate a nested runHandlers call (e.g. from a click inside the handler)
|
|
142
|
+
await runHandlers('/tmp/shot.png', host, makePage());
|
|
143
|
+
calls.push('exit');
|
|
144
|
+
}, { noWaitAfter: true });
|
|
145
|
+
const host = makeHost();
|
|
146
|
+
await runHandlers('/tmp/shot.png', host, makePage());
|
|
147
|
+
expect(calls).toEqual(['enter', 'exit']);
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
describe('runHandlers — dotted element form', () => {
|
|
151
|
+
beforeEach(() => {
|
|
152
|
+
clearScreenHandlers();
|
|
153
|
+
});
|
|
154
|
+
it('passes a ScreenElement to the callback', async () => {
|
|
155
|
+
let received;
|
|
156
|
+
addScreenHandler('license-warning.dismiss-btn', async (el) => { received = el; }, { noWaitAfter: true });
|
|
157
|
+
const host = makeHost();
|
|
158
|
+
await runHandlers('/tmp/shot.png', host, makePage());
|
|
159
|
+
expect(received).toBeInstanceOf(ScreenElement);
|
|
160
|
+
});
|
|
161
|
+
it('fires when detection confidence is above threshold', async () => {
|
|
162
|
+
const fn = vi.fn();
|
|
163
|
+
addScreenHandler('license-warning.dismiss-btn', fn, { noWaitAfter: true });
|
|
164
|
+
const host = makeHost(makeExtractor(0.9));
|
|
165
|
+
await runHandlers('/tmp/shot.png', host, makePage());
|
|
166
|
+
expect(fn).toHaveBeenCalledOnce();
|
|
167
|
+
});
|
|
168
|
+
it('does not fire when confidence is below threshold', async () => {
|
|
169
|
+
const fn = vi.fn();
|
|
170
|
+
addScreenHandler('license-warning.dismiss-btn', fn, { noWaitAfter: true });
|
|
171
|
+
const host = makeHost(makeExtractor(0.2));
|
|
172
|
+
await runHandlers('/tmp/shot.png', host, makePage());
|
|
173
|
+
expect(fn).not.toHaveBeenCalled();
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
describe('noWaitAfter option', () => {
|
|
177
|
+
beforeEach(() => {
|
|
178
|
+
clearScreenHandlers();
|
|
179
|
+
});
|
|
180
|
+
it('skips the wait-until-gone poll when noWaitAfter is true', async () => {
|
|
181
|
+
const fn = vi.fn();
|
|
182
|
+
addScreenHandler('license-warning', fn, { noWaitAfter: true });
|
|
183
|
+
const page = makePage();
|
|
184
|
+
const host = makeHost();
|
|
185
|
+
await runHandlers('/tmp/shot.png', host, page);
|
|
186
|
+
expect(fn).toHaveBeenCalledOnce();
|
|
187
|
+
// No page.screenshot call for the gone-poll (the mock extractor would keep returning high confidence)
|
|
188
|
+
expect(page.screenshot).not.toHaveBeenCalled();
|
|
189
|
+
});
|
|
190
|
+
it('waits until gone when noWaitAfter is false — polls page.screenshot until low confidence', async () => {
|
|
191
|
+
const fn = vi.fn();
|
|
192
|
+
addScreenHandler('license-warning', fn); // noWaitAfter defaults to false
|
|
193
|
+
const extractor = makeExtractor(0.95);
|
|
194
|
+
// Detection returns high confidence; gone-poll returns low confidence on the first poll
|
|
195
|
+
extractor.locateOnScreenshot
|
|
196
|
+
.mockResolvedValueOnce({ name: 'ok', value: '', location: { x: 0, y: 0, width: 1, height: 1 }, confidence: 0.95, isEmpty: true }) // detection
|
|
197
|
+
.mockResolvedValueOnce({ name: 'ok', value: '', location: { x: 0, y: 0, width: 1, height: 1 }, confidence: 0.1, isEmpty: true }); // gone-poll succeeds
|
|
198
|
+
const host = makeHost(extractor);
|
|
199
|
+
const page = makePage();
|
|
200
|
+
await runHandlers('/tmp/shot.png', host, page);
|
|
201
|
+
expect(fn).toHaveBeenCalledOnce();
|
|
202
|
+
// page.screenshot should be called at least once for the gone-poll
|
|
203
|
+
expect(page.screenshot).toHaveBeenCalled();
|
|
204
|
+
});
|
|
205
|
+
});
|
|
206
|
+
//# sourceMappingURL=screen-handler.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"screen-handler.test.js","sourceRoot":"","sources":["../src/screen-handler.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,UAAU,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AAC9D,OAAO,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAC9G,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;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nE,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,gBAAgB,EAAE,CAAC;IAC7C,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
package/dist/screen-result.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import type { ElementConfig, ElementResult, ScreenComparison } from './types.js';
|
|
2
2
|
import type { ScreenConfig } from './screen-config.js';
|
|
3
3
|
import { ScreenElement, type MatchOptions, type WaitForOptions } from './element.js';
|
|
4
|
+
import { TextElement, type TextQuery, type TextElementOptions } from './text-element.js';
|
|
4
5
|
import type { Page } from '@playwright/test';
|
|
6
|
+
import { type FieldRead } from './utils/ocr.js';
|
|
5
7
|
export type ScreenExtractor = {
|
|
6
8
|
loadForms(blankFormPath: string, filledFormPath: string): Promise<void>;
|
|
7
9
|
locateOnScreenshot(screenshotPath: string, config: ElementConfig): Promise<ElementResult>;
|
|
@@ -20,6 +22,8 @@ export type ScreenHost = {
|
|
|
20
22
|
overlay?: boolean;
|
|
21
23
|
/** Override global unhoverBeforeCapture for this bind. */
|
|
22
24
|
unhover?: boolean;
|
|
25
|
+
/** init()-level read override — wins over index.json, loses to call site. */
|
|
26
|
+
initRead?: FieldRead;
|
|
23
27
|
};
|
|
24
28
|
/**
|
|
25
29
|
* Playwright-style screen result wrapper
|
|
@@ -31,12 +35,18 @@ export declare class ScreenResult {
|
|
|
31
35
|
private host?;
|
|
32
36
|
private dirty;
|
|
33
37
|
constructor(comparison: ScreenComparison, page?: Page | undefined, host?: ScreenHost | undefined);
|
|
38
|
+
/**
|
|
39
|
+
* Construct a ScreenResult pre-populated from an already-taken screenshot.
|
|
40
|
+
* Used by screen handler dispatch — avoids an extra screenshot round-trip.
|
|
41
|
+
*/
|
|
42
|
+
static fromShot(shot: string, page: Page, host: ScreenHost): Promise<ScreenResult>;
|
|
34
43
|
/**
|
|
35
44
|
* Bind a screen to a live page so element().waitFor / fill / click can screenshot and match.
|
|
36
45
|
*/
|
|
37
46
|
static bind(page: Page, extractor: ScreenExtractor, screen: ScreenConfig, shotDir: string, options?: {
|
|
38
47
|
overlay?: boolean;
|
|
39
48
|
unhover?: boolean;
|
|
49
|
+
initRead?: FieldRead;
|
|
40
50
|
}): ScreenResult;
|
|
41
51
|
/**
|
|
42
52
|
* Get an element by name
|
|
@@ -70,6 +80,31 @@ export declare class ScreenResult {
|
|
|
70
80
|
private loadExtracted;
|
|
71
81
|
paintOverlay(result: ElementResult, label?: string): Promise<void>;
|
|
72
82
|
hideOverlay(): Promise<void>;
|
|
83
|
+
private captureWords;
|
|
84
|
+
/**
|
|
85
|
+
* Find the first occurrence of text on screen via OCR.
|
|
86
|
+
* Waits until the text is visible (by default).
|
|
87
|
+
* Use for dynamic / ad-hoc content that is not registered in index.json —
|
|
88
|
+
* dropdown options, toast messages, menu rows, table cells.
|
|
89
|
+
*
|
|
90
|
+
* @param query - Exact string (case-insensitive) or RegExp.
|
|
91
|
+
* @param options.timeout - How long to wait for the text to appear (ms). Default 15 000.
|
|
92
|
+
*/
|
|
93
|
+
getByText(query: TextQuery, options?: TextElementOptions): Promise<TextElement>;
|
|
94
|
+
/**
|
|
95
|
+
* Find all occurrences of text on screen via OCR.
|
|
96
|
+
* Returns immediately with whatever matches are currently visible — no implicit wait.
|
|
97
|
+
* Use for list rows, repeated badges, or any case where multiple matches are expected.
|
|
98
|
+
*/
|
|
99
|
+
getAllByText(query: TextQuery): Promise<TextElement[]>;
|
|
100
|
+
/**
|
|
101
|
+
* Assert that the given text is visible anywhere on screen.
|
|
102
|
+
* Useful for smoke-checking dynamic feedback without registering an element.
|
|
103
|
+
*
|
|
104
|
+
* @param query - Exact string (case-insensitive) or RegExp.
|
|
105
|
+
* @param options.timeout - How long to poll (ms). Default: expectTimeout().
|
|
106
|
+
*/
|
|
107
|
+
toContainText(query: TextQuery, options?: TextElementOptions): Promise<void>;
|
|
73
108
|
/**
|
|
74
109
|
* Get all elements
|
|
75
110
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"screen-result.d.ts","sourceRoot":"","sources":["../src/screen-result.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AACjF,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,EAAE,aAAa,EAAsB,KAAK,YAAY,EAAE,KAAK,cAAc,EAAE,MAAM,cAAc,CAAC;AACzG,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"screen-result.d.ts","sourceRoot":"","sources":["../src/screen-result.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,aAAa,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AACjF,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,EAAE,aAAa,EAAsB,KAAK,YAAY,EAAE,KAAK,cAAc,EAAE,MAAM,cAAc,CAAC;AACzG,OAAO,EAAE,WAAW,EAAgC,KAAK,SAAS,EAAE,KAAK,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AACvH,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,kBAAkB,CAAC;AAI7C,OAAO,EAAmD,KAAK,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAKjG,MAAM,MAAM,eAAe,GAAG;IAC5B,SAAS,CAAC,aAAa,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACxE,kBAAkB,CAAC,cAAc,EAAE,MAAM,EAAE,MAAM,EAAE,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC;IAC1F,eAAe,CAAC,CACd,OAAO,EAAE,SAAS,aAAa,EAAE,GAAG,aAAa,EAAE,GAClD,OAAO,CAAC,gBAAgB,CAAC,CAAC;IAC7B,aAAa,CAAC,CACZ,OAAO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pB;;OAEG;IACH,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAKjC;;OAEG;IACH,GAAG,IAAI,gBAAgB;CAGxB"}
|
package/dist/screen-result.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { ScreenElement, VISIBLE_CONFIDENCE } from './element.js';
|
|
2
|
-
import {
|
|
2
|
+
import { TextElement, findAllMatches, extractWords } from './text-element.js';
|
|
3
|
+
import { ocrStep, expectTimeout } from './ocr-step.js';
|
|
3
4
|
import { hideOcrOverlay, overlayBoxesFromResult, showOcrOverlay } from './ocr-overlay.js';
|
|
4
5
|
import { unhoverBeforeCapture } from './unhover.js';
|
|
6
|
+
import { resolveCharsetSwaps, getOcrStrategy, getOCRUtil } from './utils/ocr.js';
|
|
5
7
|
import * as fs from 'node:fs';
|
|
6
8
|
import * as path from 'node:path';
|
|
9
|
+
import { runHandlers } from './screen-handler.js';
|
|
7
10
|
async function extractComparison(extractor, configs) {
|
|
8
11
|
if (extractor.extractElements) {
|
|
9
12
|
return extractor.extractElements(configs);
|
|
@@ -33,6 +36,15 @@ export class ScreenResult {
|
|
|
33
36
|
this.page = page;
|
|
34
37
|
this.host = host;
|
|
35
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* Construct a ScreenResult pre-populated from an already-taken screenshot.
|
|
41
|
+
* Used by screen handler dispatch — avoids an extra screenshot round-trip.
|
|
42
|
+
*/
|
|
43
|
+
static async fromShot(shot, page, host) {
|
|
44
|
+
await host.extractor.loadForms(host.screen.blankScreenPath, shot);
|
|
45
|
+
const comparison = await extractComparison(host.extractor, host.screen.elementConfigs);
|
|
46
|
+
return new ScreenResult(comparison, page, host);
|
|
47
|
+
}
|
|
36
48
|
/**
|
|
37
49
|
* Bind a screen to a live page so element().waitFor / fill / click can screenshot and match.
|
|
38
50
|
*/
|
|
@@ -48,6 +60,7 @@ export class ScreenResult {
|
|
|
48
60
|
shotDir,
|
|
49
61
|
...(options.overlay !== undefined && { overlay: options.overlay }),
|
|
50
62
|
...(options.unhover !== undefined && { unhover: options.unhover }),
|
|
63
|
+
...(options.initRead !== undefined && { initRead: options.initRead }),
|
|
51
64
|
});
|
|
52
65
|
}
|
|
53
66
|
/**
|
|
@@ -87,9 +100,11 @@ export class ScreenResult {
|
|
|
87
100
|
return {};
|
|
88
101
|
const part = partName ? config.parts?.find(row => row.name === partName) : undefined;
|
|
89
102
|
const match = {};
|
|
90
|
-
|
|
91
|
-
const
|
|
92
|
-
const
|
|
103
|
+
// Resolution order: part > init() override > config (index.json) > Strategies.Ocr > (built-in defaults in element.ts)
|
|
104
|
+
const strategy = getOcrStrategy();
|
|
105
|
+
const swaps = part?.swaps ?? resolveCharsetSwaps(part?.charset) ?? config.swaps ?? resolveCharsetSwaps(config.charset) ?? strategy?.swaps;
|
|
106
|
+
const overflow = part?.overflow ?? config.overflow ?? strategy?.overflow;
|
|
107
|
+
const read = part?.read ?? this.host?.initRead ?? config.read ?? strategy?.read;
|
|
93
108
|
if (swaps)
|
|
94
109
|
match.swaps = swaps;
|
|
95
110
|
if (overflow)
|
|
@@ -246,6 +261,8 @@ export class ScreenResult {
|
|
|
246
261
|
async captureLive(shot) {
|
|
247
262
|
await unhoverBeforeCapture(this.page, this.host?.unhover);
|
|
248
263
|
await this.page.screenshot({ path: shot, timeout: 2_000 });
|
|
264
|
+
if (this.host)
|
|
265
|
+
await runHandlers(shot, this.host, this.page);
|
|
249
266
|
}
|
|
250
267
|
async loadExtracted(shot) {
|
|
251
268
|
if (!this.host) {
|
|
@@ -264,6 +281,88 @@ export class ScreenResult {
|
|
|
264
281
|
return;
|
|
265
282
|
await hideOcrOverlay(this.page);
|
|
266
283
|
}
|
|
284
|
+
// ---------------------------------------------------------------------------
|
|
285
|
+
// Text locators
|
|
286
|
+
// ---------------------------------------------------------------------------
|
|
287
|
+
async captureWords() {
|
|
288
|
+
if (!this.host || !this.page) {
|
|
289
|
+
throw new Error('getByText / toContainText requires ScreenResult.bind(page, ...)');
|
|
290
|
+
}
|
|
291
|
+
return extractWords(this.page, this.host.shotDir, this.host.screen.name, this.host.unhover);
|
|
292
|
+
}
|
|
293
|
+
/**
|
|
294
|
+
* Find the first occurrence of text on screen via OCR.
|
|
295
|
+
* Waits until the text is visible (by default).
|
|
296
|
+
* Use for dynamic / ad-hoc content that is not registered in index.json —
|
|
297
|
+
* dropdown options, toast messages, menu rows, table cells.
|
|
298
|
+
*
|
|
299
|
+
* @param query - Exact string (case-insensitive) or RegExp.
|
|
300
|
+
* @param options.timeout - How long to wait for the text to appear (ms). Default 15 000.
|
|
301
|
+
*/
|
|
302
|
+
async getByText(query, options) {
|
|
303
|
+
return ocrStep(`screen.getByText(${String(query)})`, async () => {
|
|
304
|
+
if (!this.host || !this.page) {
|
|
305
|
+
throw new Error('getByText requires ScreenResult.bind(page, ...)');
|
|
306
|
+
}
|
|
307
|
+
const timeout = options?.timeout ?? 15_000;
|
|
308
|
+
const deadline = Date.now() + timeout;
|
|
309
|
+
while (true) {
|
|
310
|
+
try {
|
|
311
|
+
const { words: w } = await this.captureWords();
|
|
312
|
+
const matches = findAllMatches(w, query);
|
|
313
|
+
if (matches[0]) {
|
|
314
|
+
return new TextElement(matches[0], query, this.page, this.host.shotDir, this.host.screen.name, this.host.unhover);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
catch {
|
|
318
|
+
// transient screenshot error (navigation, partial load) — retry
|
|
319
|
+
}
|
|
320
|
+
if (Date.now() >= deadline)
|
|
321
|
+
break;
|
|
322
|
+
await new Promise((r) => setTimeout(r, 150));
|
|
323
|
+
}
|
|
324
|
+
throw new Error(`Text ${String(query)} not found within ${timeout}ms`);
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Find all occurrences of text on screen via OCR.
|
|
329
|
+
* Returns immediately with whatever matches are currently visible — no implicit wait.
|
|
330
|
+
* Use for list rows, repeated badges, or any case where multiple matches are expected.
|
|
331
|
+
*/
|
|
332
|
+
async getAllByText(query) {
|
|
333
|
+
return ocrStep(`screen.getAllByText(${String(query)})`, async () => {
|
|
334
|
+
const { words } = await this.captureWords();
|
|
335
|
+
const matches = findAllMatches(words, query);
|
|
336
|
+
return matches.map((m) => new TextElement(m, query, this.page, this.host?.shotDir, this.host?.screen.name, this.host?.unhover));
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* Assert that the given text is visible anywhere on screen.
|
|
341
|
+
* Useful for smoke-checking dynamic feedback without registering an element.
|
|
342
|
+
*
|
|
343
|
+
* @param query - Exact string (case-insensitive) or RegExp.
|
|
344
|
+
* @param options.timeout - How long to poll (ms). Default: expectTimeout().
|
|
345
|
+
*/
|
|
346
|
+
async toContainText(query, options) {
|
|
347
|
+
return ocrStep(`screen.toContainText(${String(query)})`, async () => {
|
|
348
|
+
const timeout = options?.timeout ?? expectTimeout();
|
|
349
|
+
const deadline = Date.now() + timeout;
|
|
350
|
+
while (true) {
|
|
351
|
+
try {
|
|
352
|
+
const { words } = await this.captureWords();
|
|
353
|
+
if (findAllMatches(words, query).some((m) => m.confidence >= VISIBLE_CONFIDENCE))
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
356
|
+
catch {
|
|
357
|
+
// transient screenshot error (navigation, partial load) — retry
|
|
358
|
+
}
|
|
359
|
+
if (Date.now() >= deadline)
|
|
360
|
+
break;
|
|
361
|
+
await new Promise((r) => setTimeout(r, 150));
|
|
362
|
+
}
|
|
363
|
+
throw new Error(`Screen does not contain text ${String(query)} within ${timeout}ms`);
|
|
364
|
+
});
|
|
365
|
+
}
|
|
267
366
|
/**
|
|
268
367
|
* Get all elements
|
|
269
368
|
*/
|