lantern-harness 0.1.8 → 0.1.10
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/CHANGELOG.md +106 -0
- package/README.md +7 -7
- package/builtin/session/lh-fs.mjs +32 -14
- package/cli/server.mjs +2 -0
- package/config/app.defaults.json +1 -0
- package/dist/500.html +258 -22
- package/dist/_astro/Base.BOY6F0mi.css +1 -0
- package/dist/_astro/Base.astro_astro_type_script_index_0_lang.C30xtpc6.js +1 -0
- package/dist/_astro/CaseExecBoard.Dttqp0wg.js +1 -0
- package/dist/_astro/CaseGenerateApp.DqW_nWMc.js +9 -0
- package/dist/_astro/CasesLibrary.8aMUUGp_.js +1 -0
- package/dist/_astro/ChatApp.BLxf0h1d.js +219 -0
- package/dist/_astro/ColumnResize.BEkEW7qw.js +1 -0
- package/dist/_astro/{ConfirmDialog.CBVsDJ8f.js → ConfirmDialog.BZzZWDci.js} +1 -1
- package/dist/_astro/{DashboardBoard.Bd3eUPwZ.js → DashboardBoard.CkSbhEDI.js} +3 -3
- package/dist/_astro/DatePicker.Bkl71M49.js +1 -0
- package/dist/_astro/DocsFileBrowser.MKVCfZrE.js +1 -0
- package/dist/_astro/DocsPager.DJY4foYL.js +1 -0
- package/dist/_astro/{DrawerField.CWMLhcPX.js → DrawerField.CHRg77LG.js} +1 -1
- package/dist/_astro/{ExcelFieldTags.B_XMwNNx.js → ExcelFieldTags.DXMeaDa5.js} +4 -4
- package/dist/_astro/KnowledgeLibrary.B-p1aoQN.js +1 -0
- package/dist/_astro/NamePromptDialog.Wa9O530a.js +1 -0
- package/dist/_astro/NavLibraries.C6LImMO_.js +1 -0
- package/dist/_astro/OverflowHint.D6v4Uj2N.js +1 -0
- package/dist/_astro/PluginsPage.-97n3x2U.js +39 -0
- package/dist/_astro/PluginsPage.DkmRPfbU.js +1 -0
- package/dist/_astro/PmBugsBoard.CMT4-eoB.js +1 -0
- package/dist/_astro/PmIterationsBoard.BQM_dzdZ.js +1 -0
- package/dist/_astro/PmProjectDetail.CzU80pat.js +1 -0
- package/dist/_astro/PmProjectsBoard.Cxsa7SYQ.js +1 -0
- package/dist/_astro/PmRequirementsBoard.4_JMDDN0.js +1 -0
- package/dist/_astro/PmRequirementsBoard.B8HL07cw.js +1 -0
- package/dist/_astro/PmShared.Ca2Ao7v_.js +1 -0
- package/dist/_astro/PromptsLibrary.C9BcIxKK.js +1 -0
- package/dist/_astro/ReportsPage.Fh5DRKLT.js +1 -0
- package/dist/_astro/RequirementsLibrary.CBkuWE8X.js +1 -0
- package/dist/_astro/SearchableSelect.DnjoSslK.js +1 -0
- package/dist/_astro/SelectDropdown.CuuDHa8s.js +1 -0
- package/dist/_astro/SettingsDialog.CJvBRcs9.js +1 -0
- package/dist/_astro/SlideOverDrawer.B7B5Wu8I.js +1 -0
- package/dist/_astro/{SpaceSwitcher.BEJo5KFi.js → SpaceSwitcher.BVQLt_tL.js} +2 -2
- package/dist/_astro/TagSelect.CuN5RdF4.js +1 -0
- package/dist/_astro/TasksBoard.Bu-fpIZ7.js +1 -0
- package/dist/_astro/TemplatesLibrary.DSylQGlN.js +1 -0
- package/dist/_astro/TestStatsBoard.CPyKW4wc.js +1 -0
- package/dist/_astro/Toast.R8_2OWRX.js +1 -0
- package/dist/_astro/TodosBoard.8kvRTEW-.js +1 -0
- package/dist/_astro/UiAutoBoard.DFTiDqCp.js +9 -0
- package/dist/_astro/active-element-history.CED-m1Zp.js +1 -0
- package/dist/_astro/app-config.BUPzGl9f.js +1 -0
- package/dist/_astro/{client.Be_SakrB.js → client.BHNTqLjv.js} +3 -3
- package/dist/_astro/dialog.BAPTk9a_.js +1 -0
- package/dist/_astro/hydrate-cache.BXX604Bw.js +1 -0
- package/dist/_astro/i18n.BGPnP1Bg.js +7 -0
- package/dist/_astro/index.astro_astro_type_script_index_0_lang.LPL9mPQY.js +1 -0
- package/dist/_astro/jsx-runtime.DUg_B_vD.js +1 -0
- package/dist/_astro/{lib.DJNDi_Ht.js → lib.CgXLzMMd.js} +15 -15
- package/dist/_astro/listbox.C96OCanh.js +1 -0
- package/dist/_astro/markdown-client.DRva7DS9.js +58 -0
- package/dist/_astro/menu.IPWdRoNM.js +1 -0
- package/dist/_astro/{open-docs-in-session.B0ZdZNj2.js → open-docs-in-session.CyFNgh1I.js} +1 -1
- package/dist/_astro/{plugins.D5ypJIXn.js → plugins.C2BMfzv1.js} +1 -1
- package/dist/_astro/{pm.BsnTv63L.js → pm.CagBA-ht.js} +4 -4
- package/dist/_astro/{portal.Cm9XlIPf.js → portal.C0hAhXDF.js} +2 -2
- package/dist/_astro/prompts-meta.Bua-j14I.js +2 -0
- package/dist/_astro/react-dom.DLW9pGCF.js +1 -0
- package/dist/_astro/{react-dom.CqAxpHMt.js → react.Blvh0GOs.js} +1 -1
- package/dist/_astro/report-prompt.CAfZ6RIo.js +1 -0
- package/dist/_astro/{settings.CnDCvwnQ.js → settings.Ceefeil5.js} +2 -2
- package/dist/_astro/{spaces-client.Cai2tHEI.js → spaces-client.BocUabwy.js} +1 -1
- package/dist/_astro/ui-auto-prompt.CX8fju7r.js +3 -0
- package/dist/_astro/ui-auto.BmC9JJ_X.js +1 -0
- package/dist/_astro/update.CvfIVwnq.js +1 -0
- package/dist/_astro/use-text-value.DBQvHeNl.js +1 -0
- package/dist/_astro/use-tree-walker.DFU2AaAP.js +1 -0
- package/dist/_astro/useListAnim.BoerpmsB.js +1 -0
- package/dist/_astro/{view-prefs.HCowOWXL.js → view-prefs.BEAZRcxF.js} +1 -1
- package/dist/case-exec/index.html +458 -22
- package/dist/cases/generate/index.html +258 -22
- package/dist/cases/library/index.html +458 -22
- package/dist/dashboard/index.html +458 -22
- package/dist/docs/changelog/index.html +389 -23
- package/dist/docs/install/index.html +258 -22
- package/dist/docs/manual/index.html +324 -27
- package/dist/index.html +2 -281
- package/dist/knowledge/index.html +458 -22
- package/dist/knowledge//345/212/237/350/203/275/346/265/213/350/257/225/347/224/250/344/276/213/347/274/226/345/206/231/350/247/204/350/214/203/index.html +258 -22
- package/dist/plugins/index.html +258 -22
- package/dist/pm/bugs/index.html +458 -22
- package/dist/pm/iterations/index.html +458 -22
- package/dist/pm/projects/detail/index.html +458 -22
- package/dist/pm/projects/index.html +458 -22
- package/dist/pm/requirements/index.html +458 -22
- package/dist/prompts/index.html +458 -22
- package/dist/reports/index.html +458 -22
- package/dist/requirements/index.html +458 -22
- package/dist/tasks/index.html +458 -22
- package/dist/templates/index.html +458 -22
- package/dist/templates//345/212/237/350/203/275/346/265/213/350/257/225/346/212/245/345/221/212//345/212/237/350/203/275/346/265/213/350/257/225/346/212/245/345/221/212/346/250/241/346/235/277/index.html +258 -22
- package/dist/templates//346/200/247/350/203/275/346/265/213/350/257/225/346/212/245/345/221/212//346/200/247/350/203/275/346/265/213/350/257/225/346/212/245/345/221/212/346/250/241/346/235/277/index.html +258 -22
- package/dist/templates//346/200/247/350/203/275/346/265/213/350/257/225/347/224/250/344/276/213//346/200/247/350/203/275/346/265/213/350/257/225/347/224/250/344/276/213/346/250/241/346/235/277/index.html +258 -22
- package/dist/templates//346/216/245/345/217/243/346/265/213/350/257/225/346/212/245/345/221/212//346/216/245/345/217/243/346/265/213/350/257/225/346/212/245/345/221/212/346/250/241/346/235/277/index.html +258 -22
- package/dist/templates//350/207/252/345/212/250/345/214/226/346/265/213/350/257/225/347/224/250/344/276/213//350/207/252/345/212/250/345/214/226/346/265/213/350/257/225/347/224/250/344/276/213/346/250/241/346/235/277/index.html +258 -22
- package/dist/test-automation/index.html +717 -0
- package/dist/test-stats/index.html +458 -22
- package/dist/todos/index.html +458 -22
- package/dist/workbench/index.html +517 -0
- package/docs//346/223/215/344/275/234/346/211/213/345/206/214.md +85 -34
- package/lh-plugins/lh-project-docs.mjs +14 -0
- package/lh-plugins/lh-uiauto.mjs +2296 -0
- package/lh-plugins/plugin-prompts.mjs +200 -0
- package/lh-plugins/storage-domain.mjs +13 -0
- package/lh-plugins/uiauto-adapters/index.mjs +57 -0
- package/lh-plugins/uiauto-adapters/playwright.mjs +215 -0
- package/lh-plugins/uiauto-adapters/pytest.mjs +151 -0
- package/lh-plugins/uiauto-adapters/selenium.mjs +163 -0
- package/lh-plugins/uiauto-adapters/shared.mjs +371 -0
- package/lh-plugins/uiauto-adapters/tasks.mjs +369 -0
- package/lh-plugins/uiauto-argv-guard.cjs +41 -0
- package/lh-plugins/uiauto-explorer/apps.mjs +332 -0
- package/lh-plugins/uiauto-explorer/diff.mjs +159 -0
- package/lh-plugins/uiauto-explorer/emit-files.mjs +506 -0
- package/lh-plugins/uiauto-explorer/emit-python.mjs +160 -0
- package/lh-plugins/uiauto-explorer/emit-ts.mjs +118 -0
- package/lh-plugins/uiauto-explorer/emit.mjs +15 -0
- package/lh-plugins/uiauto-explorer/ir.mjs +114 -0
- package/lh-plugins/uiauto-explorer/locators.mjs +267 -0
- package/lh-plugins/uiauto-explorer/merge.mjs +235 -0
- package/lh-plugins/uiauto-explorer/plan.mjs +165 -0
- package/lh-plugins/uiauto-explorer/runner.mjs +370 -0
- package/lh-plugins/uiauto-explorer/sessions.mjs +1373 -0
- package/lh-plugins/uiauto-explorer/snapshot.mjs +492 -0
- package/lh-plugins/uiauto-prompt.mjs +122 -0
- package/lh-plugins/uiauto-report.mjs +111 -0
- package/package.json +1 -1
- package/dist/_astro/Base.DjIEQkT7.css +0 -1
- package/dist/_astro/Base.astro_astro_type_script_index_0_lang.D0snmAJ_.js +0 -1
- package/dist/_astro/CaseExecBoard.BAV4EE3o.js +0 -1
- package/dist/_astro/CaseGenerateApp.Bz4ryNJG.js +0 -9
- package/dist/_astro/CasesLibrary.BPifyHUA.js +0 -1
- package/dist/_astro/ChatApp.DLo31SYZ.js +0 -276
- package/dist/_astro/ColumnResize.Bs6GL3DJ.js +0 -1
- package/dist/_astro/DatePicker.CMfX7F6z.js +0 -1
- package/dist/_astro/DocsFileBrowser.D3cKurQE.js +0 -1
- package/dist/_astro/DocsPager.rzu4mnzw.js +0 -1
- package/dist/_astro/KnowledgeLibrary.B0sO4QR1.js +0 -1
- package/dist/_astro/NamePromptDialog.Dl5kSnQI.js +0 -1
- package/dist/_astro/NavLibraries.Cvg3wery.js +0 -1
- package/dist/_astro/OverflowHint.lzmplP5j.js +0 -1
- package/dist/_astro/PluginsPage.DIltPILi.js +0 -39
- package/dist/_astro/PluginsPage.uvmSuXzi.js +0 -1
- package/dist/_astro/PmBugsBoard.CiaIhCHv.js +0 -1
- package/dist/_astro/PmIterationsBoard.H67QwKFw.js +0 -1
- package/dist/_astro/PmProjectDetail.DqDoIg8a.js +0 -1
- package/dist/_astro/PmProjectsBoard.nOC1Hugt.js +0 -1
- package/dist/_astro/PmRequirementsBoard.BPjZ7khP.js +0 -1
- package/dist/_astro/PmRequirementsBoard.CanxvcKg.js +0 -1
- package/dist/_astro/PmShared.CZnXaUq-.js +0 -1
- package/dist/_astro/PromptsLibrary.DU9EuSim.js +0 -1
- package/dist/_astro/ReportsPage.Cr88iz8c.js +0 -1
- package/dist/_astro/RequirementsLibrary.BwriF8F0.js +0 -1
- package/dist/_astro/SearchableSelect.Ctg8V1Z1.js +0 -1
- package/dist/_astro/SelectDropdown.Buv7rQJ_.js +0 -1
- package/dist/_astro/SettingsDialog.DEgsVfaL.js +0 -1
- package/dist/_astro/SlideOverDrawer.TPbGEoWn.js +0 -1
- package/dist/_astro/TagSelect.u8DTop7v.js +0 -1
- package/dist/_astro/TasksBoard.BZi_GBPG.js +0 -1
- package/dist/_astro/TemplatesLibrary.T4K9jUex.js +0 -1
- package/dist/_astro/TestStatsBoard.Dd1Kcmx6.js +0 -1
- package/dist/_astro/TodosBoard.C4yd_JFb.js +0 -1
- package/dist/_astro/active-element-history.CeefGkFQ.js +0 -1
- package/dist/_astro/app-config.DKMxjv3g.js +0 -1
- package/dist/_astro/dialog.DXHznyjD.js +0 -1
- package/dist/_astro/i18n.BXeGGutk.js +0 -7
- package/dist/_astro/jsx-runtime.BUXbF_vF.js +0 -1
- package/dist/_astro/listbox.Di-t5U37.js +0 -1
- package/dist/_astro/menu.Bkluqxic.js +0 -1
- package/dist/_astro/prompts-meta.F4wwkjUA.js +0 -2
- package/dist/_astro/report-prompt.Cmw4XzfY.js +0 -1
- package/dist/_astro/update.DXCPfU4a.js +0 -1
- package/dist/_astro/use-text-value.BuY0Cm1z.js +0 -1
- package/dist/_astro/use-tree-walker.CfTHFX43.js +0 -1
- package/dist/_astro/useListAnim.CpGB1cft.js +0 -1
|
@@ -0,0 +1,506 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 多文件出码:主用例 + 按页面拆出的 page object(+ 可选的登录态 fixture、config 补丁建议)。
|
|
3
|
+
*
|
|
4
|
+
* 为什么要拆:一条用例往往要同时改 spec、页面对象、fixture。
|
|
5
|
+
* 只写一个文件的话,用户还得手工把重复的定位器抽出去。
|
|
6
|
+
*
|
|
7
|
+
* 约定:
|
|
8
|
+
* - 导航(goto / wait / 截图 / URL 断言)留在 spec 里,元素操作进 page object。
|
|
9
|
+
* - page object 按 goto 的 URL 分段:/login → LoginPage,/ → HomePage。
|
|
10
|
+
* - 定位器文本是中文时方法名退化到角色名(clickButton),重名再加序号。
|
|
11
|
+
*/
|
|
12
|
+
import { emitCode } from './emit.mjs';
|
|
13
|
+
import { locatorCode as tsLocator, valueCode as tsValue } from './emit-ts.mjs';
|
|
14
|
+
import { locatorCode as pyLocator, valueCode as pyValue } from './emit-python.mjs';
|
|
15
|
+
import { normalizeIr, pythonFunctionName, slugify } from './ir.mjs';
|
|
16
|
+
|
|
17
|
+
const q = (value) => JSON.stringify(String(value ?? ''));
|
|
18
|
+
|
|
19
|
+
/** 元素操作(进 page object);导航类动作留在 spec */
|
|
20
|
+
const ELEMENT_ACTIONS = new Set(['click', 'fill', 'select', 'press', 'hover', 'expect']);
|
|
21
|
+
|
|
22
|
+
function pascal(text) {
|
|
23
|
+
const ascii = String(text ?? '')
|
|
24
|
+
.replace(/[^a-zA-Z0-9]+/g, ' ')
|
|
25
|
+
.trim();
|
|
26
|
+
if (!ascii) return '';
|
|
27
|
+
return ascii
|
|
28
|
+
.split(/\s+/)
|
|
29
|
+
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
|
|
30
|
+
.join('');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function snake(text) {
|
|
34
|
+
return String(text ?? '')
|
|
35
|
+
.replace(/([a-z0-9])([A-Z])/g, '$1_$2')
|
|
36
|
+
.replace(/[^a-zA-Z0-9]+/g, '_')
|
|
37
|
+
.replace(/^_+|_+$/g, '')
|
|
38
|
+
.toLowerCase();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** `/cart/list?x=1` → `CartListPage`;`/` → `HomePage` */
|
|
42
|
+
export function pageNameFor(url) {
|
|
43
|
+
const clean = String(url ?? '')
|
|
44
|
+
.split('#')[0]
|
|
45
|
+
.split('?')[0]
|
|
46
|
+
.replace(/^https?:\/\/[^/]+/i, '');
|
|
47
|
+
const base = pascal(clean);
|
|
48
|
+
return `${base || 'Home'}Page`;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const KIND_HINT = {
|
|
52
|
+
testid: 'TestId',
|
|
53
|
+
role: 'Role',
|
|
54
|
+
label: 'Label',
|
|
55
|
+
placeholder: 'Placeholder',
|
|
56
|
+
text: 'Text',
|
|
57
|
+
attr: 'ByAttr',
|
|
58
|
+
css: 'Locator',
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/** 页面内方法名:优先用定位器文本,退化到角色 / 类型 */
|
|
62
|
+
function methodBase(step) {
|
|
63
|
+
const locator = step.locator ?? {};
|
|
64
|
+
const hint =
|
|
65
|
+
pascal(locator.value) ||
|
|
66
|
+
pascal(locator.name) ||
|
|
67
|
+
pascal(locator.role) ||
|
|
68
|
+
pascal(locator.attr) ||
|
|
69
|
+
KIND_HINT[locator.kind] ||
|
|
70
|
+
'';
|
|
71
|
+
const action = step.action;
|
|
72
|
+
const tail = hint || 'Element';
|
|
73
|
+
return `${action}${tail.charAt(0).toUpperCase()}${tail.slice(1)}`;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function uniqueName(base, used) {
|
|
77
|
+
if (!used.has(base)) {
|
|
78
|
+
used.add(base);
|
|
79
|
+
return base;
|
|
80
|
+
}
|
|
81
|
+
let index = 2;
|
|
82
|
+
while (used.has(`${base}${index}`)) index += 1;
|
|
83
|
+
const next = `${base}${index}`;
|
|
84
|
+
used.add(next);
|
|
85
|
+
return next;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* 把 IR 的步骤分组:每个 goto 开一段,段内元素操作归到该页面的 page object。
|
|
90
|
+
* 第一段没有 goto 时算「首页」。
|
|
91
|
+
*/
|
|
92
|
+
export function splitPages(ir) {
|
|
93
|
+
const segments = [];
|
|
94
|
+
let current = { url: '', steps: [] };
|
|
95
|
+
for (const step of ir.steps) {
|
|
96
|
+
if (step.action === 'goto') {
|
|
97
|
+
if (current.steps.length || current.url) segments.push(current);
|
|
98
|
+
current = { url: String(step.url ?? '/'), steps: [] };
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
current.steps.push(step);
|
|
102
|
+
}
|
|
103
|
+
if (current.steps.length || current.url) segments.push(current);
|
|
104
|
+
if (!segments.length) segments.push({ url: '/', steps: [] });
|
|
105
|
+
|
|
106
|
+
const pages = [];
|
|
107
|
+
const usedPageNames = new Set();
|
|
108
|
+
for (const segment of segments) {
|
|
109
|
+
const methods = [];
|
|
110
|
+
const used = new Set();
|
|
111
|
+
for (const step of segment.steps) {
|
|
112
|
+
if (!ELEMENT_ACTIONS.has(step.action) || !step.locator) continue;
|
|
113
|
+
methods.push({ step, name: uniqueName(methodBase(step), used) });
|
|
114
|
+
}
|
|
115
|
+
if (!methods.length) continue;
|
|
116
|
+
const name = uniqueName(pageNameFor(segment.url), usedPageNames);
|
|
117
|
+
pages.push({ name, url: segment.url, methods });
|
|
118
|
+
}
|
|
119
|
+
return pages;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** 页面名 → spec 里的变量名:LoginPage → loginPage */
|
|
123
|
+
function varName(pageName) {
|
|
124
|
+
return `${pageName.charAt(0).toLowerCase()}${pageName.slice(1)}`;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function tsMethod(method, index) {
|
|
128
|
+
const { step, name } = method;
|
|
129
|
+
const target = tsLocator(step.locator, 'this.page');
|
|
130
|
+
const note = step.note ? ` /** ${step.note.replace(/\s+/g, ' ').slice(0, 120)} */\n` : '';
|
|
131
|
+
if (step.action === 'fill') {
|
|
132
|
+
return `${note} async ${name}(value: string) {\n await ${target}.fill(value);\n }`;
|
|
133
|
+
}
|
|
134
|
+
if (step.action === 'select') {
|
|
135
|
+
return `${note} async ${name}(value: string) {\n await ${target}.selectOption(value);\n }`;
|
|
136
|
+
}
|
|
137
|
+
if (step.action === 'press') {
|
|
138
|
+
return `${note} async ${name}(key = ${q(step.key ?? 'Enter')}) {\n await ${target}.press(key);\n }`;
|
|
139
|
+
}
|
|
140
|
+
if (step.action === 'click') {
|
|
141
|
+
return `${note} async ${name}() {\n await ${target}.click();\n }`;
|
|
142
|
+
}
|
|
143
|
+
if (step.action === 'hover') {
|
|
144
|
+
return `${note} async ${name}() {\n await ${target}.hover();\n }`;
|
|
145
|
+
}
|
|
146
|
+
if (step.action === 'expect') {
|
|
147
|
+
if (step.text) {
|
|
148
|
+
return `${note} async ${name}(text = ${q(step.text)}) {\n await expect(${target}).toContainText(text);\n }`;
|
|
149
|
+
}
|
|
150
|
+
if (step.state === 'hidden') {
|
|
151
|
+
return `${note} async ${name}() {\n await expect(${target}).toBeHidden();\n }`;
|
|
152
|
+
}
|
|
153
|
+
return `${note} async ${name}() {\n await expect(${target}).toBeVisible();\n }`;
|
|
154
|
+
}
|
|
155
|
+
return `${note} async ${name}() {\n // 第 ${index + 1} 步:${step.action}\n }`;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export function renderTsPage(page) {
|
|
159
|
+
const lines = [];
|
|
160
|
+
lines.push('/**');
|
|
161
|
+
lines.push(` * ${page.name}:${page.url || '/'} 上的元素操作。`);
|
|
162
|
+
lines.push(' */');
|
|
163
|
+
lines.push("import { expect, type Page } from '@playwright/test';");
|
|
164
|
+
lines.push('');
|
|
165
|
+
lines.push(`export class ${page.name} {`);
|
|
166
|
+
lines.push(' constructor(private readonly page: Page) {}');
|
|
167
|
+
for (let index = 0; index < page.methods.length; index += 1) {
|
|
168
|
+
lines.push('');
|
|
169
|
+
lines.push(tsMethod(page.methods[index], index));
|
|
170
|
+
}
|
|
171
|
+
lines.push('}');
|
|
172
|
+
lines.push('');
|
|
173
|
+
return lines.join('\n');
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/** spec 里与 page object 无关的步骤:导航 / 等待 / 截图 / URL 断言 */
|
|
177
|
+
function specStepLine(step, index) {
|
|
178
|
+
switch (step.action) {
|
|
179
|
+
case 'expectUrl':
|
|
180
|
+
return ` await expect(page).toHaveURL(${tsValue(step.url ?? '/')});`;
|
|
181
|
+
case 'wait':
|
|
182
|
+
return ` await page.waitForTimeout(${Number(step.ms) || 500});`;
|
|
183
|
+
case 'screenshot': {
|
|
184
|
+
const name = String(step.value ?? `step-${index + 1}`)
|
|
185
|
+
.replace(/[^\w.-]+/g, '-')
|
|
186
|
+
.replace(/^-+|-+$/g, '');
|
|
187
|
+
return ` await page.screenshot({ path: ${q(
|
|
188
|
+
`reports/ai/${name || `step-${index + 1}`}.png`,
|
|
189
|
+
)}, fullPage: true });`;
|
|
190
|
+
}
|
|
191
|
+
case 'press':
|
|
192
|
+
return ` await page.keyboard.press(${q(step.key ?? 'Enter')});`;
|
|
193
|
+
default:
|
|
194
|
+
return '';
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** TS spec:导航留在用例里,元素操作调 page object */
|
|
199
|
+
export function renderTsSpec(ir, pages, options = {}) {
|
|
200
|
+
const title = ir.title || 'AI 生成用例';
|
|
201
|
+
const lines = [];
|
|
202
|
+
lines.push('/**');
|
|
203
|
+
lines.push(` * ${title}`);
|
|
204
|
+
if (ir.source) {
|
|
205
|
+
for (const line of String(ir.source).split(/\r?\n/).slice(0, 8)) {
|
|
206
|
+
lines.push(` * ${line.trim()}`);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
lines.push(' */');
|
|
210
|
+
lines.push("import { test, expect } from '@playwright/test';");
|
|
211
|
+
for (const page of pages) {
|
|
212
|
+
lines.push(`import { ${page.name} } from './pages/${page.name}';`);
|
|
213
|
+
}
|
|
214
|
+
if (ir.storageState && options.fixturePath) {
|
|
215
|
+
lines.push(`import { STORAGE_STATE } from '${options.fixtureImport}';`);
|
|
216
|
+
}
|
|
217
|
+
lines.push('');
|
|
218
|
+
|
|
219
|
+
const useOptions = [];
|
|
220
|
+
if (ir.baseURL) useOptions.push(`baseURL: ${q(ir.baseURL)}`);
|
|
221
|
+
if (ir.storageState) {
|
|
222
|
+
useOptions.push(`storageState: ${options.fixturePath ? 'STORAGE_STATE' : q(ir.storageState)}`);
|
|
223
|
+
}
|
|
224
|
+
if (useOptions.length) {
|
|
225
|
+
lines.push(`test.use({ ${useOptions.join(', ')} });`);
|
|
226
|
+
lines.push('');
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
lines.push(`test.describe(${q(title)}, () => {`);
|
|
230
|
+
lines.push(` test(${q(title)}, async ({ page }) => {`);
|
|
231
|
+
if (ir.baseURL) lines.push(` // baseURL: ${ir.baseURL}`);
|
|
232
|
+
for (const page of pages) {
|
|
233
|
+
lines.push(` const ${varName(page.name)} = new ${page.name}(page);`);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
let pageCursor = 0;
|
|
237
|
+
let current = pages[0] ?? null;
|
|
238
|
+
let methodCursor = 0;
|
|
239
|
+
for (let index = 0; index < ir.steps.length; index += 1) {
|
|
240
|
+
const step = ir.steps[index];
|
|
241
|
+
if (step.note) lines.push(` // ${step.note.replace(/\s+/g, ' ').slice(0, 160)}`);
|
|
242
|
+
if (step.action === 'goto') {
|
|
243
|
+
const target = pages.find(
|
|
244
|
+
(item, i) => i >= pageCursor && item.url === String(step.url ?? '/'),
|
|
245
|
+
);
|
|
246
|
+
if (target) {
|
|
247
|
+
pageCursor = pages.indexOf(target);
|
|
248
|
+
current = target;
|
|
249
|
+
}
|
|
250
|
+
methodCursor = 0;
|
|
251
|
+
lines.push(` await page.goto(${q(step.url ?? '/')});`);
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
if (ELEMENT_ACTIONS.has(step.action) && step.locator) {
|
|
255
|
+
const method = current?.methods[methodCursor];
|
|
256
|
+
methodCursor += 1;
|
|
257
|
+
if (method && current) {
|
|
258
|
+
const call = `${varName(current.name)}.${method.name}`;
|
|
259
|
+
if (step.action === 'fill' || step.action === 'select') {
|
|
260
|
+
lines.push(` await ${call}(${tsValue(step.value)});`);
|
|
261
|
+
} else if (step.action === 'expect' && step.text) {
|
|
262
|
+
lines.push(` await ${call}(${tsValue(step.text)});`);
|
|
263
|
+
} else if (step.action === 'press' && step.key) {
|
|
264
|
+
lines.push(` await ${call}(${q(step.key)});`);
|
|
265
|
+
} else {
|
|
266
|
+
lines.push(` await ${call}();`);
|
|
267
|
+
}
|
|
268
|
+
continue;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
const line = specStepLine(step, index);
|
|
272
|
+
if (line) lines.push(line);
|
|
273
|
+
}
|
|
274
|
+
lines.push(' });');
|
|
275
|
+
lines.push('});');
|
|
276
|
+
lines.push('');
|
|
277
|
+
return lines.join('\n');
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
function pyMethod(method, index) {
|
|
281
|
+
const { step } = method;
|
|
282
|
+
const name = snake(method.name);
|
|
283
|
+
const target = pyLocator(step.locator, 'self.page');
|
|
284
|
+
const withValue = step.action === 'fill' || step.action === 'select';
|
|
285
|
+
const signature = `def ${name}(self${withValue ? ', value: str' : ''}) -> None:`;
|
|
286
|
+
const note = step.note ? ` # ${step.note.replace(/\s+/g, ' ').slice(0, 120)}\n` : '';
|
|
287
|
+
if (step.action === 'fill') return `${note} ${signature}\n ${target}.fill(value)`;
|
|
288
|
+
if (step.action === 'select') {
|
|
289
|
+
return `${note} ${signature}\n ${target}.select_option(value)`;
|
|
290
|
+
}
|
|
291
|
+
if (step.action === 'press') {
|
|
292
|
+
return `${note} ${signature}\n ${target}.press(${q(step.key ?? 'Enter')})`;
|
|
293
|
+
}
|
|
294
|
+
if (step.action === 'click') return `${note} ${signature}\n ${target}.click()`;
|
|
295
|
+
if (step.action === 'hover') return `${note} ${signature}\n ${target}.hover()`;
|
|
296
|
+
if (step.action === 'expect') {
|
|
297
|
+
if (step.text) {
|
|
298
|
+
return `${note} ${signature}\n expect(${target}).to_contain_text(${q(step.text)})`;
|
|
299
|
+
}
|
|
300
|
+
if (step.state === 'hidden') {
|
|
301
|
+
return `${note} ${signature}\n expect(${target}).to_be_hidden()`;
|
|
302
|
+
}
|
|
303
|
+
return `${note} ${signature}\n expect(${target}).to_be_visible()`;
|
|
304
|
+
}
|
|
305
|
+
return `${note} ${signature}\n pass # 第 ${index + 1} 步:${step.action}`;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
export function renderPyPage(page) {
|
|
309
|
+
const lines = [];
|
|
310
|
+
lines.push('"""');
|
|
311
|
+
lines.push(`${page.name}:${page.url || '/'} 上的元素操作。`);
|
|
312
|
+
lines.push('"""');
|
|
313
|
+
lines.push('from playwright.sync_api import Page, expect');
|
|
314
|
+
lines.push('');
|
|
315
|
+
lines.push('');
|
|
316
|
+
lines.push(`class ${page.name}:`);
|
|
317
|
+
lines.push(' def __init__(self, page: Page) -> None:');
|
|
318
|
+
lines.push(' self.page = page');
|
|
319
|
+
for (let index = 0; index < page.methods.length; index += 1) {
|
|
320
|
+
lines.push('');
|
|
321
|
+
lines.push(pyMethod(page.methods[index], index));
|
|
322
|
+
}
|
|
323
|
+
lines.push('');
|
|
324
|
+
return lines.join('\n');
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
export function renderPySpec(ir, pages) {
|
|
328
|
+
const title = ir.title || 'AI 生成用例';
|
|
329
|
+
const funcName = pythonFunctionName(title);
|
|
330
|
+
const lines = [];
|
|
331
|
+
lines.push('"""');
|
|
332
|
+
lines.push(title);
|
|
333
|
+
if (ir.source) {
|
|
334
|
+
for (const line of String(ir.source).split(/\r?\n/).slice(0, 8)) {
|
|
335
|
+
lines.push(line.trim());
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
lines.push('"""');
|
|
339
|
+
lines.push('import os');
|
|
340
|
+
lines.push('');
|
|
341
|
+
lines.push('from playwright.sync_api import Page, expect');
|
|
342
|
+
for (const page of pages) {
|
|
343
|
+
lines.push(`from pages.${snake(page.name)} import ${page.name}`);
|
|
344
|
+
}
|
|
345
|
+
if (ir.baseURL) {
|
|
346
|
+
lines.push('');
|
|
347
|
+
lines.push(`BASE_URL = os.environ.get("BASE_URL", ${q(ir.baseURL)})`);
|
|
348
|
+
}
|
|
349
|
+
lines.push('');
|
|
350
|
+
lines.push('');
|
|
351
|
+
lines.push(`def ${funcName}(page: Page) -> None:`);
|
|
352
|
+
const url = (value) =>
|
|
353
|
+
ir.baseURL && String(value ?? '/').startsWith('/')
|
|
354
|
+
? `BASE_URL + ${q(value ?? '/')}`
|
|
355
|
+
: q(value ?? '/');
|
|
356
|
+
|
|
357
|
+
for (const page of pages) {
|
|
358
|
+
lines.push(` ${snake(page.name)} = ${page.name}(page)`);
|
|
359
|
+
}
|
|
360
|
+
let pageCursor = 0;
|
|
361
|
+
let current = pages[0] ?? null;
|
|
362
|
+
let methodCursor = 0;
|
|
363
|
+
for (let index = 0; index < ir.steps.length; index += 1) {
|
|
364
|
+
const step = ir.steps[index];
|
|
365
|
+
if (step.note) lines.push(` # ${step.note.replace(/\s+/g, ' ').slice(0, 160)}`);
|
|
366
|
+
if (step.action === 'goto') {
|
|
367
|
+
const target = pages.find(
|
|
368
|
+
(item, i) => i >= pageCursor && item.url === String(step.url ?? '/'),
|
|
369
|
+
);
|
|
370
|
+
if (target) {
|
|
371
|
+
pageCursor = pages.indexOf(target);
|
|
372
|
+
current = target;
|
|
373
|
+
}
|
|
374
|
+
methodCursor = 0;
|
|
375
|
+
lines.push(` page.goto(${url(step.url)})`);
|
|
376
|
+
continue;
|
|
377
|
+
}
|
|
378
|
+
if (ELEMENT_ACTIONS.has(step.action) && step.locator) {
|
|
379
|
+
const method = current?.methods[methodCursor];
|
|
380
|
+
methodCursor += 1;
|
|
381
|
+
if (method && current) {
|
|
382
|
+
const call = `${snake(current.name)}.${snake(method.name)}`;
|
|
383
|
+
if (step.action === 'fill' || step.action === 'select') {
|
|
384
|
+
lines.push(` ${call}(${pyValue(step.value)})`);
|
|
385
|
+
} else {
|
|
386
|
+
lines.push(` ${call}()`);
|
|
387
|
+
}
|
|
388
|
+
continue;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
if (step.action === 'expectUrl') {
|
|
392
|
+
lines.push(` expect(page).to_have_url(${url(step.url ?? '/')})`);
|
|
393
|
+
} else if (step.action === 'wait') {
|
|
394
|
+
lines.push(` page.wait_for_timeout(${Number(step.ms) || 500})`);
|
|
395
|
+
} else if (step.action === 'screenshot') {
|
|
396
|
+
const name = String(step.value ?? `step-${index + 1}`)
|
|
397
|
+
.replace(/[^\w.-]+/g, '-')
|
|
398
|
+
.replace(/^-+|-+$/g, '');
|
|
399
|
+
const file = `reports/ai/${name || `step-${index + 1}`}.png`;
|
|
400
|
+
lines.push(` page.screenshot(path=${q(file)}, full_page=True)`);
|
|
401
|
+
} else if (step.action === 'press') {
|
|
402
|
+
lines.push(` page.keyboard.press(${q(step.key ?? 'Enter')})`);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
if (!ir.steps.length) lines.push(' pass');
|
|
406
|
+
lines.push('');
|
|
407
|
+
return lines.join('\n');
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/** TS 登录态常量:让 spec 与项目里其它用例复用同一份 storageState */
|
|
411
|
+
function tsFixture(storageState) {
|
|
412
|
+
return [
|
|
413
|
+
'/**',
|
|
414
|
+
' * 登录态路径。',
|
|
415
|
+
' */',
|
|
416
|
+
`export const STORAGE_STATE = ${q(storageState)};`,
|
|
417
|
+
'',
|
|
418
|
+
].join('\n');
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
function normalizeDir(value, fallback) {
|
|
422
|
+
const raw = String(value ?? '')
|
|
423
|
+
.replace(/\\/g, '/')
|
|
424
|
+
.replace(/^\.?\//, '')
|
|
425
|
+
.replace(/\/+$/, '')
|
|
426
|
+
.trim();
|
|
427
|
+
return raw || fallback;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/**
|
|
431
|
+
* 生成一组待写入文件。
|
|
432
|
+
*
|
|
433
|
+
* @returns {{ files: Array<{path:string,content:string,language:string,role:string}>, configPatch: object|null }}
|
|
434
|
+
* role: 'spec' 主用例 / 'page' 页面对象 / 'fixture' 登录态常量
|
|
435
|
+
*/
|
|
436
|
+
export function emitFiles(rawIr, target, options = {}) {
|
|
437
|
+
const ir = normalizeIr(rawIr, { target });
|
|
438
|
+
const language = target === 'python' || ir.target === 'python' ? 'python' : 'ts';
|
|
439
|
+
const testDir = normalizeDir(options.testDir, 'tests');
|
|
440
|
+
const single = emitCode(ir, language);
|
|
441
|
+
const pages = splitPages(ir);
|
|
442
|
+
const files = [];
|
|
443
|
+
|
|
444
|
+
if (language === 'python') {
|
|
445
|
+
files.push({
|
|
446
|
+
path: `${testDir}/${single.filename}`,
|
|
447
|
+
content: renderPySpec(ir, pages),
|
|
448
|
+
language: 'python',
|
|
449
|
+
role: 'spec',
|
|
450
|
+
});
|
|
451
|
+
for (const page of pages) {
|
|
452
|
+
files.push({
|
|
453
|
+
path: `${testDir}/pages/${snake(page.name).replace(/_page$/, '')}_page.py`,
|
|
454
|
+
content: renderPyPage(page),
|
|
455
|
+
language: 'python',
|
|
456
|
+
role: 'page',
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
} else {
|
|
460
|
+
const fixturePath = `${testDir}/fixtures/auth-state.ts`;
|
|
461
|
+
const fixtureImport = './fixtures/auth-state';
|
|
462
|
+
const useFixture = Boolean(ir.storageState) && options.fixture !== false;
|
|
463
|
+
files.push({
|
|
464
|
+
path: `${testDir}/${single.filename}`,
|
|
465
|
+
content: renderTsSpec(ir, pages, {
|
|
466
|
+
fixturePath: useFixture ? fixturePath : '',
|
|
467
|
+
fixtureImport,
|
|
468
|
+
}),
|
|
469
|
+
language: 'ts',
|
|
470
|
+
role: 'spec',
|
|
471
|
+
});
|
|
472
|
+
for (const page of pages) {
|
|
473
|
+
files.push({
|
|
474
|
+
path: `${testDir}/pages/${page.name}.ts`,
|
|
475
|
+
content: renderTsPage(page),
|
|
476
|
+
language: 'ts',
|
|
477
|
+
role: 'page',
|
|
478
|
+
});
|
|
479
|
+
}
|
|
480
|
+
if (useFixture) {
|
|
481
|
+
files.push({
|
|
482
|
+
path: fixturePath,
|
|
483
|
+
content: tsFixture(ir.storageState),
|
|
484
|
+
language: 'ts',
|
|
485
|
+
role: 'fixture',
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
// 项目没配 baseURL 时只给建议,不替用户改 config
|
|
491
|
+
let configPatch = null;
|
|
492
|
+
if (ir.baseURL && !options.hasBaseURL) {
|
|
493
|
+
configPatch = {
|
|
494
|
+
path: language === 'python' ? 'pytest.ini' : 'playwright.config.ts',
|
|
495
|
+
reason: '项目配置里没有 baseURL,想全局生效可以补上;用例本身不依赖它',
|
|
496
|
+
snippet:
|
|
497
|
+
language === 'python'
|
|
498
|
+
? `# pytest.ini\n# addopts = --base-url ${ir.baseURL}`
|
|
499
|
+
: `// playwright.config.ts\nuse: { baseURL: ${q(ir.baseURL)} },`,
|
|
500
|
+
};
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
return { files, configPatch };
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
export { slugify, snake };
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* IR → pytest + playwright.sync_api
|
|
3
|
+
*
|
|
4
|
+
* 依赖项目自己的 pytest-playwright(提供 `page` fixture)。
|
|
5
|
+
* 浏览器二进制可以直接复用 Playwright 的全局缓存,不必重新下载。
|
|
6
|
+
*/
|
|
7
|
+
import { pythonFunctionName, slugify, splitEnvValue } from './ir.mjs';
|
|
8
|
+
|
|
9
|
+
const q = (value) => JSON.stringify(String(value ?? ''));
|
|
10
|
+
|
|
11
|
+
const PY_KEYWORDS = new Set([
|
|
12
|
+
'False',
|
|
13
|
+
'None',
|
|
14
|
+
'True',
|
|
15
|
+
'and',
|
|
16
|
+
'as',
|
|
17
|
+
'assert',
|
|
18
|
+
'async',
|
|
19
|
+
'await',
|
|
20
|
+
'break',
|
|
21
|
+
'class',
|
|
22
|
+
'continue',
|
|
23
|
+
'def',
|
|
24
|
+
'del',
|
|
25
|
+
'elif',
|
|
26
|
+
'else',
|
|
27
|
+
'except',
|
|
28
|
+
'finally',
|
|
29
|
+
'for',
|
|
30
|
+
'from',
|
|
31
|
+
'global',
|
|
32
|
+
'if',
|
|
33
|
+
'import',
|
|
34
|
+
'in',
|
|
35
|
+
'is',
|
|
36
|
+
'lambda',
|
|
37
|
+
'nonlocal',
|
|
38
|
+
'not',
|
|
39
|
+
'or',
|
|
40
|
+
'pass',
|
|
41
|
+
'raise',
|
|
42
|
+
'return',
|
|
43
|
+
'try',
|
|
44
|
+
'while',
|
|
45
|
+
'with',
|
|
46
|
+
'yield',
|
|
47
|
+
]);
|
|
48
|
+
|
|
49
|
+
function locatorCode(spec, owner = 'page') {
|
|
50
|
+
const base = (() => {
|
|
51
|
+
if (spec.kind === 'testid') return `${owner}.get_by_test_id(${q(spec.value)})`;
|
|
52
|
+
if (spec.kind === 'role') {
|
|
53
|
+
if (!spec.name) return `${owner}.get_by_role(${q(spec.role)})`;
|
|
54
|
+
const exact = spec.exact ? ', exact=True' : '';
|
|
55
|
+
return `${owner}.get_by_role(${q(spec.role)}, name=${q(spec.name)}${exact})`;
|
|
56
|
+
}
|
|
57
|
+
if (spec.kind === 'label') {
|
|
58
|
+
return `${owner}.get_by_label(${q(spec.value)}${spec.exact ? ', exact=True' : ''})`;
|
|
59
|
+
}
|
|
60
|
+
if (spec.kind === 'placeholder') {
|
|
61
|
+
return `${owner}.get_by_placeholder(${q(spec.value)}${spec.exact ? ', exact=True' : ''})`;
|
|
62
|
+
}
|
|
63
|
+
if (spec.kind === 'text') {
|
|
64
|
+
return `${owner}.get_by_text(${q(spec.value)}${spec.exact ? ', exact=True' : ''})`;
|
|
65
|
+
}
|
|
66
|
+
if (spec.kind === 'attr') {
|
|
67
|
+
const body = `[${spec.attr}="${String(spec.value).replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"]`;
|
|
68
|
+
return `${owner}.locator(${q(body)})`;
|
|
69
|
+
}
|
|
70
|
+
return `${owner}.locator(${q(spec.value)})`;
|
|
71
|
+
})();
|
|
72
|
+
if (spec.nth == null) return base;
|
|
73
|
+
return spec.nth === 0 ? `${base}.first` : `${base}.nth(${spec.nth})`;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function valueCode(value) {
|
|
77
|
+
const env = splitEnvValue(value);
|
|
78
|
+
if (env) return `os.environ.get(${q(env)}, "")`;
|
|
79
|
+
return q(value ?? '');
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function stepCode(step, index, ir) {
|
|
83
|
+
const target = step.locator ? locatorCode(step.locator) : '';
|
|
84
|
+
const url = (value) =>
|
|
85
|
+
ir.baseURL && String(value ?? '/').startsWith('/')
|
|
86
|
+
? `BASE_URL + ${q(value ?? '/')}`
|
|
87
|
+
: q(value ?? '/');
|
|
88
|
+
switch (step.action) {
|
|
89
|
+
case 'goto':
|
|
90
|
+
return ` page.goto(${url(step.url)})`;
|
|
91
|
+
case 'click':
|
|
92
|
+
return ` ${target}.click()`;
|
|
93
|
+
case 'fill':
|
|
94
|
+
return ` ${target}.fill(${valueCode(step.value)})`;
|
|
95
|
+
case 'select':
|
|
96
|
+
return ` ${target}.select_option(${valueCode(step.value)})`;
|
|
97
|
+
case 'press':
|
|
98
|
+
return step.locator
|
|
99
|
+
? ` ${target}.press(${q(step.key ?? 'Enter')})`
|
|
100
|
+
: ` page.keyboard.press(${q(step.key ?? 'Enter')})`;
|
|
101
|
+
case 'hover':
|
|
102
|
+
return ` ${target}.hover()`;
|
|
103
|
+
case 'expect': {
|
|
104
|
+
if (step.text) return ` expect(${target}).to_contain_text(${valueCode(step.text)})`;
|
|
105
|
+
if (step.state === 'hidden') return ` expect(${target}).to_be_hidden()`;
|
|
106
|
+
return ` expect(${target}).to_be_visible()`;
|
|
107
|
+
}
|
|
108
|
+
case 'expectUrl':
|
|
109
|
+
return ` expect(page).to_have_url(${url(step.url)})`;
|
|
110
|
+
case 'wait':
|
|
111
|
+
return ` page.wait_for_timeout(${Number(step.ms) || 500})`;
|
|
112
|
+
case 'screenshot': {
|
|
113
|
+
const name = String(step.value ?? `step-${index + 1}`)
|
|
114
|
+
.replace(/[^\w.-]+/g, '-')
|
|
115
|
+
.replace(/^-+|-+$/g, '');
|
|
116
|
+
return ` page.screenshot(path=${q(`reports/ai/${name || `step-${index + 1}`}.png`)}, full_page=True)`;
|
|
117
|
+
}
|
|
118
|
+
default:
|
|
119
|
+
return '';
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** 生成 Python 脚本;`ir` 需先过 normalizeIr */
|
|
124
|
+
export function renderPython(ir) {
|
|
125
|
+
const title = ir.title || 'AI 生成用例';
|
|
126
|
+
const funcName = PY_KEYWORDS.has(pythonFunctionName(title))
|
|
127
|
+
? 'test_ai_generated'
|
|
128
|
+
: pythonFunctionName(title);
|
|
129
|
+
const lines = [];
|
|
130
|
+
lines.push('"""');
|
|
131
|
+
lines.push(title);
|
|
132
|
+
if (ir.source) {
|
|
133
|
+
for (const line of String(ir.source).split(/\r?\n/).slice(0, 8)) {
|
|
134
|
+
lines.push(line.trim());
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
lines.push('"""');
|
|
138
|
+
lines.push('import os');
|
|
139
|
+
lines.push('');
|
|
140
|
+
lines.push('from playwright.sync_api import Page, expect');
|
|
141
|
+
if (ir.baseURL) {
|
|
142
|
+
lines.push('');
|
|
143
|
+
// 相对路径靠这里拼成完整地址:不依赖项目的 pytest-playwright --base-url 配置
|
|
144
|
+
lines.push(`BASE_URL = os.environ.get("BASE_URL", ${q(ir.baseURL)})`);
|
|
145
|
+
}
|
|
146
|
+
lines.push('');
|
|
147
|
+
lines.push('');
|
|
148
|
+
lines.push(`def ${funcName}(page: Page) -> None:`);
|
|
149
|
+
for (let index = 0; index < ir.steps.length; index += 1) {
|
|
150
|
+
const step = ir.steps[index];
|
|
151
|
+
if (step.note) lines.push(` # ${step.note.replace(/\s+/g, ' ').slice(0, 160)}`);
|
|
152
|
+
const code = stepCode(step, index, ir);
|
|
153
|
+
if (code) lines.push(code);
|
|
154
|
+
}
|
|
155
|
+
if (!ir.steps.length) lines.push(' pass');
|
|
156
|
+
lines.push('');
|
|
157
|
+
return { code: lines.join('\n'), filename: `${funcName}.py`, language: 'python' };
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export { slugify, locatorCode, valueCode };
|