@testmuai/playwright-bindings 0.1.53-beta.1 → 0.1.53-beta.2

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.
@@ -1,29 +1,45 @@
1
1
  /**
2
- * V3 JS-dialog handling — prime a persistent accept/dismiss policy.
2
+ * V4 JS-dialog handling — one-shot answers armed before the trigger.
3
3
  *
4
- * Playwright auto-dismisses a JavaScript dialog (alert / confirm / prompt)
5
- * unless a `page.on('dialog')` listener is registered BEFORE the action that
6
- * opens it. The generator records the dialog-response operation AFTER the
7
- * triggering action (instruction N = trigger, N+1 = JS_DIALOG — the Selenium
8
- * post-hoc order), so a Selenium-style `switch_to.alert` is impossible here:
9
- * by the time the JS_DIALOG op runs the dialog is already gone.
4
+ * Playwright auto-dismisses a JavaScript dialog (alert / confirm / prompt /
5
+ * beforeunload) unless a `dialog` listener exists BEFORE the action that opens
6
+ * it, and while a dialog is open every page call blocks. So the answer is
7
+ * decided up front and given the instant the dialog opens:
10
8
  *
11
- * The fix is a permanent handler primed up front. `primeDialog` registers ONE
12
- * idempotent, page-scoped listener and mutates a per-page policy the listener
13
- * consults for every dialog.
9
+ * - One listener per browser context, so tabs and popups opened later are
10
+ * covered and Playwright's auto-dismiss is off everywhere.
11
+ * - `primeDialog` arms a one-shot answer in a FIFO per context and returns its
12
+ * id. When a dialog opens, the earliest armed entry that fits its type
13
+ * (`expect`; `*` fits any type but beforeunload) answers it and is removed
14
+ * unless `repeat`; an entry for a dialog that never opened therefore never
15
+ * blocks a later one of another type. No fitting entry: the type default,
16
+ * alert accept, confirm dismiss, prompt dismiss, beforeunload accept
17
+ * (Playwright's own outcome).
18
+ * - An answer that fails (another listener took the dialog, the page closed)
19
+ * was not given: its one-shot entry is re-armed.
20
+ *
21
+ * Same behaviour as the Python binding's `prime_dialog`.
14
22
  */
15
23
  import type { Page } from 'playwright';
16
24
  /**
17
- * Arm/refresh the JS-dialog policy for `page` and ensure a live listener.
25
+ * Arm a one-shot answer for the next fitting JS dialog of `page`'s context;
26
+ * returns the entry's id.
18
27
  *
19
- * Idempotent per page: the listener is registered exactly once; later calls
20
- * only update the policy applied to every future dialog.
28
+ * - `action`: `'accept'` or `'dismiss'` (case-insensitive; default dismiss).
29
+ * - `value`: text typed into a prompt when accepting (`''` submits an empty
30
+ * answer); `{{var}}` / `${var}` resolve when the dialog opens.
31
+ * Omitted / `null` uses the prompt's own default value.
32
+ * - `expect`: dialog type the answer is for (`alert`, `confirm`, `prompt`,
33
+ * `beforeunload`); omitted or `'*'` fits any type but beforeunload.
34
+ * - `repeat`: keep the entry armed after it is used.
21
35
  *
22
- * Async so generated code can `await` it uniformly with the other page
23
- * helpers; it performs no I/O itself.
36
+ * Entries are tried in arming order; the first that fits the dialog's type
37
+ * answers it. A dialog no entry fits gets the type default.
24
38
  */
25
39
  export declare function primeDialog(page: Page, opts?: {
26
40
  action?: string;
27
- value?: string;
28
- }): Promise<void>;
41
+ value?: string | null;
42
+ expect?: string | null;
43
+ repeat?: boolean;
44
+ }): Promise<number>;
29
45
  //# sourceMappingURL=dialog.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"dialog.d.ts","sourceRoot":"","sources":["../../../src/v4/helpers/dialog.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AACH,OAAO,KAAK,EAAU,IAAI,EAAE,MAAM,YAAY,CAAC;AAa/C;;;;;;;;GAQG;AACH,wBAAsB,WAAW,CAC/B,IAAI,EAAE,IAAI,EACV,IAAI,GAAE;IAAE,MAAM,CAAC,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAA;CAAO,GAC7C,OAAO,CAAC,IAAI,CAAC,CAsBf"}
