@kuafucloud/product-test 0.0.0-stage → 0.2.2-alpha.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.
- package/LICENSE.md +9 -0
- package/assets/admin-test-host/renderer.mjs +58 -0
- package/assets/workspace.config.mjs +85 -0
- package/dist/e2e/define-e2e.d.ts +88 -0
- package/dist/e2e/define-e2e.js +209 -0
- package/dist/e2e/define-product-flow.d.ts +109 -0
- package/dist/e2e/define-product-flow.js +498 -0
- package/dist/e2e/index.d.ts +12 -0
- package/dist/e2e/index.js +16 -0
- package/dist/e2e/manifest-reporter.d.ts +11 -0
- package/dist/e2e/manifest-reporter.js +193 -0
- package/dist/e2e/manifest-rules.d.ts +107 -0
- package/dist/e2e/manifest-rules.js +69 -0
- package/dist/e2e/product-flow-rules.d.ts +10 -0
- package/dist/e2e/product-flow-rules.js +95 -0
- package/dist/e2e/support/admin-test-host-rules.d.ts +34 -0
- package/dist/e2e/support/admin-test-host-rules.js +195 -0
- package/dist/e2e/support/admin-test-host.d.ts +24 -0
- package/dist/e2e/support/admin-test-host.js +220 -0
- package/dist/e2e/support/auth-session-seed.d.ts +7 -0
- package/dist/e2e/support/auth-session-seed.js +22 -0
- package/dist/e2e/support/auth.d.ts +44 -0
- package/dist/e2e/support/auth.js +351 -0
- package/dist/e2e/support/extension-artifact-fixture.d.ts +29 -0
- package/dist/e2e/support/extension-artifact-fixture.js +139 -0
- package/dist/e2e/support/fixtures.d.ts +9 -0
- package/dist/e2e/support/fixtures.js +41 -0
- package/dist/e2e/support/local-platform-session-rules.d.ts +3 -0
- package/dist/e2e/support/local-platform-session-rules.js +8 -0
- package/dist/e2e/support/local-platform-session.d.ts +9 -0
- package/dist/e2e/support/local-platform-session.js +16 -0
- package/dist/e2e/support/playwright.d.ts +5 -0
- package/dist/e2e/support/playwright.js +16 -0
- package/dist/e2e/support/process.d.ts +10 -0
- package/dist/e2e/support/process.js +45 -0
- package/dist/e2e/support/runtime-rules.d.ts +14 -0
- package/dist/e2e/support/runtime-rules.js +48 -0
- package/dist/e2e/support/runtime.d.ts +14 -0
- package/dist/e2e/support/runtime.js +28 -0
- package/dist/e2e/support/selectors.d.ts +2 -0
- package/dist/e2e/support/selectors.js +7 -0
- package/dist/e2e/support/sensitive-input.d.ts +13 -0
- package/dist/e2e/support/sensitive-input.js +78 -0
- package/dist/e2e/support/workspace-browser-recording.d.ts +18 -0
- package/dist/e2e/support/workspace-browser-recording.js +209 -0
- package/dist/playground/index.d.ts +19 -0
- package/dist/playground/index.js +44 -0
- package/dist/playground/prepare.d.ts +13 -0
- package/dist/playground/prepare.js +22 -0
- package/dist/playground/runtime-artifacts.d.ts +8 -0
- package/dist/playground/runtime-artifacts.js +46 -0
- package/dist/playground/server.d.ts +23 -0
- package/dist/playground/server.js +118 -0
- package/dist/shared/index.d.ts +43 -0
- package/dist/shared/index.js +84 -0
- package/dist/shared/workspace-paths.d.ts +3 -0
- package/dist/shared/workspace-paths.js +26 -0
- package/dist/types/playwright/LICENSE +202 -0
- package/dist/types/playwright/NOTICE +5 -0
- package/dist/types/playwright/protocol.d.ts +24720 -0
- package/dist/types/playwright/structs.d.ts +45 -0
- package/dist/types/playwright/test.d.ts +10322 -0
- package/dist/types/playwright/testReporter.d.ts +822 -0
- package/dist/types/playwright/types.d.ts +23623 -0
- package/dist/unit/index.d.ts +52 -0
- package/dist/unit/index.js +388 -0
- package/package.json +110 -4
- package/skills/kuafu-testing/SKILL.md +33 -0
- package/README.md +0 -3
package/LICENSE.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# KuafuCloud Proprietary Platform License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 KuafuCloud. All rights reserved.
|
|
4
|
+
|
|
5
|
+
This package is published solely to support the KuafuCloud platform, official KuafuCloud tooling, and applications developed and operated on the KuafuCloud platform.
|
|
6
|
+
|
|
7
|
+
1. Permitted Use: You may install and execute this package only as a dependency of projects developed with or running on the KuafuCloud platform.
|
|
8
|
+
2. Restrictions: You may not redistribute, sublicense, fork and republish, reverse-engineer for competing platforms, or use this package outside the KuafuCloud platform without prior written permission from KuafuCloud.
|
|
9
|
+
3. No Warranty: This software is provided "as is", without warranty of any kind.
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import * as ReactDOM from "react-dom";
|
|
3
|
+
import * as ReactJsx from "react/jsx-runtime";
|
|
4
|
+
import { createRoot } from "react-dom/client";
|
|
5
|
+
import { createInstance } from "@module-federation/runtime";
|
|
6
|
+
import * as Admin from "@kuafucloud/extension-kit/admin";
|
|
7
|
+
import * as AdminUI from "@kuafucloud/extension-kit/admin-ui";
|
|
8
|
+
|
|
9
|
+
const config = window.__KUAFU_ADMIN_TEST_CONTEXT__;
|
|
10
|
+
const shell = document.getElementById("kuafu-admin-test-host");
|
|
11
|
+
async function request(path, input) {
|
|
12
|
+
const response = await fetch(path, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify(input) });
|
|
13
|
+
const result = await response.json();
|
|
14
|
+
if (!response.ok) throw new Error(result.message ?? result.code ?? `Test Host HTTP ${response.status}`);
|
|
15
|
+
return result;
|
|
16
|
+
}
|
|
17
|
+
window[Admin.ADMIN_SHELL_AUTH_BRIDGE_WINDOW_KEY] = {
|
|
18
|
+
version: Admin.ADMIN_SHELL_AUTH_BRIDGE_VERSION,
|
|
19
|
+
readResourceScopeRevision: () => 1,
|
|
20
|
+
readContext: async () => ({ origin: location.origin, controlPlaneAdapter: "test-host", projectId: config.projectId, projectEnvKey: config.projectEnvKey, artifactIdentity: config.artifactIdentity }),
|
|
21
|
+
readAuthStatus: async () => config.authStatus,
|
|
22
|
+
executeRequestOperation: async input => await request("/__test_host__/operation", input),
|
|
23
|
+
canExecuteRequestOperation: input => input.projectId === config.projectId && input.projectEnvKey === config.projectEnvKey,
|
|
24
|
+
hydrateOperation: async () => null,
|
|
25
|
+
invalidateTags: async () => {},
|
|
26
|
+
subscribe: () => () => {},
|
|
27
|
+
loginWithPassword: async () => { throw new Error("Test authentication is owned by the server fixture."); },
|
|
28
|
+
updateAuthProfile: async () => { throw new Error("Use the real System API in the owning fixture."); },
|
|
29
|
+
};
|
|
30
|
+
window.dispatchEvent(new Event(Admin.ADMIN_SHELL_AUTH_BRIDGE_READY_EVENT));
|
|
31
|
+
|
|
32
|
+
try {
|
|
33
|
+
const shared = Object.fromEntries([
|
|
34
|
+
["react", React, config.reactVersion], ["react-dom", ReactDOM, config.reactVersion],
|
|
35
|
+
["react/jsx-runtime", ReactJsx, config.reactVersion],
|
|
36
|
+
["@kuafucloud/extension-kit/admin", Admin, config.kitVersion],
|
|
37
|
+
["@kuafucloud/extension-kit/admin-ui", AdminUI, config.kitVersion],
|
|
38
|
+
].map(([name, module, version]) => [name, { version, lib: () => module, shareConfig: { singleton: true, requiredVersion: "*" } }]));
|
|
39
|
+
const federation = createInstance({ name: "standalone_admin_test_host", remotes: [{ name: "subject", entry: `${location.origin}/__extension_admin__/remote/mf-manifest.json` }], shared });
|
|
40
|
+
const pagePath = new URLSearchParams(location.search).get("targetPath") ?? config.initialPage;
|
|
41
|
+
const parameters = new URLSearchParams(location.search);
|
|
42
|
+
for (const [key, value] of Object.entries({ devPreview: "1", previewProduct: config.extensionId, projectId: String(config.projectId), projectEnvKey: config.projectEnvKey, targetPath: pagePath })) parameters.set(key, value);
|
|
43
|
+
history.replaceState(null, "", `${location.pathname}?${parameters}`);
|
|
44
|
+
const localeModule = await federation.loadRemote("subject/i18n");
|
|
45
|
+
const loadLocale = localeModule?.loadLocale ?? localeModule?.default?.loadLocale;
|
|
46
|
+
const messages = loadLocale ? await loadLocale(config.locale) : {};
|
|
47
|
+
const loaded = await federation.loadRemote(`subject/page${pagePath}`);
|
|
48
|
+
const definition = loaded?.default ?? loaded;
|
|
49
|
+
const Component = definition?.component ?? definition;
|
|
50
|
+
if (typeof Component !== "function" && typeof Component !== "object") throw new Error(`Admin page ${pagePath} has no component.`);
|
|
51
|
+
createRoot(document.getElementById("root")).render(React.createElement(Admin.AdminI18nProvider,
|
|
52
|
+
{ intl: Admin.createAdminIntl({ locale: config.locale, messages }) }, React.createElement(Component)));
|
|
53
|
+
shell.dataset.remoteReady = "true";
|
|
54
|
+
} catch (error) {
|
|
55
|
+
shell.dataset.remoteError = String(error);
|
|
56
|
+
document.getElementById("root").textContent = String(error);
|
|
57
|
+
throw error;
|
|
58
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { realpathSync, lstatSync } from "node:fs";
|
|
3
|
+
import { createRequire } from "node:module";
|
|
4
|
+
import { pathToFileURL } from "node:url";
|
|
5
|
+
|
|
6
|
+
// 本配置由项目自己的Product Test提供;Parent先创建测试环境,本配置绝不启动服务。
|
|
7
|
+
const workspaceRoot = absolute("PLATFORM_E2E_WORKSPACE_ROOT");
|
|
8
|
+
const spec = ownedFile(process.env.PLATFORM_WORKSPACE_E2E_SPEC);
|
|
9
|
+
const config = process.env.PLATFORM_WORKSPACE_E2E_CONFIG ? ownedFile(process.env.PLATFORM_WORKSPACE_E2E_CONFIG) : null;
|
|
10
|
+
const evidenceRoot = absolute("PLATFORM_TEST_SHARED_DOT_CACHE_ROOT");
|
|
11
|
+
const outputDir = absolute("PLATFORM_E2E_PLAYWRIGHT_OUTPUT_DIR");
|
|
12
|
+
if (!evidenceRoot.startsWith(`${workspaceRoot}${path.sep}.cache${path.sep}tmp${path.sep}`)
|
|
13
|
+
|| !outputDir.startsWith(`${evidenceRoot}${path.sep}`)) throw new Error("工作区测试证据必须属于本Flow。");
|
|
14
|
+
const protectedKeys = ["PLATFORM_TEST_SHARED_MODE", "PLATFORM_TEST_SHARED_WEB_ORIGIN", "PLATFORM_TEST_SHARED_DAEMON_ORIGIN",
|
|
15
|
+
"PLATFORM_TEST_SHARED_DEVMANAGER_ORIGIN", "PLATFORM_TEST_SHARED_DOT_CACHE_ROOT", "PLATFORM_E2E_WORKSPACE_ROOT",
|
|
16
|
+
"PLATFORM_E2E_PLAYWRIGHT_OUTPUT_DIR", "PLATFORM_E2E_RECORDING_DIR", "KUAFUCLOUD_PLAYWRIGHT_TEST_MODULE_PATH",
|
|
17
|
+
"PLAYWRIGHT_BROWSERS_PATH", "PLATFORM_WORKSPACE_E2E_VIEWPORT", "PLATFORM_WORKSPACE_E2E_SPEC", "PLATFORM_WORKSPACE_E2E_CONFIG",
|
|
18
|
+
"PLATFORM_WORKSPACE_E2E_OWNED_MEDIA", "PLATFORM_E2E_RUN_ID", "PLATFORM_E2E_TEST_ID", "PLATFORM_E2E_PRODUCT_ID", "PLATFORM_E2E_LIVE_SCREENSHOTS"];
|
|
19
|
+
const protectedEnvironment = Object.fromEntries(protectedKeys.map(key => [key, process.env[key]]));
|
|
20
|
+
if (protectedEnvironment.PLATFORM_TEST_SHARED_MODE !== "1") throw new Error("工作区测试必须由Parent创建隔离测试环境。");
|
|
21
|
+
const origin = new URL(protectedEnvironment.PLATFORM_TEST_SHARED_WEB_ORIGIN);
|
|
22
|
+
if (origin.protocol !== "http:" || origin.hostname !== "127.0.0.1" || !origin.port || origin.pathname !== "/" || origin.username || origin.password || origin.search || origin.hash) {
|
|
23
|
+
throw new Error("工作区测试缺少实际隔离origin。");
|
|
24
|
+
}
|
|
25
|
+
if (protectedEnvironment.PLATFORM_TEST_SHARED_DAEMON_ORIGIN !== origin.origin || protectedEnvironment.PLATFORM_TEST_SHARED_DEVMANAGER_ORIGIN !== origin.origin) {
|
|
26
|
+
throw new Error("工作区测试必须使用同一受控前门。");
|
|
27
|
+
}
|
|
28
|
+
const original = config ? (await import(pathToFileURL(config).href)).default : {};
|
|
29
|
+
if (!original || typeof original !== "object" || Array.isArray(original)) throw new Error("项目测试配置必须导出结构体。");
|
|
30
|
+
if (protectedKeys.some(key => process.env[key] !== protectedEnvironment[key])) throw new Error("项目测试配置覆写了Parent隔离地址或证据根,请改为消费受控测试环境。");
|
|
31
|
+
if (original.webServer && (!Array.isArray(original.webServer) || original.webServer.length)
|
|
32
|
+
|| original.globalSetup || original.globalTeardown) throw new Error("工作区测试配置不能另起服务或全局生命周期,请使用已创建的测试环境。");
|
|
33
|
+
if (original.outputDir && path.resolve(path.dirname(config), original.outputDir) !== outputDir) throw new Error("项目测试配置覆写了本Flow证据目录。");
|
|
34
|
+
if (original.projects !== undefined && (!Array.isArray(original.projects)
|
|
35
|
+
|| original.projects.some(item => !item || typeof item !== "object" || Array.isArray(item)))) throw new Error("项目测试projects结构无效。");
|
|
36
|
+
const projects = original.projects?.filter(item => item.name === "host-local") ?? [];
|
|
37
|
+
if (projects.length > 1) throw new Error("项目Browser测试不能重复声明host-local。");
|
|
38
|
+
const project = projects[0];
|
|
39
|
+
if (original.projects?.length && !project) throw new Error("项目Browser测试未声明host-local。");
|
|
40
|
+
if (project?.dependencies?.length || project?.teardown) throw new Error("工作区测试不支持额外项目生命周期。");
|
|
41
|
+
const allowedProjectKeys = new Set(["name", "testDir", "testMatch", "testIgnore", "outputDir", "timeout", "expect", "retries", "use", "dependencies", "teardown"]);
|
|
42
|
+
if (project && Object.keys(project).some(key => !allowedProjectKeys.has(key))) throw new Error("host-local含未支持的执行选项,不能静默忽略。");
|
|
43
|
+
if (project?.outputDir && path.resolve(path.dirname(config), project.outputDir) !== outputDir) throw new Error("host-local覆写了本Flow证据目录。");
|
|
44
|
+
const tsconfig = original.tsconfig;
|
|
45
|
+
const use = { ...original.use, ...project?.use };
|
|
46
|
+
if (use.channel || use.connectOptions || use.launchOptions?.executablePath || use.launchOptions?.env
|
|
47
|
+
|| use.browserName && use.browserName !== "chromium") throw new Error("项目测试不能替换Host冻结Browser运行资产。");
|
|
48
|
+
if (typeof use.storageState === "string") use.storageState = ownedFile(path.resolve(path.dirname(config), use.storageState));
|
|
49
|
+
if (use.baseURL && use.baseURL !== origin.origin) throw new Error("项目测试配置的baseURL不属于Parent隔离测试环境。");
|
|
50
|
+
const allowedKeys = new Set(["webServer", "globalSetup", "globalTeardown", "testDir", "testMatch", "testIgnore", "projects", "outputDir",
|
|
51
|
+
"workers", "fullyParallel", "reporter", "timeout", "globalTimeout", "expect", "retries", "forbidOnly", "use", "tsconfig", "name"]);
|
|
52
|
+
if (Object.keys(original).some(key => !allowedKeys.has(key))) throw new Error("项目测试配置含未支持的执行选项,不能静默忽略。");
|
|
53
|
+
const require = createRequire(import.meta.url);
|
|
54
|
+
const runtime = require(absolute("KUAFUCLOUD_PLAYWRIGHT_TEST_MODULE_PATH"));
|
|
55
|
+
const viewport = JSON.parse(process.env.PLATFORM_WORKSPACE_E2E_VIEWPORT);
|
|
56
|
+
if (![viewport.width, viewport.height].every(value => Number.isInteger(value) && value > 0 && value <= 8192)) throw new Error("测试viewport无效。");
|
|
57
|
+
export default runtime.defineConfig({
|
|
58
|
+
testDir: path.dirname(spec), testMatch: [path.basename(spec)],
|
|
59
|
+
fullyParallel: false, workers: 1, retries: 0,
|
|
60
|
+
timeout: project?.timeout ?? original.timeout ?? 120_000, globalTimeout: original.globalTimeout ?? 0,
|
|
61
|
+
expect: { timeout: 15_000, ...original.expect, ...project?.expect }, forbidOnly: true,
|
|
62
|
+
...(tsconfig ? { tsconfig: ownedFile(path.resolve(path.dirname(config), tsconfig)) } : {}),
|
|
63
|
+
outputDir, reporter: [["line"], [require.resolve("@kuafucloud/product-test/e2e/manifest-reporter")]],
|
|
64
|
+
use: { ...use, baseURL: origin.origin, browserName: "chromium", headless: true, viewport, screenshot: "off",
|
|
65
|
+
// owned Browser 由 SDK 连续录制并返回编码帧偏移,不能另起 fixture 视频。
|
|
66
|
+
video: { mode: "off", size: viewport }, trace: { mode: "retain-on-failure", screenshots: false, snapshots: false, sources: false } },
|
|
67
|
+
projects: [{ name: "host-local" }],
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
function absolute(key) {
|
|
71
|
+
const value = process.env[key];
|
|
72
|
+
if (!value || !path.isAbsolute(value) || path.resolve(value) !== value) throw new Error(`${key}必须是明确的绝对路径。`);
|
|
73
|
+
return value;
|
|
74
|
+
}
|
|
75
|
+
function ownedFile(value) {
|
|
76
|
+
if (!value || !path.isAbsolute(value) || !value.startsWith(`${workspaceRoot}${path.sep}`)) throw new Error("项目测试文件不属于当前项目。");
|
|
77
|
+
let cursor = workspaceRoot;
|
|
78
|
+
for (const segment of path.relative(workspaceRoot, value).split(path.sep)) {
|
|
79
|
+
if (!segment || segment === "." || segment === "..") throw new Error("项目测试文件路径无效。");
|
|
80
|
+
cursor = path.join(cursor, segment);
|
|
81
|
+
if (lstatSync(cursor).isSymbolicLink() || realpathSync(cursor) !== cursor) throw new Error("项目测试文件不能经过软链。");
|
|
82
|
+
}
|
|
83
|
+
if (!lstatSync(cursor).isFile()) throw new Error("项目测试文件不是普通文件。");
|
|
84
|
+
return cursor;
|
|
85
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* defineE2E — 结构化 E2E 测试定义框架
|
|
3
|
+
*
|
|
4
|
+
* 在 Playwright test() 基础上包装:
|
|
5
|
+
* - 声明式步骤定义(steps + assertions 配置)
|
|
6
|
+
* - checkpoint() 执行断言并收集结果
|
|
7
|
+
* - 通过 annotation 传递定义元数据供 reporter 消费
|
|
8
|
+
*/
|
|
9
|
+
import type { Page } from "../types/playwright/test.js";
|
|
10
|
+
import type { E2EHostAppBase } from "./support/runtime.js";
|
|
11
|
+
export interface ExpectAssertion {
|
|
12
|
+
type: "expect";
|
|
13
|
+
label: string;
|
|
14
|
+
fn: (context: {
|
|
15
|
+
page: Page;
|
|
16
|
+
}) => Promise<void>;
|
|
17
|
+
}
|
|
18
|
+
export type AssertionDefinition = ExpectAssertion;
|
|
19
|
+
export interface AssertionResult {
|
|
20
|
+
type: "expect";
|
|
21
|
+
label: string;
|
|
22
|
+
passed: boolean;
|
|
23
|
+
error?: string;
|
|
24
|
+
}
|
|
25
|
+
export interface E2EStepDefinition {
|
|
26
|
+
id: string;
|
|
27
|
+
label: string;
|
|
28
|
+
description?: string;
|
|
29
|
+
assertions?: AssertionDefinition[];
|
|
30
|
+
}
|
|
31
|
+
export interface E2ERunContext {
|
|
32
|
+
page: Page;
|
|
33
|
+
app: E2EHostAppBase;
|
|
34
|
+
/** 在当前步骤执行所有断言 */
|
|
35
|
+
checkpoint: (stepId: string) => Promise<void>;
|
|
36
|
+
/** 注册测试结束时一定要执行的清理动作;后注册的先执行。 */
|
|
37
|
+
onCleanup: (label: string, cleanup: () => Promise<void> | void) => void;
|
|
38
|
+
}
|
|
39
|
+
/** defineE2E 能接受的 test fixture 最小签名 */
|
|
40
|
+
type E2ETestFixture = {
|
|
41
|
+
(title: string, fn: (args: {
|
|
42
|
+
app: E2EHostAppBase;
|
|
43
|
+
page: Page;
|
|
44
|
+
}) => Promise<void>): void;
|
|
45
|
+
skip(title: string, fn: (args: {
|
|
46
|
+
app: E2EHostAppBase;
|
|
47
|
+
page: Page;
|
|
48
|
+
}) => Promise<void>): void;
|
|
49
|
+
info(): {
|
|
50
|
+
annotations: Array<{
|
|
51
|
+
type: string;
|
|
52
|
+
description?: string;
|
|
53
|
+
}>;
|
|
54
|
+
setTimeout(timeout: number): void;
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
export interface E2ETestDefinition {
|
|
58
|
+
id: string;
|
|
59
|
+
title: string;
|
|
60
|
+
description?: string;
|
|
61
|
+
product: string;
|
|
62
|
+
tags?: string[];
|
|
63
|
+
/** 当前故事通过隔离 Host 预览业务 Product 时所需的底层 Runtime 制品。 */
|
|
64
|
+
hostPreviewRuntimes?: Array<"app-simulator" | "site" | "mini-program-simulator">;
|
|
65
|
+
/** 在定义阶段跳过测试;适合 remote-only 等环境门槛。 */
|
|
66
|
+
skip?: {
|
|
67
|
+
when: boolean;
|
|
68
|
+
reason: string;
|
|
69
|
+
};
|
|
70
|
+
/** 远端 Workflow 等长耗时场景可显式扩大单用例超时。 */
|
|
71
|
+
timeoutMs?: number;
|
|
72
|
+
steps: E2EStepDefinition[];
|
|
73
|
+
/** 默认使用本地 Console Test Host;Electron 原生场景使用桌面产品自己的 Playwright 配置。 */
|
|
74
|
+
test?: E2ETestFixture;
|
|
75
|
+
run: (context: E2ERunContext) => Promise<void>;
|
|
76
|
+
}
|
|
77
|
+
export interface E2ECleanupResult {
|
|
78
|
+
label: string;
|
|
79
|
+
passed: boolean;
|
|
80
|
+
error?: string;
|
|
81
|
+
}
|
|
82
|
+
export declare function step(id: string, label: string, options?: {
|
|
83
|
+
description?: string;
|
|
84
|
+
assertions?: AssertionDefinition[];
|
|
85
|
+
}): E2EStepDefinition;
|
|
86
|
+
export declare function defineE2E(definition: E2ETestDefinition): void;
|
|
87
|
+
export {};
|
|
88
|
+
//# sourceMappingURL=define-e2e.d.ts.map
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* defineE2E — 结构化 E2E 测试定义框架
|
|
3
|
+
*
|
|
4
|
+
* 在 Playwright test() 基础上包装:
|
|
5
|
+
* - 声明式步骤定义(steps + assertions 配置)
|
|
6
|
+
* - checkpoint() 执行断言并收集结果
|
|
7
|
+
* - 通过 annotation 传递定义元数据供 reporter 消费
|
|
8
|
+
*/
|
|
9
|
+
import { test as localHostTest } from "./support/fixtures.js";
|
|
10
|
+
// ─── step 辅助函数 ───
|
|
11
|
+
export function step(id, label, options) {
|
|
12
|
+
return {
|
|
13
|
+
id,
|
|
14
|
+
label,
|
|
15
|
+
description: options?.description,
|
|
16
|
+
assertions: options?.assertions,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
// ─── defineE2E ───
|
|
20
|
+
export function defineE2E(definition) {
|
|
21
|
+
const tags = definition.tags ?? [];
|
|
22
|
+
const tagSuffix = tags.length > 0 ? ` @${tags.join(" @")}` : "";
|
|
23
|
+
const testFn = definition.test ?? localHostTest;
|
|
24
|
+
const title = `[${definition.product}] ${definition.title}${tagSuffix}`;
|
|
25
|
+
if (definition.skip?.when) {
|
|
26
|
+
testFn.skip(title, async () => { });
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
testFn(title, async ({ app, page }) => {
|
|
30
|
+
if (definition.timeoutMs) {
|
|
31
|
+
testFn.info().setTimeout(definition.timeoutMs);
|
|
32
|
+
}
|
|
33
|
+
annotateDefinition(testFn, definition, tags);
|
|
34
|
+
if (process.env.PLATFORM_E2E_RECORDING_DIR?.trim()) {
|
|
35
|
+
emitE2EEvent({ event: "recording:start" });
|
|
36
|
+
}
|
|
37
|
+
const allResults = {};
|
|
38
|
+
const cleanupStack = [];
|
|
39
|
+
const checkpoint = createCheckpointFn(page, definition, allResults);
|
|
40
|
+
const onCleanup = (label, cleanup) => {
|
|
41
|
+
cleanupStack.push({ label, cleanup });
|
|
42
|
+
};
|
|
43
|
+
let runError;
|
|
44
|
+
let cleanupResults = [];
|
|
45
|
+
try {
|
|
46
|
+
await definition.run({ page, app, checkpoint, onCleanup });
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
runError = error;
|
|
50
|
+
}
|
|
51
|
+
finally {
|
|
52
|
+
cleanupResults = await runCleanups(cleanupStack);
|
|
53
|
+
// 将所有断言结果写入 annotation
|
|
54
|
+
testFn.info().annotations.push({
|
|
55
|
+
type: "e2e-assertion-results",
|
|
56
|
+
description: JSON.stringify(allResults),
|
|
57
|
+
});
|
|
58
|
+
if (cleanupResults.length > 0) {
|
|
59
|
+
testFn.info().annotations.push({
|
|
60
|
+
type: "e2e-cleanup-results",
|
|
61
|
+
description: JSON.stringify(cleanupResults),
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
const cleanupFailures = cleanupResults.filter((result) => !result.passed);
|
|
66
|
+
if (runError) {
|
|
67
|
+
throw runError;
|
|
68
|
+
}
|
|
69
|
+
if (cleanupFailures.length > 0) {
|
|
70
|
+
throw new Error([
|
|
71
|
+
"E2E cleanup failed:",
|
|
72
|
+
...cleanupFailures.map((failure) => `- ${failure.label}: ${failure.error ?? "unknown error"}`),
|
|
73
|
+
].join("\n"));
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
function annotateDefinition(testFn, definition, tags) {
|
|
78
|
+
// 将定义元数据序列化到 annotation,供 reporter 跨进程读取
|
|
79
|
+
testFn.info().annotations.push({
|
|
80
|
+
type: "e2e-definition",
|
|
81
|
+
description: JSON.stringify({
|
|
82
|
+
kind: "technical-regression",
|
|
83
|
+
id: definition.id,
|
|
84
|
+
title: definition.title,
|
|
85
|
+
description: definition.description,
|
|
86
|
+
product: definition.product,
|
|
87
|
+
tags,
|
|
88
|
+
...(definition.hostPreviewRuntimes?.length
|
|
89
|
+
? { hostPreviewRuntimes: definition.hostPreviewRuntimes }
|
|
90
|
+
: {}),
|
|
91
|
+
steps: definition.steps.map((s) => ({
|
|
92
|
+
id: s.id,
|
|
93
|
+
label: s.label,
|
|
94
|
+
description: s.description,
|
|
95
|
+
// assertions 定义中 fn 不可序列化,只传稳定类型与可读标签。
|
|
96
|
+
assertions: resolveStepAssertions(s).map((assertion) => ({
|
|
97
|
+
type: assertion.type,
|
|
98
|
+
label: assertion.label,
|
|
99
|
+
})),
|
|
100
|
+
})),
|
|
101
|
+
}),
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
async function runCleanups(cleanupStack) {
|
|
105
|
+
const results = [];
|
|
106
|
+
for (const item of [...cleanupStack].reverse()) {
|
|
107
|
+
emitE2EEvent({ event: "cleanup:start", label: item.label });
|
|
108
|
+
try {
|
|
109
|
+
await item.cleanup();
|
|
110
|
+
const result = { label: item.label, passed: true };
|
|
111
|
+
results.push(result);
|
|
112
|
+
emitE2EEvent({ event: "cleanup:done", ...result });
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
const result = {
|
|
116
|
+
label: item.label,
|
|
117
|
+
passed: false,
|
|
118
|
+
error: error instanceof Error ? error.message : String(error),
|
|
119
|
+
};
|
|
120
|
+
results.push(result);
|
|
121
|
+
emitE2EEvent({ event: "cleanup:done", ...result });
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return results;
|
|
125
|
+
}
|
|
126
|
+
// ─── checkpoint 实现 ───
|
|
127
|
+
function createCheckpointFn(page, definition, allResults) {
|
|
128
|
+
return async (stepId) => {
|
|
129
|
+
const stepDef = definition.steps.find((s) => s.id === stepId);
|
|
130
|
+
if (!stepDef) {
|
|
131
|
+
throw new Error(`checkpoint("${stepId}") failed: step "${stepId}" not found in definition "${definition.id}". ` +
|
|
132
|
+
`Available: ${definition.steps.map((s) => s.id).join(", ")}`);
|
|
133
|
+
}
|
|
134
|
+
// 通知 step 开始
|
|
135
|
+
emitE2EEvent({ event: "step:start", stepId });
|
|
136
|
+
const results = [];
|
|
137
|
+
let firstError = null;
|
|
138
|
+
for (const assertion of resolveStepAssertions(stepDef)) {
|
|
139
|
+
const assertionIndex = results.length;
|
|
140
|
+
const assertionLabel = assertion.label;
|
|
141
|
+
emitE2EEvent({
|
|
142
|
+
event: "assertion:start",
|
|
143
|
+
stepId,
|
|
144
|
+
assertionIndex,
|
|
145
|
+
type: assertion.type,
|
|
146
|
+
label: assertionLabel,
|
|
147
|
+
});
|
|
148
|
+
const result = await executeAssertion(page, assertion);
|
|
149
|
+
results.push(result);
|
|
150
|
+
emitE2EEvent({
|
|
151
|
+
event: "assertion:done",
|
|
152
|
+
stepId,
|
|
153
|
+
assertionIndex,
|
|
154
|
+
type: result.type,
|
|
155
|
+
label: result.label,
|
|
156
|
+
passed: result.passed,
|
|
157
|
+
error: result.error?.slice(0, 500),
|
|
158
|
+
});
|
|
159
|
+
if (!result.passed && !firstError) {
|
|
160
|
+
firstError = new Error(result.error ?? `Assertion failed: ${result.label}`);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
allResults[stepId] = results;
|
|
164
|
+
// 通知 step 完成
|
|
165
|
+
const donePayload = {
|
|
166
|
+
event: "step:done",
|
|
167
|
+
stepId,
|
|
168
|
+
passed: !firstError,
|
|
169
|
+
assertions: results.map((r) => ({
|
|
170
|
+
type: r.type,
|
|
171
|
+
label: r.label,
|
|
172
|
+
passed: r.passed,
|
|
173
|
+
error: r.error ? r.error.slice(0, 500) : undefined,
|
|
174
|
+
})),
|
|
175
|
+
};
|
|
176
|
+
emitE2EEvent(donePayload);
|
|
177
|
+
if (firstError) {
|
|
178
|
+
throw firstError;
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
function emitE2EEvent(payload) {
|
|
183
|
+
process.stdout.write(`__E2E_EVENT__${JSON.stringify({
|
|
184
|
+
...payload,
|
|
185
|
+
eventAtMs: Date.now(),
|
|
186
|
+
})}\n`);
|
|
187
|
+
}
|
|
188
|
+
function resolveStepAssertions(stepDefinition) {
|
|
189
|
+
if (stepDefinition.assertions?.length)
|
|
190
|
+
return stepDefinition.assertions;
|
|
191
|
+
return [{
|
|
192
|
+
type: "expect",
|
|
193
|
+
label: "页面上下文在步骤检查点保持可用",
|
|
194
|
+
fn: async ({ page }) => {
|
|
195
|
+
if (page.isClosed())
|
|
196
|
+
throw new Error(`步骤“${stepDefinition.label}”到达检查点前页面已关闭。`);
|
|
197
|
+
},
|
|
198
|
+
}];
|
|
199
|
+
}
|
|
200
|
+
async function executeAssertion(page, assertion) {
|
|
201
|
+
try {
|
|
202
|
+
await assertion.fn({ page });
|
|
203
|
+
return { type: "expect", label: assertion.label, passed: true };
|
|
204
|
+
}
|
|
205
|
+
catch (e) {
|
|
206
|
+
return { type: "expect", label: assertion.label, passed: false, error: String(e) };
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
//# sourceMappingURL=define-e2e.js.map
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
export { action, expect, stage, validateProductFlowDefinition, resolveProductFlowPresentationHoldMs } from "./product-flow-rules.js";
|
|
2
|
+
import type { Page } from "../types/playwright/test.js";
|
|
3
|
+
import type { E2EHostAppBase } from "./support/runtime.js";
|
|
4
|
+
export type ProductFlowOperationKind = "action" | "expect";
|
|
5
|
+
export type ProductFlowStatus = "passed" | "failed" | "skipped" | "canceled" | "completed-with-deferred";
|
|
6
|
+
export type ProductFlowDeferredCategory = "feature-incomplete" | "test-framework-gap";
|
|
7
|
+
export interface ProductFlowDeferredDefinition {
|
|
8
|
+
category: ProductFlowDeferredCategory;
|
|
9
|
+
reason: string;
|
|
10
|
+
evidenceRef?: string;
|
|
11
|
+
affectedStageIds?: string[];
|
|
12
|
+
reviewStatus: "pending-user-confirmation" | "confirmed";
|
|
13
|
+
}
|
|
14
|
+
export interface ProductFlowContext {
|
|
15
|
+
page: Page;
|
|
16
|
+
app: E2EHostAppBase;
|
|
17
|
+
/** Electron Flow 重启同一安装态 Host 后,将后续 Operation 切换到新窗口。 */
|
|
18
|
+
replacePage: (page: Page) => Promise<void>;
|
|
19
|
+
/** 注册流程结束时一定执行的清理动作;后注册的先执行。 */
|
|
20
|
+
onCleanup: (label: string, cleanup: () => Promise<void> | void) => void;
|
|
21
|
+
}
|
|
22
|
+
export interface ProductFlowOperationDefinition {
|
|
23
|
+
id: string;
|
|
24
|
+
kind: ProductFlowOperationKind;
|
|
25
|
+
label: string;
|
|
26
|
+
run: (context: ProductFlowContext) => Promise<void> | void;
|
|
27
|
+
}
|
|
28
|
+
export interface ProductFlowStageDefinition {
|
|
29
|
+
id: string;
|
|
30
|
+
label: string;
|
|
31
|
+
description?: string;
|
|
32
|
+
deferred?: ProductFlowDeferredDefinition;
|
|
33
|
+
operations: ProductFlowOperationDefinition[];
|
|
34
|
+
}
|
|
35
|
+
/** defineProductFlow 能接受的 test fixture 最小签名。 */
|
|
36
|
+
export type ProductFlowTestFixture = {
|
|
37
|
+
(title: string, run: (args: {
|
|
38
|
+
app: E2EHostAppBase;
|
|
39
|
+
page: Page;
|
|
40
|
+
}) => Promise<void>): void;
|
|
41
|
+
skip(title: string, run: (args: {
|
|
42
|
+
app: E2EHostAppBase;
|
|
43
|
+
page: Page;
|
|
44
|
+
}) => Promise<void>): void;
|
|
45
|
+
info(): {
|
|
46
|
+
annotations: Array<{
|
|
47
|
+
type: string;
|
|
48
|
+
description?: string;
|
|
49
|
+
}>;
|
|
50
|
+
setTimeout(timeout: number): void;
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
export interface ProductFlowDefinition {
|
|
54
|
+
id: string;
|
|
55
|
+
title: string;
|
|
56
|
+
description?: string;
|
|
57
|
+
product: string;
|
|
58
|
+
tags?: string[];
|
|
59
|
+
skip?: {
|
|
60
|
+
when: boolean;
|
|
61
|
+
reason: string;
|
|
62
|
+
};
|
|
63
|
+
timeoutMs?: number;
|
|
64
|
+
execution?: {
|
|
65
|
+
/** Browser Flow 默认 browser;真实 Electron Flow 必须显式声明 host-electron。 */
|
|
66
|
+
target?: "browser" | "host-electron";
|
|
67
|
+
/** 同一 Product 内的稳定执行顺序。 */
|
|
68
|
+
order?: number;
|
|
69
|
+
};
|
|
70
|
+
/** 跨 Stage 的公共低成本前置验证。 */
|
|
71
|
+
stages: ProductFlowStageDefinition[];
|
|
72
|
+
/** 默认使用本地 Console Test Host;Electron 原生流程可传入自己的 fixture。 */
|
|
73
|
+
test?: ProductFlowTestFixture;
|
|
74
|
+
/** 技术前置条件,不进入用户可见 Stage。 */
|
|
75
|
+
setup?: (context: ProductFlowContext) => Promise<void> | void;
|
|
76
|
+
/** 定义级清理,即使 setup、action 或 expect 失败也会执行。 */
|
|
77
|
+
cleanup?: (context: ProductFlowContext) => Promise<void> | void;
|
|
78
|
+
}
|
|
79
|
+
export interface ProductFlowOperationResult {
|
|
80
|
+
id: string;
|
|
81
|
+
kind: ProductFlowOperationKind;
|
|
82
|
+
label: string;
|
|
83
|
+
status: ProductFlowStatus;
|
|
84
|
+
error?: string;
|
|
85
|
+
}
|
|
86
|
+
export interface ProductFlowDeferredResult extends ProductFlowDeferredDefinition {
|
|
87
|
+
stageId: string;
|
|
88
|
+
label: string;
|
|
89
|
+
}
|
|
90
|
+
export interface ProductFlowCleanupResult {
|
|
91
|
+
label: string;
|
|
92
|
+
passed: boolean;
|
|
93
|
+
error?: string;
|
|
94
|
+
}
|
|
95
|
+
export interface ProductFlowFrameCaptureResult {
|
|
96
|
+
/** 当前画面在最终录像时间轴中的精确位置。 */
|
|
97
|
+
recordingOffsetMs?: number;
|
|
98
|
+
}
|
|
99
|
+
export interface ProductFlowFramePublisher {
|
|
100
|
+
capture: (boundary: string) => Promise<ProductFlowFrameCaptureResult | void>;
|
|
101
|
+
stop: () => Promise<void>;
|
|
102
|
+
}
|
|
103
|
+
export declare function defineProductFlow(definition: ProductFlowDefinition): void;
|
|
104
|
+
/**
|
|
105
|
+
* Electron 等外部 fixture 在拿到真实 Page 后注册自己的 compositor 采集器。
|
|
106
|
+
* 定义层只依赖统一边界,不感知 BrowserWindow 或编码实现。
|
|
107
|
+
*/
|
|
108
|
+
export declare function registerProductFlowFramePublisher(page: Page, publisher: ProductFlowFramePublisher): () => void;
|
|
109
|
+
//# sourceMappingURL=define-product-flow.d.ts.map
|