@9l/playwright 0.4.8 → 0.5.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/README.md +2 -2
- package/dist/index.d.ts +2 -0
- package/dist/index.js +6 -1
- package/dist/protocol.d.ts +8 -0
- package/dist/protocol.js +16 -1
- package/dist/reporter.d.ts +1 -0
- package/dist/reporter.js +15 -3
- package/dist/targets.d.ts +87 -0
- package/dist/targets.js +273 -0
- package/package.json +6 -1
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@ completing does not verify that behavior.
|
|
|
10
10
|
Published on [npm](https://www.npmjs.com/package/@9l/playwright):
|
|
11
11
|
|
|
12
12
|
```sh
|
|
13
|
-
npm install --save-dev @9l/playwright@0.
|
|
13
|
+
npm install --save-dev @9l/playwright@0.5.0 @playwright/test@1.64.0
|
|
14
14
|
npm exec playwright install chromium
|
|
15
15
|
```
|
|
16
16
|
|
|
@@ -23,7 +23,7 @@ unqualified.
|
|
|
23
23
|
|
|
24
24
|
The Go `9l` executable is also required. Use the
|
|
25
25
|
[CLI installation guide](https://github.com/Quality-Max/9lives-runner/blob/main/docs/install.md)
|
|
26
|
-
for release availability or install from source with Go 1.
|
|
26
|
+
for release availability or install from source with Go 1.27.3 or newer.
|
|
27
27
|
This npm package does not install Go or
|
|
28
28
|
bundle the runner, browsers or a hosted service. No platform account is needed.
|
|
29
29
|
|
package/dist/index.d.ts
CHANGED
|
@@ -11,3 +11,5 @@ export declare const test: import("@playwright/test").TestType<import("@playwrig
|
|
|
11
11
|
}, import("@playwright/test").PlaywrightWorkerArgs & import("@playwright/test").PlaywrightWorkerOptions>;
|
|
12
12
|
export { expect };
|
|
13
13
|
export { protocolVersion } from './protocol';
|
|
14
|
+
export { createTargetTest, TargetApp, webTargetDriver, targetCapabilities } from './targets';
|
|
15
|
+
export type { TargetDriver, TargetHandle, TargetIdentity, TargetContext, TargetState, TargetFixtures, TargetOperationOptions } from './targets';
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.protocolVersion = exports.expect = exports.test = void 0;
|
|
3
|
+
exports.targetCapabilities = exports.webTargetDriver = exports.TargetApp = exports.createTargetTest = exports.protocolVersion = exports.expect = exports.test = void 0;
|
|
4
4
|
const test_1 = require("@playwright/test");
|
|
5
5
|
Object.defineProperty(exports, "expect", { enumerable: true, get: function () { return test_1.expect; } });
|
|
6
6
|
const protocol_1 = require("./protocol");
|
|
@@ -32,3 +32,8 @@ exports.test = test_1.test.extend({
|
|
|
32
32
|
});
|
|
33
33
|
var protocol_2 = require("./protocol");
|
|
34
34
|
Object.defineProperty(exports, "protocolVersion", { enumerable: true, get: function () { return protocol_2.protocolVersion; } });
|
|
35
|
+
var targets_1 = require("./targets");
|
|
36
|
+
Object.defineProperty(exports, "createTargetTest", { enumerable: true, get: function () { return targets_1.createTargetTest; } });
|
|
37
|
+
Object.defineProperty(exports, "TargetApp", { enumerable: true, get: function () { return targets_1.TargetApp; } });
|
|
38
|
+
Object.defineProperty(exports, "webTargetDriver", { enumerable: true, get: function () { return targets_1.webTargetDriver; } });
|
|
39
|
+
Object.defineProperty(exports, "targetCapabilities", { enumerable: true, get: function () { return targets_1.targetCapabilities; } });
|
package/dist/protocol.d.ts
CHANGED
|
@@ -2,6 +2,14 @@ export declare const protocolVersion = "9l.engine/1";
|
|
|
2
2
|
export declare const maxFrameBytes: number;
|
|
3
3
|
export declare const maxEvents = 10000;
|
|
4
4
|
export declare const capabilities: readonly ["steps", "artifact-metadata", "terminal-outcomes"];
|
|
5
|
+
export declare const targetVersion = "9l.target/1";
|
|
6
|
+
export declare const targetStepFile: string;
|
|
7
|
+
export declare function selectedTarget(env?: NodeJS.ProcessEnv): {
|
|
8
|
+
name: string;
|
|
9
|
+
kind: "web" | "android-fake";
|
|
10
|
+
appId: string;
|
|
11
|
+
id: string;
|
|
12
|
+
};
|
|
5
13
|
export declare function engineIdentity(env?: NodeJS.ProcessEnv): {
|
|
6
14
|
version: string;
|
|
7
15
|
runId: string;
|
package/dist/protocol.js
CHANGED
|
@@ -1,15 +1,30 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.capabilities = exports.maxEvents = exports.maxFrameBytes = exports.protocolVersion = void 0;
|
|
3
|
+
exports.targetStepFile = exports.targetVersion = exports.capabilities = exports.maxEvents = exports.maxFrameBytes = exports.protocolVersion = void 0;
|
|
4
|
+
exports.selectedTarget = selectedTarget;
|
|
4
5
|
exports.engineIdentity = engineIdentity;
|
|
5
6
|
exports.engineEventsPath = engineEventsPath;
|
|
6
7
|
exports.skipPinKey = skipPinKey;
|
|
7
8
|
const node_crypto_1 = require("node:crypto");
|
|
9
|
+
const node_fs_1 = require("node:fs");
|
|
8
10
|
const node_path_1 = require("node:path");
|
|
9
11
|
exports.protocolVersion = '9l.engine/1';
|
|
10
12
|
exports.maxFrameBytes = 16 * 1024;
|
|
11
13
|
exports.maxEvents = 10000;
|
|
12
14
|
exports.capabilities = ['steps', 'artifact-metadata', 'terminal-outcomes'];
|
|
15
|
+
exports.targetVersion = '9l.target/1';
|
|
16
|
+
// Typed target steps carry this module's own location. The reporter counts only
|
|
17
|
+
// those, so a user step titled "9l target ..." stays an ordinary step. Code in the
|
|
18
|
+
// worker can still forge one deliberately; the worker remains trusted.
|
|
19
|
+
exports.targetStepFile = (0, node_fs_1.realpathSync)((0, node_path_1.join)(__dirname, 'targets.js'));
|
|
20
|
+
function selectedTarget(env = process.env) {
|
|
21
|
+
const name = env.NINELIVES_TARGET;
|
|
22
|
+
if (!name || !/^(web|android-fake):[a-zA-Z0-9_-]{1,64}$/.test(name)) {
|
|
23
|
+
throw new Error('9lives target requires --sdk --target web:<app-alias> or android-fake:<app-alias>');
|
|
24
|
+
}
|
|
25
|
+
const [kind, appId] = name.split(':');
|
|
26
|
+
return { name, kind, appId, id: (0, node_crypto_1.createHash)('sha256').update(name).digest('hex') };
|
|
27
|
+
}
|
|
13
28
|
function engineIdentity(env = process.env) {
|
|
14
29
|
if (env.NINELIVES_ENGINE_PROTOCOL !== exports.protocolVersion) {
|
|
15
30
|
throw new Error('9lives SDK requires a supported Go engine; run this spec with 9l run --sdk');
|
package/dist/reporter.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { Reporter, FullConfig, Suite, TestCase, TestResult, TestStep, FullResult } from '@playwright/test/reporter';
|
|
2
2
|
export default class EngineReporter implements Reporter {
|
|
3
3
|
private identity;
|
|
4
|
+
private target;
|
|
4
5
|
private events;
|
|
5
6
|
private seq;
|
|
6
7
|
private stepSeq;
|
package/dist/reporter.js
CHANGED
|
@@ -7,6 +7,7 @@ const protocol_1 = require("./protocol");
|
|
|
7
7
|
// the protocol. IDs are hashes; Go persists only this bounded metadata stream.
|
|
8
8
|
class EngineReporter {
|
|
9
9
|
identity = (0, protocol_1.engineIdentity)();
|
|
10
|
+
target = process.env.NINELIVES_TARGET ? (0, protocol_1.selectedTarget)() : undefined;
|
|
10
11
|
// Exclusive create: the file must be new for this attempt, never appended to.
|
|
11
12
|
events = (0, node_fs_1.openSync)((0, protocol_1.engineEventsPath)(), 'wx', 0o600);
|
|
12
13
|
seq = 0;
|
|
@@ -24,7 +25,7 @@ class EngineReporter {
|
|
|
24
25
|
(0, node_fs_1.writeSync)(this.events, JSON.stringify({ ...this.identity, seq: ++this.seq, type: 'engine_error' }) + '\n');
|
|
25
26
|
return;
|
|
26
27
|
}
|
|
27
|
-
const frame = JSON.stringify({ ...this.identity, seq: ++this.seq, type, ...fields }) + '\n';
|
|
28
|
+
const frame = JSON.stringify({ ...this.identity, ...(this.target ? { targetId: this.target.id } : {}), seq: ++this.seq, type, ...fields }) + '\n';
|
|
28
29
|
if (Buffer.byteLength(frame) > protocol_1.maxFrameBytes) {
|
|
29
30
|
this.overflow = true;
|
|
30
31
|
(0, node_fs_1.writeSync)(this.events, JSON.stringify({ ...this.identity, seq: this.seq, type: 'engine_error' }) + '\n');
|
|
@@ -34,16 +35,27 @@ class EngineReporter {
|
|
|
34
35
|
}
|
|
35
36
|
onBegin(_config, suite) {
|
|
36
37
|
this.suite = suite;
|
|
37
|
-
this.emit('hello', { capabilities: protocol_1.capabilities, totalTests: suite.allTests().length });
|
|
38
|
+
this.emit('hello', { capabilities: this.target ? [...protocol_1.capabilities, protocol_1.targetVersion] : protocol_1.capabilities, totalTests: suite.allTests().length, ...(this.target ? { targetVersion: protocol_1.targetVersion } : {}) });
|
|
39
|
+
if (this.target) {
|
|
40
|
+
for (const test of suite.allTests()) {
|
|
41
|
+
if (test.parent.project()?.name !== this.target.name) {
|
|
42
|
+
this.emit('engine_error');
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
this.emit('required_test', { testId: this.testId(test) });
|
|
46
|
+
}
|
|
47
|
+
}
|
|
38
48
|
}
|
|
39
49
|
onTestBegin(test, result) {
|
|
40
50
|
this.emit('test_begin', { testId: this.testId(test), retry: result.retry });
|
|
41
51
|
}
|
|
42
52
|
onStepEnd(test, result, step) {
|
|
43
53
|
const categories = { 'test.step': 'action', expect: 'assertion', 'pw:api': 'browser', fixture: 'fixture' };
|
|
54
|
+
const operation = this.target && step.category === 'test.step' && step.location?.file === protocol_1.targetStepFile && step.title.startsWith('9l target ') ? step.title.slice('9l target '.length) : undefined;
|
|
44
55
|
this.emit('step_end', {
|
|
45
56
|
testId: this.testId(test), retry: result.retry, stepId: `step-${++this.stepSeq}`,
|
|
46
|
-
category: step.category === 'test.step' && step.title === '9l goal' ? 'goal' : categories[step.category] ?? 'other', status: step.error ? 'failed' : 'passed',
|
|
57
|
+
category: operation ? operation.startsWith('assert-') ? 'assertion' : 'action' : step.category === 'test.step' && step.title === '9l goal' ? 'goal' : categories[step.category] ?? 'other', status: step.error ? 'failed' : 'passed',
|
|
58
|
+
...(operation ? { operation } : {}),
|
|
47
59
|
});
|
|
48
60
|
}
|
|
49
61
|
onTestEnd(test, result) {
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { type Page } from '@playwright/test';
|
|
2
|
+
import { targetVersion } from './protocol';
|
|
3
|
+
export declare const targetCapabilities: readonly ["open", "reset", "test-id", "tap", "fill", "visible", "text", "state"];
|
|
4
|
+
export type TargetIdentity = {
|
|
5
|
+
kind: 'web' | 'android-fake';
|
|
6
|
+
appId: string;
|
|
7
|
+
};
|
|
8
|
+
export type TargetContext = {
|
|
9
|
+
signal: AbortSignal;
|
|
10
|
+
deadlineMs: number;
|
|
11
|
+
};
|
|
12
|
+
export type TargetState = 'enabled' | 'checked';
|
|
13
|
+
/** Per-call bound, capped by Go's worker deadline. */
|
|
14
|
+
export type TargetOperationOptions = {
|
|
15
|
+
timeoutMs?: number;
|
|
16
|
+
};
|
|
17
|
+
/** Handles stay in the worker. Drivers must stop pending work on abort/close,
|
|
18
|
+
* honor the context deadline, must not retry mutations, and must never act on
|
|
19
|
+
* an arbitrary first match. */
|
|
20
|
+
export interface TargetHandle {
|
|
21
|
+
tap(context: TargetContext): Promise<void>;
|
|
22
|
+
fill(value: string, context: TargetContext): Promise<void>;
|
|
23
|
+
visible(context: TargetContext): Promise<boolean>;
|
|
24
|
+
text(context: TargetContext): Promise<string>;
|
|
25
|
+
state(name: TargetState, context: TargetContext): Promise<boolean>;
|
|
26
|
+
}
|
|
27
|
+
export interface TargetDriver extends TargetIdentity {
|
|
28
|
+
version: typeof targetVersion;
|
|
29
|
+
capabilities: readonly string[];
|
|
30
|
+
open(context: TargetContext): Promise<void>;
|
|
31
|
+
reset(context: TargetContext): Promise<void>;
|
|
32
|
+
/** Observe at most two matches; two means ambiguous and must fail. */
|
|
33
|
+
observeTestId(id: string, context: TargetContext): Promise<readonly TargetHandle[]>;
|
|
34
|
+
close(): Promise<void>;
|
|
35
|
+
}
|
|
36
|
+
/** Deterministic shared operations. No semantic judge, replay or native driver. */
|
|
37
|
+
export declare class TargetApp {
|
|
38
|
+
private driver;
|
|
39
|
+
private options;
|
|
40
|
+
private controller;
|
|
41
|
+
private timer;
|
|
42
|
+
private busy;
|
|
43
|
+
private reason;
|
|
44
|
+
private closing?;
|
|
45
|
+
private externalAbort;
|
|
46
|
+
constructor(driver: TargetDriver, expected: TargetIdentity, options: {
|
|
47
|
+
deadlineMs: number;
|
|
48
|
+
signal?: AbortSignal;
|
|
49
|
+
});
|
|
50
|
+
/** Stop the session permanently. Releasing closes the owned driver now, which
|
|
51
|
+
* also cancels transports without AbortSignal support (notably Playwright
|
|
52
|
+
* actions). A settled failure keeps the driver until teardown, so Playwright
|
|
53
|
+
* can still capture failure artifacts. */
|
|
54
|
+
private stop;
|
|
55
|
+
private release;
|
|
56
|
+
close(): Promise<void>;
|
|
57
|
+
/** Permanently stop this session, including a pending mutation. */
|
|
58
|
+
cancel(): void;
|
|
59
|
+
private perform;
|
|
60
|
+
open(options?: TargetOperationOptions): Promise<void>;
|
|
61
|
+
reset(options?: TargetOperationOptions): Promise<void>;
|
|
62
|
+
getByTestId(id: string): {
|
|
63
|
+
tap: (options?: TargetOperationOptions) => Promise<any>;
|
|
64
|
+
fill: (value: string, options?: TargetOperationOptions) => Promise<void>;
|
|
65
|
+
toBeVisible: (timeoutMs?: number) => Promise<void>;
|
|
66
|
+
toHaveText: (value: string, timeoutMs?: number) => Promise<void>;
|
|
67
|
+
toHaveState: (name: TargetState, value: boolean, timeoutMs?: number) => Promise<void>;
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
/** Ordinary Playwright handles implement the web side of the shared contract. */
|
|
71
|
+
export declare function webTargetDriver(page: Page, appId: string, lifecycle: {
|
|
72
|
+
open(page: Page, context: TargetContext): Promise<void>;
|
|
73
|
+
reset(page: Page, context: TargetContext): Promise<void>;
|
|
74
|
+
}): TargetDriver;
|
|
75
|
+
export type TargetFixtures = {
|
|
76
|
+
app: TargetApp;
|
|
77
|
+
};
|
|
78
|
+
type TargetOptions = {
|
|
79
|
+
web?: Parameters<typeof webTargetDriver>[2];
|
|
80
|
+
/** Construct only; acquire resources in the driver's bounded open method. */
|
|
81
|
+
native?: (target: TargetIdentity) => TargetDriver;
|
|
82
|
+
};
|
|
83
|
+
/** Go selects one configured project per job. The native fixture has no page,
|
|
84
|
+
* context or browser dependency, including when the default config uses them.
|
|
85
|
+
* Specs load without 9l (listing, editors); using `app` requires 9l run --sdk --target. */
|
|
86
|
+
export declare function createTargetTest(options: TargetOptions): import("@playwright/test").TestType<import("@playwright/test").PlaywrightTestArgs & import("@playwright/test").PlaywrightTestOptions & TargetFixtures, import("@playwright/test").PlaywrightWorkerArgs & import("@playwright/test").PlaywrightWorkerOptions>;
|
|
87
|
+
export {};
|
package/dist/targets.js
ADDED
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TargetApp = exports.targetCapabilities = void 0;
|
|
4
|
+
exports.webTargetDriver = webTargetDriver;
|
|
5
|
+
exports.createTargetTest = createTargetTest;
|
|
6
|
+
const test_1 = require("@playwright/test");
|
|
7
|
+
const promises_1 = require("node:timers/promises");
|
|
8
|
+
const protocol_1 = require("./protocol");
|
|
9
|
+
exports.targetCapabilities = ['open', 'reset', 'test-id', 'tap', 'fill', 'visible', 'text', 'state'];
|
|
10
|
+
const defaultOperationMs = 5_000;
|
|
11
|
+
const defaultAssertionMs = 5_000;
|
|
12
|
+
const maxOperationMs = 60_000;
|
|
13
|
+
const maxObservations = 200;
|
|
14
|
+
// An operation is aborted and rejected at its deadline. If it is still pending
|
|
15
|
+
// after this grace, the session closes its driver to cancel it.
|
|
16
|
+
const settleMs = 250;
|
|
17
|
+
const stepLocation = { file: protocol_1.targetStepFile, line: 1, column: 1 };
|
|
18
|
+
class TargetStopped extends Error {
|
|
19
|
+
reason;
|
|
20
|
+
constructor(reason, cause) {
|
|
21
|
+
super(`9lives target stopped: ${reason}`, cause === undefined ? undefined : { cause });
|
|
22
|
+
this.reason = reason;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
const stopped = (reason, cause) => new TargetStopped(reason, cause);
|
|
26
|
+
const boundedText = (value, max) => typeof value === 'string' && Buffer.byteLength(value) <= max;
|
|
27
|
+
/** Deterministic shared operations. No semantic judge, replay or native driver. */
|
|
28
|
+
class TargetApp {
|
|
29
|
+
driver;
|
|
30
|
+
options;
|
|
31
|
+
controller = new AbortController();
|
|
32
|
+
timer;
|
|
33
|
+
busy = false;
|
|
34
|
+
reason = 'canceled';
|
|
35
|
+
closing;
|
|
36
|
+
externalAbort = () => this.stop('canceled');
|
|
37
|
+
constructor(driver, expected, options) {
|
|
38
|
+
this.driver = driver;
|
|
39
|
+
this.options = options;
|
|
40
|
+
if (driver.version !== protocol_1.targetVersion || driver.kind !== expected.kind || driver.appId !== expected.appId ||
|
|
41
|
+
driver.capabilities.length !== exports.targetCapabilities.length || driver.capabilities.some((cap, index) => cap !== exports.targetCapabilities[index])) {
|
|
42
|
+
throw stopped('unsupported_driver_contract');
|
|
43
|
+
}
|
|
44
|
+
if (!Number.isSafeInteger(options.deadlineMs))
|
|
45
|
+
throw stopped('invalid_deadline');
|
|
46
|
+
this.timer = setTimeout(() => this.stop('deadline'), Math.max(0, Math.min(2 ** 31 - 1, options.deadlineMs - Date.now())));
|
|
47
|
+
options.signal?.addEventListener('abort', this.externalAbort, { once: true });
|
|
48
|
+
if (options.signal?.aborted || options.deadlineMs <= Date.now())
|
|
49
|
+
this.stop(options.signal?.aborted ? 'canceled' : 'deadline');
|
|
50
|
+
}
|
|
51
|
+
/** Stop the session permanently. Releasing closes the owned driver now, which
|
|
52
|
+
* also cancels transports without AbortSignal support (notably Playwright
|
|
53
|
+
* actions). A settled failure keeps the driver until teardown, so Playwright
|
|
54
|
+
* can still capture failure artifacts. */
|
|
55
|
+
stop(reason, release = true) {
|
|
56
|
+
if (!this.controller.signal.aborted) {
|
|
57
|
+
this.reason = reason;
|
|
58
|
+
this.controller.abort();
|
|
59
|
+
}
|
|
60
|
+
if (release)
|
|
61
|
+
this.release();
|
|
62
|
+
}
|
|
63
|
+
release() {
|
|
64
|
+
if (this.closing)
|
|
65
|
+
return;
|
|
66
|
+
this.closing = Promise.resolve().then(() => this.driver.close());
|
|
67
|
+
void this.closing.catch(() => { });
|
|
68
|
+
}
|
|
69
|
+
async close() {
|
|
70
|
+
clearTimeout(this.timer);
|
|
71
|
+
this.options.signal?.removeEventListener('abort', this.externalAbort);
|
|
72
|
+
this.stop('closed');
|
|
73
|
+
try {
|
|
74
|
+
await this.closing;
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
throw stopped('cleanup_failed');
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/** Permanently stop this session, including a pending mutation. */
|
|
81
|
+
cancel() { this.stop('canceled'); }
|
|
82
|
+
async perform(operation, body, timeoutMs = defaultOperationMs) {
|
|
83
|
+
// The SDK location marks this as a typed target step for the reporter.
|
|
84
|
+
return test_1.test.step(`9l target ${operation}`, async () => {
|
|
85
|
+
if (this.controller.signal.aborted)
|
|
86
|
+
throw stopped(this.reason);
|
|
87
|
+
if (this.busy) {
|
|
88
|
+
this.stop('concurrent_operation');
|
|
89
|
+
throw stopped(this.reason);
|
|
90
|
+
}
|
|
91
|
+
if (!Number.isSafeInteger(timeoutMs) || timeoutMs < 1 || timeoutMs > maxOperationMs) {
|
|
92
|
+
this.stop('invalid_timeout', false);
|
|
93
|
+
throw stopped(this.reason);
|
|
94
|
+
}
|
|
95
|
+
this.busy = true;
|
|
96
|
+
const deadlineMs = Math.min(this.options.deadlineMs, Date.now() + timeoutMs);
|
|
97
|
+
const signal = this.controller.signal;
|
|
98
|
+
const work = Promise.resolve().then(() => { signal.throwIfAborted(); return body({ signal, deadlineMs }); });
|
|
99
|
+
let settled = false;
|
|
100
|
+
void work.then(() => { settled = true; }, () => { settled = true; });
|
|
101
|
+
// Abort and reject at the deadline: a late result never succeeds. The
|
|
102
|
+
// grace only decides whether to close the driver, if work is still pending.
|
|
103
|
+
const timer = setTimeout(() => {
|
|
104
|
+
this.stop('deadline', false);
|
|
105
|
+
setTimeout(() => { if (!settled)
|
|
106
|
+
this.release(); }, settleMs);
|
|
107
|
+
}, Math.max(0, deadlineMs - Date.now()));
|
|
108
|
+
let abort = () => { };
|
|
109
|
+
try {
|
|
110
|
+
return await Promise.race([
|
|
111
|
+
work,
|
|
112
|
+
new Promise((_, reject) => {
|
|
113
|
+
abort = () => reject(stopped(this.reason));
|
|
114
|
+
signal.addEventListener('abort', abort, { once: true });
|
|
115
|
+
if (signal.aborted)
|
|
116
|
+
abort();
|
|
117
|
+
}),
|
|
118
|
+
]);
|
|
119
|
+
}
|
|
120
|
+
catch (error) {
|
|
121
|
+
// Keep the first reason (deadline, cancellation, a missing control...)
|
|
122
|
+
// and the local cause. Neither crosses the engine bridge.
|
|
123
|
+
if (signal.aborted)
|
|
124
|
+
throw error instanceof TargetStopped ? error : stopped(this.reason, error);
|
|
125
|
+
this.stop(error instanceof TargetStopped ? error.reason : 'operation_failed', false);
|
|
126
|
+
throw error instanceof TargetStopped ? error : stopped(this.reason, error);
|
|
127
|
+
}
|
|
128
|
+
finally {
|
|
129
|
+
clearTimeout(timer);
|
|
130
|
+
signal.removeEventListener('abort', abort);
|
|
131
|
+
this.busy = false;
|
|
132
|
+
}
|
|
133
|
+
}, { location: stepLocation });
|
|
134
|
+
}
|
|
135
|
+
open(options = {}) { return this.perform('open', context => this.driver.open(context), options.timeoutMs); }
|
|
136
|
+
reset(options = {}) { return this.perform('reset', context => this.driver.reset(context), options.timeoutMs); }
|
|
137
|
+
getByTestId(id) {
|
|
138
|
+
const observe = async (context) => {
|
|
139
|
+
if (!boundedText(id, 128) || !id || /[\x00-\x1f]/.test(id))
|
|
140
|
+
throw stopped('invalid_test_id');
|
|
141
|
+
const matches = await this.driver.observeTestId(id, context);
|
|
142
|
+
context.signal.throwIfAborted();
|
|
143
|
+
if (!Array.isArray(matches) || matches.length > 1)
|
|
144
|
+
throw stopped('ambiguous_test_id');
|
|
145
|
+
return matches[0];
|
|
146
|
+
};
|
|
147
|
+
const required = async (context) => {
|
|
148
|
+
const handle = await observe(context);
|
|
149
|
+
if (!handle)
|
|
150
|
+
throw stopped('missing_test_id');
|
|
151
|
+
return handle;
|
|
152
|
+
};
|
|
153
|
+
const assertion = (operation, check, timeoutMs = defaultAssertionMs) => this.perform(operation, async (context) => {
|
|
154
|
+
// At most maxObservations polls within the bound; the poll ends before
|
|
155
|
+
// the deadline so a false assertion fails by itself.
|
|
156
|
+
const interval = Math.max(25, Math.ceil(timeoutMs / maxObservations));
|
|
157
|
+
let last;
|
|
158
|
+
for (let poll = 0; poll < maxObservations; poll++) {
|
|
159
|
+
context.signal.throwIfAborted();
|
|
160
|
+
try {
|
|
161
|
+
const handle = await observe(context);
|
|
162
|
+
if (handle && await check(handle, context))
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
catch (error) {
|
|
166
|
+
// Ambiguity, invalid input and cancellation are not transient.
|
|
167
|
+
if (error instanceof TargetStopped || context.signal.aborted)
|
|
168
|
+
throw error;
|
|
169
|
+
last = error;
|
|
170
|
+
}
|
|
171
|
+
if (Date.now() + interval >= context.deadlineMs)
|
|
172
|
+
break;
|
|
173
|
+
await (0, promises_1.setTimeout)(interval, undefined, { signal: context.signal });
|
|
174
|
+
}
|
|
175
|
+
throw stopped('assertion_failed', last);
|
|
176
|
+
}, timeoutMs);
|
|
177
|
+
return {
|
|
178
|
+
tap: (options = {}) => this.perform('tap', async (context) => (await required(context)).tap(context), options.timeoutMs),
|
|
179
|
+
fill: (value, options = {}) => this.perform('fill', async (context) => {
|
|
180
|
+
if (!boundedText(value, 4096))
|
|
181
|
+
throw stopped('invalid_value');
|
|
182
|
+
await (await required(context)).fill(value, context);
|
|
183
|
+
}, options.timeoutMs),
|
|
184
|
+
toBeVisible: (timeoutMs) => assertion('assert-visible', (handle, context) => handle.visible(context), timeoutMs),
|
|
185
|
+
toHaveText: (value, timeoutMs) => {
|
|
186
|
+
if (!boundedText(value, 4096))
|
|
187
|
+
return assertion('assert-text', async () => { throw stopped('invalid_value'); }, timeoutMs);
|
|
188
|
+
return assertion('assert-text', async (handle, context) => {
|
|
189
|
+
const actual = await handle.text(context);
|
|
190
|
+
return boundedText(actual, 4096) && actual === value;
|
|
191
|
+
}, timeoutMs);
|
|
192
|
+
},
|
|
193
|
+
toHaveState: (name, value, timeoutMs) => {
|
|
194
|
+
if (!['enabled', 'checked'].includes(name) || typeof value !== 'boolean')
|
|
195
|
+
return assertion('assert-state', async () => { throw stopped('unsupported_state'); }, timeoutMs);
|
|
196
|
+
return assertion('assert-state', async (handle, context) => await handle.state(name, context) === value, timeoutMs);
|
|
197
|
+
},
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
exports.TargetApp = TargetApp;
|
|
202
|
+
const remaining = (context) => { context.signal.throwIfAborted(); return Math.max(1, context.deadlineMs - Date.now()); };
|
|
203
|
+
function locatorHandle(locator) {
|
|
204
|
+
// Strict locators re-resolve a replaced node (re-renders, hydration) but
|
|
205
|
+
// throw rather than choose when more than one element matches.
|
|
206
|
+
return {
|
|
207
|
+
tap: async (context) => { await locator.click({ timeout: remaining(context) }); },
|
|
208
|
+
fill: async (value, context) => { await locator.fill(value, { timeout: remaining(context) }); },
|
|
209
|
+
visible: async (context) => { remaining(context); return locator.isVisible(); },
|
|
210
|
+
text: async (context) => await locator.textContent({ timeout: remaining(context) }) ?? '',
|
|
211
|
+
state: async (name, context) => name === 'enabled' ? locator.isEnabled({ timeout: remaining(context) }) : locator.isChecked({ timeout: remaining(context) }),
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
/** Ordinary Playwright handles implement the web side of the shared contract. */
|
|
215
|
+
function webTargetDriver(page, appId, lifecycle) {
|
|
216
|
+
return {
|
|
217
|
+
version: protocol_1.targetVersion, capabilities: exports.targetCapabilities, kind: 'web', appId,
|
|
218
|
+
open: context => lifecycle.open(page, context), reset: context => lifecycle.reset(page, context),
|
|
219
|
+
close: () => page.close({ runBeforeUnload: false }),
|
|
220
|
+
observeTestId: async (id, context) => {
|
|
221
|
+
remaining(context);
|
|
222
|
+
const locator = page.getByTestId(id);
|
|
223
|
+
const count = await locator.count();
|
|
224
|
+
const handle = locatorHandle(locator);
|
|
225
|
+
return count === 0 ? [] : count === 1 ? [handle] : [handle, handle];
|
|
226
|
+
},
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
/** Go selects one configured project per job. The native fixture has no page,
|
|
230
|
+
* context or browser dependency, including when the default config uses them.
|
|
231
|
+
* Specs load without 9l (listing, editors); using `app` requires 9l run --sdk --target. */
|
|
232
|
+
function createTargetTest(options) {
|
|
233
|
+
const resolve = () => { (0, protocol_1.engineIdentity)(); return (0, protocol_1.selectedTarget)(); };
|
|
234
|
+
const useApp = async (target, driver, use) => {
|
|
235
|
+
let app;
|
|
236
|
+
try {
|
|
237
|
+
app = new TargetApp(driver, target, { deadlineMs: Number(process.env.NINELIVES_TARGET_DEADLINE_MS) });
|
|
238
|
+
await use(app);
|
|
239
|
+
}
|
|
240
|
+
finally {
|
|
241
|
+
if (app)
|
|
242
|
+
await app.close();
|
|
243
|
+
else {
|
|
244
|
+
try {
|
|
245
|
+
await driver.close();
|
|
246
|
+
}
|
|
247
|
+
catch {
|
|
248
|
+
throw stopped('cleanup_failed');
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
const owned = (target, info) => {
|
|
254
|
+
if (info.retry !== 0 || info.project.name !== target.name)
|
|
255
|
+
throw stopped('repeated_or_foreign_target');
|
|
256
|
+
};
|
|
257
|
+
if (process.env.NINELIVES_TARGET?.startsWith('web:')) {
|
|
258
|
+
return test_1.test.extend({ app: async ({ page }, use, info) => {
|
|
259
|
+
const target = resolve();
|
|
260
|
+
owned(target, info);
|
|
261
|
+
if (!options.web)
|
|
262
|
+
throw stopped('unsupported_target');
|
|
263
|
+
await useApp(target, webTargetDriver(page, target.appId, options.web), use);
|
|
264
|
+
} });
|
|
265
|
+
}
|
|
266
|
+
return test_1.test.extend({ app: async ({}, use, info) => {
|
|
267
|
+
const target = resolve();
|
|
268
|
+
owned(target, info);
|
|
269
|
+
if (target.kind !== 'android-fake' || !options.native)
|
|
270
|
+
throw stopped('unsupported_target');
|
|
271
|
+
await useApp(target, options.native(target), use);
|
|
272
|
+
} });
|
|
273
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@9l/playwright",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "Playwright fixtures and the versioned 9lives Go engine bridge",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"homepage": "https://github.com/Quality-Max/9lives-runner#local-playwright-sdk",
|
|
@@ -21,5 +21,10 @@
|
|
|
21
21
|
"scripts": {"build": "tsc -p tsconfig.json"},
|
|
22
22
|
"peerDependencies": {"@playwright/test": ">=1.61.1 <2"},
|
|
23
23
|
"engines": {"node": ">=22"},
|
|
24
|
+
"9l": {
|
|
25
|
+
"protocol": "9l.engine/1",
|
|
26
|
+
"capabilities": ["steps", "artifact-metadata", "terminal-outcomes"],
|
|
27
|
+
"faults": ["abort", "http-500", "empty-json", "http-401", "http-403", "http-429", "malformed-json"]
|
|
28
|
+
},
|
|
24
29
|
"publishConfig": {"access": "public"}
|
|
25
30
|
}
|