gz-laya-pilot 0.1.0 → 0.1.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/NOTICE.md +20 -2
- package/README.md +54 -2
- package/dist/agent/bridge.d.ts +21 -0
- package/dist/agent/bridge.js +158 -0
- package/dist/agent/bridge.js.map +1 -0
- package/dist/agent/report.d.ts +54 -0
- package/dist/agent/report.js +100 -0
- package/dist/agent/report.js.map +1 -0
- package/dist/agent/run.d.ts +19 -0
- package/dist/agent/run.js +121 -0
- package/dist/agent/run.js.map +1 -0
- package/dist/agent/schema.d.ts +42 -0
- package/dist/agent/schema.js +9 -0
- package/dist/agent/schema.js.map +1 -0
- package/dist/agent/worker.d.ts +1 -0
- package/dist/agent/worker.js +102 -0
- package/dist/agent/worker.js.map +1 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +7 -0
- package/dist/cli.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/package.json +14 -6
- package/src/agent/bridge.ts +180 -0
- package/src/agent/report.ts +139 -0
- package/src/agent/run.ts +136 -0
- package/src/agent/schema.ts +38 -0
- package/src/agent/worker.ts +114 -0
- package/src/cli.ts +5 -0
- package/src/index.ts +3 -0
- package/vendor/.gitattributes +5 -0
- package/vendor/UPSTREAM.json +168 -0
- package/vendor/laya-pilot/LICENSE +21 -0
- package/vendor/laya-pilot/README.en.md +116 -0
- package/vendor/laya-pilot/README.md +116 -0
- package/vendor/laya-pilot/ROADMAP.md +21 -0
- package/vendor/laya-pilot/config.example.json +14 -0
- package/vendor/laya-pilot/docs/architecture.md +21 -0
- package/vendor/laya-pilot/docs/browser-use-evaluation.md +32 -0
- package/vendor/laya-pilot/docs/demo.png +0 -0
- package/vendor/laya-pilot/docs/local-runtime.md +88 -0
- package/vendor/laya-pilot/docs/pr1-selection.md +33 -0
- package/vendor/laya-pilot/docs/retry-policy.md +14 -0
- package/vendor/laya-pilot/examples/demo-cases.xlsx +0 -0
- package/vendor/laya-pilot/examples/demo-server.mjs +19 -0
- package/vendor/laya-pilot/examples/demo.html +733 -0
- package/vendor/laya-pilot/lib/auth-state.mjs +141 -0
- package/vendor/laya-pilot/lib/browser-provider.mjs +43 -0
- package/vendor/laya-pilot/lib/diagnostics.mjs +210 -0
- package/vendor/laya-pilot/lib/discovery.mjs +474 -0
- package/vendor/laya-pilot/lib/dom.mjs +539 -0
- package/vendor/laya-pilot/lib/engine.mjs +879 -0
- package/vendor/laya-pilot/lib/form.mjs +377 -0
- package/vendor/laya-pilot/lib/language.mjs +273 -0
- package/vendor/laya-pilot/lib/model.mjs +271 -0
- package/vendor/laya-pilot/lib/page-capabilities.mjs +356 -0
- package/vendor/laya-pilot/lib/page-exploration.mjs +372 -0
- package/vendor/laya-pilot/lib/page-session.mjs +69 -0
- package/vendor/laya-pilot/lib/pending-writes.mjs +50 -0
- package/vendor/laya-pilot/lib/planner.mjs +127 -0
- package/vendor/laya-pilot/lib/project-config.mjs +95 -0
- package/vendor/laya-pilot/lib/readiness.mjs +179 -0
- package/vendor/laya-pilot/lib/related-records.mjs +328 -0
- package/vendor/laya-pilot/lib/replay-model.mjs +44 -0
- package/vendor/laya-pilot/lib/run-state.mjs +133 -0
- package/vendor/laya-pilot/lib/test-data.mjs +128 -0
- package/vendor/laya-pilot/lib/workflow-policy.mjs +68 -0
- package/vendor/laya-pilot/lib/workflow.mjs +2321 -0
- package/vendor/laya-pilot/lib/workflow_excel.py +462 -0
- package/vendor/laya-pilot/requirements-local.txt +2 -0
- package/vendor/laya-pilot/requirements.txt +1 -0
- package/vendor/laya-pilot/runner.mjs +452 -0
- package/vendor/laya-pilot/tests/api.test.mjs +98 -0
- package/vendor/laya-pilot/tests/browser/runtime.test.mjs +847 -0
- package/vendor/laya-pilot/tests/config.test.mjs +109 -0
- package/vendor/laya-pilot/tests/diagnostics.test.mjs +91 -0
- package/vendor/laya-pilot/tests/discovery.test.mjs +76 -0
- package/vendor/laya-pilot/tests/fixtures/local-model/laya.py +28 -0
- package/vendor/laya-pilot/tests/fixtures/local-model/torch.py +5 -0
- package/vendor/laya-pilot/tests/guards.test.mjs +43 -0
- package/vendor/laya-pilot/tests/language.test.mjs +39 -0
- package/vendor/laya-pilot/tests/planner.test.mjs +129 -0
- package/vendor/laya-pilot/tests/readiness.test.mjs +118 -0
- package/vendor/laya-pilot/tests/real-model.smoke.mjs +74 -0
- package/vendor/laya-pilot/tests/replay-model.test.mjs +90 -0
- package/vendor/laya-pilot/tests/retries.test.mjs +270 -0
- package/vendor/laya-pilot/tests/run-state.test.mjs +162 -0
- package/vendor/laya-pilot/tests/selected-pr1.test.mjs +309 -0
- package/vendor/laya-pilot/tests/test-data.test.mjs +83 -0
- package/vendor/laya-pilot/tests/test_workflow_excel.py +162 -0
- package/vendor/laya-pilot/tests/worker_test.py +33 -0
- package/vendor/laya-pilot/tests/workflow.test.mjs +98 -0
- package/vendor/laya-pilot/worker.py +135 -0
- package/vendor/patches/0001-native-field-recipes.patch +3009 -0
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import readline from 'node:readline/promises';
|
|
4
|
+
import { writePrivateJson } from './run-state.mjs';
|
|
5
|
+
import { waitForReady, switchLanguage } from './readiness.mjs';
|
|
6
|
+
|
|
7
|
+
export async function loadAuthState(config) {
|
|
8
|
+
if (!config.authState || config.authReset) return null;
|
|
9
|
+
let saved;
|
|
10
|
+
try {
|
|
11
|
+
saved = JSON.parse(await fs.readFile(path.resolve(config.authState), 'utf8'));
|
|
12
|
+
} catch (error) {
|
|
13
|
+
if (error.code === 'ENOENT') return null;
|
|
14
|
+
throw Error('登录态文件无法读取,请重新登录并使用 --auth-reset');
|
|
15
|
+
}
|
|
16
|
+
if (
|
|
17
|
+
saved.schemaVersion !== 1 ||
|
|
18
|
+
saved.origin !== new URL(config.url).origin ||
|
|
19
|
+
saved.account !== (config.user || '') ||
|
|
20
|
+
!Array.isArray(saved.storageState?.cookies) ||
|
|
21
|
+
!Array.isArray(saved.storageState?.origins)
|
|
22
|
+
)
|
|
23
|
+
throw Error('登录态的站点、账号或格式不匹配,请使用独立文件或 --auth-reset');
|
|
24
|
+
return saved;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export async function restoreSessionStorage(context, saved, config) {
|
|
28
|
+
if (!config.authSessionStorage || !saved?.sessionStorage) return;
|
|
29
|
+
await context.addInitScript(
|
|
30
|
+
({ origin, entries }) => {
|
|
31
|
+
if (location.origin === origin && !sessionStorage.getItem('__laya_auth_restored__')) {
|
|
32
|
+
for (const [key, value] of Object.entries(entries))
|
|
33
|
+
sessionStorage.setItem(key, value);
|
|
34
|
+
sessionStorage.setItem('__laya_auth_restored__', '1');
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
{ origin: saved.origin, entries: saved.sessionStorage },
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export async function saveAuthState(context, page, config) {
|
|
42
|
+
if (!config.authState) return;
|
|
43
|
+
if (new URL(page.url()).origin !== new URL(config.url).origin)
|
|
44
|
+
throw Error('登录后未返回目标站点,不保存登录态');
|
|
45
|
+
const storageState = await context.storageState({ indexedDB: true });
|
|
46
|
+
const sessionStorage = config.authSessionStorage
|
|
47
|
+
? await page.evaluate(() =>
|
|
48
|
+
Object.fromEntries(
|
|
49
|
+
Object.entries(window.sessionStorage).filter(
|
|
50
|
+
([key]) => key !== '__laya_auth_restored__',
|
|
51
|
+
),
|
|
52
|
+
),
|
|
53
|
+
)
|
|
54
|
+
: undefined;
|
|
55
|
+
writePrivateJson(path.resolve(config.authState), {
|
|
56
|
+
schemaVersion: 1,
|
|
57
|
+
origin: new URL(config.url).origin,
|
|
58
|
+
account: config.user || '',
|
|
59
|
+
savedAt: new Date().toISOString(),
|
|
60
|
+
storageState,
|
|
61
|
+
...(sessionStorage ? { sessionStorage } : {}),
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export async function ensureAuthentication(page, config, readSecret, chooseTarget, restored) {
|
|
66
|
+
if (config.manualLogin && config.headless) throw Error('手动登录需要可见浏览器');
|
|
67
|
+
const passwordVisible = () => page.locator('input[type="password"]:visible').count();
|
|
68
|
+
const check = async () => {
|
|
69
|
+
if (await passwordVisible()) return false;
|
|
70
|
+
const expected = new URL(config.url),
|
|
71
|
+
actual = new URL(page.url());
|
|
72
|
+
if (expected.origin !== actual.origin || expected.pathname !== actual.pathname)
|
|
73
|
+
return false;
|
|
74
|
+
if (config.authCheck) {
|
|
75
|
+
const marker = page.locator(config.authCheck);
|
|
76
|
+
return (await marker.count()) === 1 && (await marker.isVisible());
|
|
77
|
+
}
|
|
78
|
+
return true;
|
|
79
|
+
};
|
|
80
|
+
if (restored && config.authCheck) {
|
|
81
|
+
// Let hydration either show the authenticated marker or redirect to login.
|
|
82
|
+
await Promise.race([
|
|
83
|
+
page
|
|
84
|
+
.locator(config.authCheck)
|
|
85
|
+
.waitFor({ state: 'visible', timeout: config.pageTimeout }),
|
|
86
|
+
page
|
|
87
|
+
.locator('input[type="password"]:visible')
|
|
88
|
+
.first()
|
|
89
|
+
.waitFor({ timeout: config.pageTimeout }),
|
|
90
|
+
]).catch(() => {});
|
|
91
|
+
}
|
|
92
|
+
if (restored && (await check())) return 'reused';
|
|
93
|
+
if (config.manualLogin) {
|
|
94
|
+
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
95
|
+
try {
|
|
96
|
+
await rl.question('请在浏览器登录并打开目标模块,回车继续:', {
|
|
97
|
+
signal: config.signal,
|
|
98
|
+
});
|
|
99
|
+
} finally {
|
|
100
|
+
rl.close();
|
|
101
|
+
}
|
|
102
|
+
} else if (config.user) {
|
|
103
|
+
await page.locator('input[type="password"]:visible').first().waitFor({ timeout: 30000 });
|
|
104
|
+
await switchLanguage(page, config.language === 'auto' ? 'zh-CN' : config.language);
|
|
105
|
+
const accountName =
|
|
106
|
+
/^(?:(?:请输入|请填写)\s*)?(?:登录)?(?:账号|账户|用户名|邮箱)$|^(account|username|email)$/i;
|
|
107
|
+
const accounts = page
|
|
108
|
+
.getByRole('textbox', { name: accountName })
|
|
109
|
+
.or(page.getByPlaceholder(accountName))
|
|
110
|
+
.filter({ visible: true });
|
|
111
|
+
const account =
|
|
112
|
+
(await accounts.count()) === 1 && (await accounts.isVisible())
|
|
113
|
+
? { locator: accounts }
|
|
114
|
+
: await chooseTarget('账号 Account Username', 'fill');
|
|
115
|
+
const password = await readSecret();
|
|
116
|
+
await account.locator.fill(config.user);
|
|
117
|
+
const passwords = page.locator('input[type="password"]:visible');
|
|
118
|
+
if ((await passwords.count()) !== 1)
|
|
119
|
+
throw Error('登录页密码框不唯一,请使用 --manual-login');
|
|
120
|
+
await passwords.fill(password);
|
|
121
|
+
// A fresh observation replaces live refs; finish filling before resolving
|
|
122
|
+
// another target so the account locator cannot become stale.
|
|
123
|
+
const buttons = page.getByRole('button', { name: /^(登\s*录|login|log in|sign in)$/i });
|
|
124
|
+
const login =
|
|
125
|
+
(await buttons.count()) === 1 && (await buttons.isVisible())
|
|
126
|
+
? { locator: buttons }
|
|
127
|
+
: await chooseTarget('登录 Login Sign in');
|
|
128
|
+
await login.locator.click();
|
|
129
|
+
await passwords.waitFor({ state: 'hidden', timeout: 30000 });
|
|
130
|
+
} else if (restored || (await passwordVisible())) {
|
|
131
|
+
throw Error('登录态已失效或仍在登录页,请提供 --user 或使用 --manual-login 重新登录');
|
|
132
|
+
}
|
|
133
|
+
if (page.url() !== config.url) await page.goto(config.url, { waitUntil: 'domcontentloaded' });
|
|
134
|
+
await waitForReady(page, { timeout: config.pageTimeout });
|
|
135
|
+
if (config.authCheck)
|
|
136
|
+
await page
|
|
137
|
+
.locator(config.authCheck)
|
|
138
|
+
.waitFor({ state: 'visible', timeout: config.pageTimeout });
|
|
139
|
+
if (!(await check())) throw Error('登录后目标页面或 --auth-check 未通过,不保存登录态');
|
|
140
|
+
return config.user || config.manualLogin ? 'logged-in' : 'not-required';
|
|
141
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { loadAuthState, restoreSessionStorage } from './auth-state.mjs';
|
|
2
|
+
// Keep the browser boundary in one place. A future driver must provide the
|
|
3
|
+
// page/context methods used by the engines before it can be selected here.
|
|
4
|
+
export async function launchBrowser(config) {
|
|
5
|
+
if (config.browserProvider !== 'playwright') {
|
|
6
|
+
throw new Error(
|
|
7
|
+
`Unsupported browser provider: ${config.browserProvider}. Currently supported: playwright`,
|
|
8
|
+
);
|
|
9
|
+
}
|
|
10
|
+
const { chromium } = await import('playwright');
|
|
11
|
+
const options = { headless: config.headless };
|
|
12
|
+
if (config.browserChannel) options.channel = config.browserChannel;
|
|
13
|
+
return chromium.launch(options);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export async function openBrowser(config, { authState, browser: sharedBrowser } = {}) {
|
|
17
|
+
const auth = authState ?? (await loadAuthState(config));
|
|
18
|
+
const browser = sharedBrowser ?? (await launchBrowser(config));
|
|
19
|
+
let context;
|
|
20
|
+
try {
|
|
21
|
+
context = await browser.newContext({
|
|
22
|
+
...(auth ? { storageState: auth.storageState } : {}),
|
|
23
|
+
viewport: { width: 1500, height: 980 },
|
|
24
|
+
locale: 'zh-CN',
|
|
25
|
+
acceptDownloads: true,
|
|
26
|
+
});
|
|
27
|
+
await restoreSessionStorage(context, auth, config);
|
|
28
|
+
const page = await context.newPage();
|
|
29
|
+
page.setDefaultTimeout(9000);
|
|
30
|
+
page.setDefaultNavigationTimeout(config.pageTimeout || 18000);
|
|
31
|
+
return {
|
|
32
|
+
browser,
|
|
33
|
+
context,
|
|
34
|
+
page,
|
|
35
|
+
authRestored: !!auth,
|
|
36
|
+
close: () => (sharedBrowser ? context.close() : browser.close()),
|
|
37
|
+
};
|
|
38
|
+
} catch (error) {
|
|
39
|
+
if (sharedBrowser) await context?.close();
|
|
40
|
+
else await browser.close();
|
|
41
|
+
throw error;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
const MAX_EVENTS = 60;
|
|
2
|
+
|
|
3
|
+
function errorBodySummary(body) {
|
|
4
|
+
if (typeof body !== 'string' || body.length > 20000) return '';
|
|
5
|
+
try {
|
|
6
|
+
const data = JSON.parse(body);
|
|
7
|
+
if (!data || typeof data !== 'object') return '';
|
|
8
|
+
const code = data.code ?? data.errorCode ?? data.error_code;
|
|
9
|
+
const message =
|
|
10
|
+
data.message ?? data.msg ?? data.error_description ?? data.error?.message ?? data.error;
|
|
11
|
+
const parts = [code, message].filter((v) => typeof v === 'string' || typeof v === 'number');
|
|
12
|
+
return safeText(parts.join(':'), 320);
|
|
13
|
+
} catch {
|
|
14
|
+
return '';
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function safeUrl(raw) {
|
|
19
|
+
try {
|
|
20
|
+
const url = new URL(raw);
|
|
21
|
+
if (!['http:', 'https:'].includes(url.protocol)) return '[non-http URL]';
|
|
22
|
+
return url.origin + url.pathname;
|
|
23
|
+
} catch {
|
|
24
|
+
return '[invalid URL]';
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function safeText(value, limit = 500) {
|
|
29
|
+
return String(value ?? '')
|
|
30
|
+
.replace(/https?:\/\/[^\s"'<>]+/gi, (match) => safeUrl(match))
|
|
31
|
+
.replace(/Bearer\s+\S+/gi, 'Bearer [REDACTED]')
|
|
32
|
+
.replace(/\bsk-[A-Za-z0-9_-]+\b/g, '[REDACTED]')
|
|
33
|
+
.replace(
|
|
34
|
+
/\b(password|passwd|token|api[_-]?key|secret)\b\s*[:=]\s*[^\s,;]+/gi,
|
|
35
|
+
'$1=[REDACTED]',
|
|
36
|
+
)
|
|
37
|
+
.replace(/[\r\n\t]+/g, ' ')
|
|
38
|
+
.slice(0, limit);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export class BrowserDiagnostics {
|
|
42
|
+
constructor(context) {
|
|
43
|
+
this.active = null;
|
|
44
|
+
this.requests = new WeakMap();
|
|
45
|
+
context.on('request', (request) => {
|
|
46
|
+
if (this.active)
|
|
47
|
+
this.requests.set(request, { owner: this.active, step: this.active.step });
|
|
48
|
+
});
|
|
49
|
+
context.on('response', (response) => {
|
|
50
|
+
const request = response.request(),
|
|
51
|
+
kind = request.resourceType(),
|
|
52
|
+
status = response.status();
|
|
53
|
+
if (!['xhr', 'fetch'].includes(kind) || status < 400) return;
|
|
54
|
+
const attribution = this.requests.get(request);
|
|
55
|
+
if (!attribution || attribution.owner !== this.active) return;
|
|
56
|
+
const owner = this.active;
|
|
57
|
+
const event = this.add({
|
|
58
|
+
kind: 'http',
|
|
59
|
+
status,
|
|
60
|
+
method: request.method(),
|
|
61
|
+
url: safeUrl(response.url()),
|
|
62
|
+
detail: safeText(response.statusText(), 160),
|
|
63
|
+
step: attribution.step,
|
|
64
|
+
});
|
|
65
|
+
if (owner && event && /json/i.test(response.headers()['content-type'] || '')) {
|
|
66
|
+
const pending = Promise.race([
|
|
67
|
+
response.text(),
|
|
68
|
+
new Promise((resolve) => setTimeout(() => resolve(''), 700)),
|
|
69
|
+
])
|
|
70
|
+
.then((body) => {
|
|
71
|
+
const summary = errorBodySummary(body);
|
|
72
|
+
if (summary) event.detail = summary;
|
|
73
|
+
})
|
|
74
|
+
.catch(() => {});
|
|
75
|
+
owner.pending.push(pending);
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
context.on('requestfailed', (request) => {
|
|
79
|
+
if (!['xhr', 'fetch', 'document'].includes(request.resourceType())) return;
|
|
80
|
+
const attribution = this.requests.get(request);
|
|
81
|
+
if (!attribution || attribution.owner !== this.active) return;
|
|
82
|
+
const detail = safeText(request.failure()?.errorText || '请求未完成', 200);
|
|
83
|
+
this.add({
|
|
84
|
+
kind: /ERR_ABORTED|NS_BINDING_ABORTED/i.test(detail)
|
|
85
|
+
? 'requestcancelled'
|
|
86
|
+
: 'requestfailed',
|
|
87
|
+
method: request.method(),
|
|
88
|
+
url: safeUrl(request.url()),
|
|
89
|
+
detail,
|
|
90
|
+
step: attribution.step,
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
for (const page of context.pages()) this.attachPage(page);
|
|
94
|
+
context.on('page', (page) => this.attachPage(page));
|
|
95
|
+
}
|
|
96
|
+
attachPage(page) {
|
|
97
|
+
page.on('pageerror', (error) =>
|
|
98
|
+
this.add({ kind: 'pageerror', detail: safeText(error.message) }),
|
|
99
|
+
);
|
|
100
|
+
page.on('console', (entry) => {
|
|
101
|
+
if (entry.type() === 'error')
|
|
102
|
+
this.add({ kind: 'console', detail: safeText(entry.text()) });
|
|
103
|
+
});
|
|
104
|
+
page.on('crash', () => this.add({ kind: 'crash', detail: '页面进程崩溃' }));
|
|
105
|
+
}
|
|
106
|
+
add(event) {
|
|
107
|
+
if (!this.active || this.active.events.length >= MAX_EVENTS) return;
|
|
108
|
+
const saved = {
|
|
109
|
+
time: new Date().toISOString(),
|
|
110
|
+
...event,
|
|
111
|
+
step: event.step ?? this.active.step ?? null,
|
|
112
|
+
};
|
|
113
|
+
this.active.events.push(saved);
|
|
114
|
+
return saved;
|
|
115
|
+
}
|
|
116
|
+
begin(id) {
|
|
117
|
+
if (this.active) throw Error('上一条用例的错误收集尚未结束');
|
|
118
|
+
this.active = { id, events: [], pending: [], step: null };
|
|
119
|
+
}
|
|
120
|
+
setStep(step) {
|
|
121
|
+
if (this.active) this.active.step = step ? { ...step } : null;
|
|
122
|
+
}
|
|
123
|
+
async end() {
|
|
124
|
+
const owner = this.active;
|
|
125
|
+
this.active = null;
|
|
126
|
+
if (!owner) return [];
|
|
127
|
+
await Promise.allSettled(owner.pending);
|
|
128
|
+
return owner.events;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export function eventLabel(event) {
|
|
133
|
+
if (event.kind === 'http')
|
|
134
|
+
return `HTTP ${event.status} ${event.method} ${event.url}${event.detail ? ':' + event.detail : ''}`;
|
|
135
|
+
if (event.kind === 'requestfailed')
|
|
136
|
+
return `请求失败 ${event.method} ${event.url}:${event.detail}`;
|
|
137
|
+
if (event.kind === 'requestcancelled')
|
|
138
|
+
return `请求取消 ${event.method} ${event.url}:${event.detail}`;
|
|
139
|
+
if (event.kind === 'pageerror') return `前端异常:${event.detail}`;
|
|
140
|
+
if (event.kind === 'crash') return '浏览器页面崩溃';
|
|
141
|
+
return `控制台错误:${event.detail}`;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export function severeEvent(events) {
|
|
145
|
+
return (
|
|
146
|
+
events.find((e) => e.kind === 'crash') ||
|
|
147
|
+
events.find((e) => e.kind === 'requestfailed') ||
|
|
148
|
+
events.find((e) => e.kind === 'http' && e.status >= 500) ||
|
|
149
|
+
events.find((e) => e.kind === 'http' && [401, 403].includes(e.status)) ||
|
|
150
|
+
events.find((e) => e.kind === 'http') ||
|
|
151
|
+
events.find((e) => e.kind === 'pageerror')
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export function diagnoseFailure(error, events = []) {
|
|
156
|
+
const crash = events.find((e) => e.kind === 'crash');
|
|
157
|
+
const network = events.find((e) => e.kind === 'requestfailed');
|
|
158
|
+
const server = events.find((e) => e.kind === 'http' && e.status >= 500);
|
|
159
|
+
const auth = events.find((e) => e.kind === 'http' && [401, 403].includes(e.status));
|
|
160
|
+
const client = events.find((e) => e.kind === 'http' && e.status >= 400);
|
|
161
|
+
const script = events.find((e) => e.kind === 'pageerror');
|
|
162
|
+
const text = safeText(error, 700);
|
|
163
|
+
let category, assessment, primary;
|
|
164
|
+
if (crash || /Target page.*closed|browser.*disconnected/i.test(text)) {
|
|
165
|
+
category = '浏览器异常';
|
|
166
|
+
assessment = '页面进程或浏览器中断,当前用例未完成';
|
|
167
|
+
primary = crash;
|
|
168
|
+
} else if (network) {
|
|
169
|
+
category = '网络请求失败';
|
|
170
|
+
assessment = '接口请求未完成;需排查网络、服务可用性和请求取消原因';
|
|
171
|
+
primary = network;
|
|
172
|
+
} else if (server) {
|
|
173
|
+
category = '接口 HTTP 5xx';
|
|
174
|
+
assessment = '用例期间接口返回服务端错误;优先排查接口与后端日志';
|
|
175
|
+
primary = server;
|
|
176
|
+
} else if (auth) {
|
|
177
|
+
category = '接口权限或登录错误';
|
|
178
|
+
assessment = '接口返回 401/403;需核对登录会话和账号权限';
|
|
179
|
+
primary = auth;
|
|
180
|
+
} else if (client) {
|
|
181
|
+
category = '接口 HTTP 4xx';
|
|
182
|
+
assessment = '接口拒绝请求;需结合请求参数和业务规则区分用例数据与产品问题';
|
|
183
|
+
primary = client;
|
|
184
|
+
} else if (script) {
|
|
185
|
+
category = '前端脚本异常';
|
|
186
|
+
assessment = '页面出现未捕获的 JavaScript 错误;优先排查前端';
|
|
187
|
+
primary = script;
|
|
188
|
+
} else if (/执行需要 --data|表单还有必填字段|未知数据变量/.test(text)) {
|
|
189
|
+
category = '测试数据不足';
|
|
190
|
+
assessment = '用例所需数据未提供,不能判定产品功能失败';
|
|
191
|
+
} else if (/Laya未能确定|Laya选择.*把握不足/.test(text)) {
|
|
192
|
+
category = '模型无法可靠定位';
|
|
193
|
+
assessment = 'Laya 未能在可见候选中作出可靠选择,尚不能判定产品功能';
|
|
194
|
+
} else if (/页面上没有可操作|找不到本轮独立记录|决策后页面控件已变化|复现「/.test(text)) {
|
|
195
|
+
category = '控件或页面状态不匹配';
|
|
196
|
+
assessment = '实际页面与记录步骤不一致;需核对用例、权限和页面变化';
|
|
197
|
+
} else if (/列表没有|列表仍有|Timeout|等待|waitFor/.test(text)) {
|
|
198
|
+
category = '页面结果与预期不符';
|
|
199
|
+
assessment = '页面断言未满足;需结合截图与轨迹区分产品问题和用例预期问题';
|
|
200
|
+
} else {
|
|
201
|
+
category = '执行步骤失败';
|
|
202
|
+
assessment = '现有证据不足以区分用例问题与产品问题,需查看浏览器轨迹';
|
|
203
|
+
}
|
|
204
|
+
return {
|
|
205
|
+
category,
|
|
206
|
+
assessment,
|
|
207
|
+
primary: primary ? eventLabel(primary) : null,
|
|
208
|
+
observed: events.map(eventLabel),
|
|
209
|
+
};
|
|
210
|
+
}
|