1
+ {"version":3,"file":"dialog.d.ts","sourceRoot":"","sources":["../../../src/v4/helpers/dialog.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,OAAO,KAAK,EAA0B,IAAI,EAAE,MAAM,YAAY,CAAC;AA0G/D;;;;;;;;;;;;;;GAcG;AACH,wBAAsB,WAAW,CAC/B,IAAI,EAAE,IAAI,EACV,IAAI,GAAE;IAAE,MAAM,CAAC,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAAC,MAAM,CAAC,EAAE,OAAO,CAAA;CAAO,GAC9F,OAAO,CAAC,MAAM,CAAC,CAejB"}
@@ -1,35 +1,118 @@
1
- const DEFAULT_POLICY = { action: 'dismiss', value: '' };
2
- // Keyed weakly so a closed/GC'd page auto-evicts.
3
- const policies = new WeakMap();
4
- const registered = new WeakMap();
1
+ import { log } from '../../log.js';
2
+ import { resolveVar } from '../vars.js';
3
+ // Answer for a dialog no armed entry fits.
4
+ const TYPE_DEFAULTS = {
5
+ alert: 'accept',
6
+ confirm: 'dismiss',
7
+ prompt: 'dismiss',
8
+ beforeunload: 'accept',
9
+ };
10
+ const PURE_TEMPLATE = /^(?:\{\{[^{}]+\}\}|\$\{[^{}]+\})$/;
11
+ // Keyed weakly by BrowserContext so a closed context evicts itself.
12
+ const contexts = new WeakMap();
13
+ function state(page) {
14
+ const context = page.context();
15
+ let s = contexts.get(context);
16
+ if (!s) {
17
+ const created = { armed: [], nextId: 0 };
18
+ contexts.set(context, created);
19
+ context.on('dialog', (dialog) => {
20
+ // The listener is sync; the answer is async. Never let it reject unhandled:
21
+ // that kills the Node process before the LT status is reported.
22
+ void onDialog(created, dialog).catch(() => { });
23
+ });
24
+ s = created;
25
+ }
26
+ return s;
27
+ }
28
+ function fits(entry, dialogType) {
29
+ // A wildcard never answers beforeunload: a dismiss would cancel the navigation.
30
+ if (entry.expect === '*')
31
+ return dialogType !== 'beforeunload';
32
+ return entry.expect === dialogType;
33
+ }
34
+ async function onDialog(s, dialog) {
35
+ const dialogType = dialog.type();
36
+ const entry = s.armed.find((e) => fits(e, dialogType));
37
+ let used;
38
+ if (entry && !entry.repeat) {
39
+ s.armed.splice(s.armed.indexOf(entry), 1);
40
+ used = entry;
41
+ }
42
+ const action = entry ? entry.action : (TYPE_DEFAULTS[dialogType] ?? 'dismiss');
43
+ log.helper(` [primeDialog] ${action}: type=${dialogType} message=${JSON.stringify(dialog.message())} `
44
+ + `source=${entry ? 'armed' : 'default'}`);
45
+ try {
46
+ if (action === 'accept' && dialogType === 'prompt') {
47
+ const value = entry?.value == null ? dialog.defaultValue() : await resolveValue(entry.value);
48
+ await dialog.accept(value);
49
+ }
50
+ else if (action === 'accept') {
51
+ await dialog.accept();
52
+ }
53
+ else {
54
+ await dialog.dismiss();
55
+ }
56
+ }
57
+ catch (err) {
58
+ log.warn(`[v4] JS dialog answer failed: ${err.message}`);
59
+ if (used) {
60
+ s.armed.push(used);
61
+ s.armed.sort((a, b) => a.id - b.id);
62
+ }
63
+ }
64
+ }
5
65
  /**
6
- * Arm/refresh the JS-dialog policy for `page` and ensure a live listener.
66
+ * Resolve a prompt answer's variable references as the dialog opens. A pure
67
+ * reference with no value is typed as written, so a missing variable shows
68
+ * instead of submitting an empty answer.
69
+ */
70
+ async function resolveValue(value) {
71
+ if (!value.includes('{{') && !value.includes('${'))
72
+ return value;
73
+ let resolved;
74
+ try {
75
+ resolved = await resolveVar(value);
76
+ }
77
+ catch (err) {
78
+ log.warn(`[v4] JS dialog prompt text not resolved: ${err.message}`);
79
+ return value;
80
+ }
81
+ if ((resolved == null || resolved === '') && PURE_TEMPLATE.test(value)) {
82
+ log.warn(`[v4] JS dialog prompt text ${value} has no value; typed as written`);
83
+ return value;
84
+ }
85
+ return String(resolved);
86
+ }
87
+ /**
88
+ * Arm a one-shot answer for the next fitting JS dialog of `page`'s context;
89
+ * returns the entry's id.
7
90
  *
8
- * Idempotent per page: the listener is registered exactly once; later calls
9
- * only update the policy applied to every future dialog.
91
+ * - `action`: `'accept'` or `'dismiss'` (case-insensitive; default dismiss).
92
+ * - `value`: text typed into a prompt when accepting (`''` submits an empty
93
+ * answer); `{{var}}` / `${var}` resolve when the dialog opens.
94
+ * Omitted / `null` uses the prompt's own default value.
95
+ * - `expect`: dialog type the answer is for (`alert`, `confirm`, `prompt`,
96
+ * `beforeunload`); omitted or `'*'` fits any type but beforeunload.
97
+ * - `repeat`: keep the entry armed after it is used.
10
98
  *
11
- * Async so generated code can `await` it uniformly with the other page
12
- * helpers; it performs no I/O itself.
99
+ * Entries are tried in arming order; the first that fits the dialog's type
100
+ * answers it. A dialog no entry fits gets the type default.
13
101
  */
14
102
  export async function primeDialog(page, opts = {}) {
15
- policies.set(page, {
16
- action: (opts.action || 'dismiss').trim().toLowerCase(),
17
- value: opts.value || '',
18
- });
19
- if (registered.get(page))
20
- return;
21
- page.on('dialog', (dialog) => {
22
- const policy = policies.get(page) ?? DEFAULT_POLICY;
23
- const settled = policy.action === 'accept'
24
- ? policy.value
25
- ? dialog.accept(policy.value)
26
- : dialog.accept()
27
- : dialog.dismiss();
28
- // The listener is sync but accept()/dismiss() are async. Swallow rejection
29
- // rather than let it float: an unhandled rejection kills the Node process
30
- // before the LT status is reported, surfacing as "completed" on the grid.
31
- void settled.catch(() => { });
103
+ const action = (opts.action || 'dismiss').trim().toLowerCase();
104
+ if (action !== 'accept' && action !== 'dismiss') {
105
+ throw new Error(`primeDialog action must be 'accept' or 'dismiss', got ${JSON.stringify(opts.action)}`);
106
+ }
107
+ const s = state(page);
108
+ s.nextId += 1;
109
+ s.armed.push({
110
+ id: s.nextId,
111
+ action,
112
+ value: opts.value ?? null,
113
+ expect: (opts.expect || '*').trim().toLowerCase(),
114
+ repeat: Boolean(opts.repeat),
32
115
  });
33
- registered.set(page, true);
116
+ return s.nextId;
34
117
  }
35
118
  //# sourceMappingURL=dialog.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"dialog.js","sourceRoot":"","sources":["../../../src/v4/helpers/dialog.ts"],"names":[],"mappings":"AAqBA,MAAM,cAAc,GAAiB,EAAE,MAAM,EAAE,SAAS,EAAE,KAAK,EAAE,EAAE,EA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I,EAAE,CAAC,WAAW,EAAE,CAAC;IAC/D,IAAI,MAAM,KAAK,QAAQ,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;QAChD,MAAM,IAAI,KAAK,CAAC,yDAAyD,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;IAC1G,CAAC;IACD,MAAM,CAAC,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC;IACtB,CAAC,CAAC,MAAM,IAAI,CAAC,CAAC;IACd,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC;QACX,EAAE,EAAE,CAAC,CAAC,MAAM;QACZ,MAAM;QACN,KAAK,EAAE,IAAI,CAAC,KAAK,IAAI,IAAI;QACzB,MAAM,EAAE,CAAC,IAAI,CAAC,MAAM,IAAI,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC,WAAW,EAAE;QACjD,MAAM,EAAE,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC;KAC7B,CAAC,CAAC;IACH,OAAO,CAAC,CAAC,MAAM,CAAC;AAClB,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@testmuai/playwright-bindings",
3
- "version": "0.1.53-beta.1",
3
+ "version": "0.1.53-beta.2",
4
4
  "description": "Testmu binding for Playwright — thin runtime for LambdaTest exports",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",