import { chmod, mkdtemp, readFile, rm, stat, utimes, writeFile } from "node:fs/promises"; import type { Server } from "node:http"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { pathToFileURL } from "node:url"; import { Key } from "webdriverio"; import { TID_BROWSER_DESIGN_INSPECTOR, TID_BROWSER_DESIGN_PANEL_BUTTON, TID_BROWSER_DESIGN_SLIDE_COUNTER, TID_BROWSER_DESIGN_SLIDE_NEXT, TID_BROWSER_DESIGN_SOURCE_STATUS, TID_BROWSER_DESIGN_TOOLBAR, TID_BROWSER_ELEMENT_PICKER_BUTTON, TID_BROWSER_REFRESH_BUTTON, TID_SIDE_PANE_FULL_VIEW_HOST, TID_WEB_ELEMENT_CONTEXT_CHIP, } from "@zcode/shared/test-ids"; import { clearAppData, clickTestIdByDom } from "#e2e/helpers/desktop-app"; import { clickCloseButton, openFirstBrowserTab, readBrowserTabIds, waitForTabCount, } from "#e2e/helpers/browser-side-pane"; import { enableDesignPlugin, clickGuestElement, ELEMENT_TOKEN_SELECTOR, closeFixtureServer, countElementTokens, deleteLastElementToken, guestEval, hasReadyAttachment, holdGuestCapture, navigateBrowser, pressGuestUndo, readHeldCaptureCalls, releaseHeldCapture, selectGuestElement, startFixtureServer, typeAfterLastElementToken, readDataCount, waitForElementTokens, waitForReadyAttachment, waitForTestIdText, } from "#e2e/helpers/browser-design-mode"; import { waitForUpstreamRequest } from "#e2e/helpers/conversation-session-network"; import { clickV4Send, prepareV4ConversationE2E } from "#e2e/helpers/v4-conversation"; // 内置浏览器设计工具(docs/browser-use/2026-11-03-browser-design-mode-spec.md): // 普通页面不出设计工具行;设计稿页面自动出现。点选 = 选中元素作为网页元素引用进输入框(Shift 多选、普通点击替换), // 属性面板改数值直接写回本机 HTML 源文件、页面不刷新,Cmd/Ctrl + Z 撤销、加 Shift 重做。 // 页面里的点击用 main 侧 sendInputEvent 发真实鼠标事件,与用户在 guest 里点击同一条路径。 const MOD = process.platform === "darwin" ? Key.Command : Key.Control; /** 本机 HTML 经 zcode-html:// 预览协议加载 */ const LOCAL_GUEST_PREFIX = "zcode-html://"; const PLAIN_HTML = `E2E plain page

E2E_PLAIN

`; const FIXTURE_HTML = ` E2E design mode

E2E_DESIGN_TITLE

E2E_NOTE

E2E_TODO

E2E_IMP

Second

Third

`; let fixtureServer: Server | null = null; let fixtureOrigin = ""; let fixtureUrl = ""; let localDir = ""; let localFile = ""; /** 属性面板里的选中数;没有选中时面板收起、读不到,按 0 算 */ async function readInspectorSelectedCount(): Promise { return (await browser.execute( (id) => document.querySelector(`[data-testid="${id}"]`)?.getAttribute("data-selected-count") ?? "0", TID_BROWSER_DESIGN_INSPECTOR, )) as string; } /** 同一个 app 里连跑多条用例:关掉当前界面上的浏览器 tab,下一条从一个 tab 起步,也不留隐藏的旧地址栏、旧页面。 */ async function closeBrowserTabs(): Promise { for (const tabId of await readBrowserTabIds()) await clickCloseButton(tabId); await waitForTabCount(0); } async function openFreshBrowserTab(): Promise { await closeBrowserTabs(); await clearElementTokens(); await openFirstBrowserTab(); } /** 草稿输入框跨用例保留:像用户一样全选删掉上一条留下的元素引用(程序改写不撤元素上下文,新选中时旧引用会被补回来) */ async function clearElementTokens(): Promise { for (let attempt = 0; attempt < 5 && (await countElementTokens()) > 0; attempt += 1) { await browser.execute((selector) => { document.querySelector(selector)?.closest("[contenteditable=true]")?.focus(); }, ELEMENT_TOKEN_SELECTOR); await browser.keys([MOD, "a"]); await browser.keys("Backspace"); await browser.pause(300); } await waitForElementTokens(0, "清空输入框后不应还留着元素引用"); } describe("BU-E2E-021:内置浏览器设计工具", () => { before(async () => { ({ server: fixtureServer, origin: fixtureOrigin } = await startFixtureServer( (request, response) => { response.setHeader("content-type", "text/html; charset=utf-8"); response.end(request.url?.startsWith("/plain") ? PLAIN_HTML : FIXTURE_HTML); }, )); fixtureUrl = `${fixtureOrigin}/design`; localDir = await mkdtemp(join(tmpdir(), "zcode-e2e-design-")); localFile = join(localDir, "design.html"); }); // 首发后界面在新任务里,它的浏览器会常驻(隐藏):在这里关掉,后面用例的 $() 才不会拿到它的元素 afterEach(async () => { // 先在还开着的输入框里删掉引用,再关 tab:关掉后那份引用就看不到、删不着了 await clearElementTokens().catch(() => undefined); await closeBrowserTabs().catch(() => undefined); }); after(async () => { await closeFixtureServer(fixtureServer); fixtureServer = null; if (localDir) await rm(localDir, { recursive: true, force: true }); await clearAppData(); }); it("设计工具随页面出现;点选元素进输入框成引用(Shift 多选、普通点击替换),翻页,首发带引用与截图", async function () { this.timeout(240_000); await prepareV4ConversationE2E(); await enableDesignPlugin(); await openFreshBrowserTab(); // 普通页面:没有设计稿信号,不出设计工具行。 await navigate(`${fixtureOrigin}/plain`, "E2E plain page"); await browser.pause(1_500); if (await $(`[data-testid="${TID_BROWSER_DESIGN_TOOLBAR}"]`).isExisting()) { throw new Error("普通页面不应出现设计工具行"); } // 本机 HTML 没有 data-zcode-design、只有 :root 变量:经 zcode-html:// 预览打开也算本机页面,自动出现设计工具(!3450 CR-01) const variablesFile = join(localDir, "variables.html"); await writeFile( variablesFile, `E2E local variables

E2E_VARIABLES

`, "utf-8", ); await navigateBrowser( pathToFileURL(variablesFile).href, "E2E local variables", LOCAL_GUEST_PREFIX, ); await $(`[data-testid="${TID_BROWSER_DESIGN_TOOLBAR}"]`).waitForDisplayed({ timeout: 15_000, timeoutMsg: "本机带 :root 变量的页面应自动出现设计工具行", }); // 设计稿页面:自动出现设计工具行与翻页。 await navigate(fixtureUrl, "E2E design mode"); await $(`[data-testid="${TID_BROWSER_DESIGN_TOOLBAR}"]`).waitForDisplayed({ timeout: 15_000, timeoutMsg: "设计稿页面没有自动出现设计工具行", }); await waitForTestIdText(TID_BROWSER_DESIGN_SLIDE_COUNTER, "1 / 3"); // 点 #cta:属性面板打开,输入框出现 1 个元素引用 + element-1.png await selectGuestElement(fixtureOrigin, "#cta"); await waitForElementTokens(1, "点选后输入框没有出现元素引用"); // 选中元素不把对话挤成浮窗:留在半屏,面板在画布右侧打开(11-11 用户反馈) await $(`[data-testid="${TID_BROWSER_DESIGN_INSPECTOR}"]`).waitForDisplayed({ timeout: 10_000, timeoutMsg: "设计稿选中元素后应打开调整面板", }); await browser.pause(1_000); await waitForFullView(false, "选中元素后不应进完整视图"); await waitForReadyAttachment("element-1.png"); // http 页面不是本机文件:面板写明不能直接写回 await browser.waitUntil( async () => (await $(`[data-testid="${TID_BROWSER_DESIGN_SOURCE_STATUS}"]`).getAttribute( "data-writable", )) === "false", { timeout: 10_000, timeoutMsg: "http 页面不应能写回源文件" }, ); // Shift + 点 #title:两个元素都在 await selectGuestElement(fixtureOrigin, "#title", { shift: true, selectedCount: "2" }); await waitForElementTokens(2, "Shift 点选后输入框不是 2 个元素"); await waitForReadyAttachment("element-2.png"); // 普通点 #cta:替换选中,#title 的引用和截图撤回 await selectGuestElement(fixtureOrigin, "#cta"); await waitForElementTokens(1, "普通点击后应只剩 1 个元素引用"); await browser.waitUntil( async () => !(await hasReadyAttachment("element-2.png")) && (await hasReadyAttachment("element-1.png")), { timeout: 10_000, timeoutMsg: "替换选中后截图没有对应:应只剩 element-1.png" }, ); // 翻页:下一页 → 计数 2 / 3,页面滚到第二个 slide。 await clickTestIdByDom(TID_BROWSER_DESIGN_SLIDE_NEXT); await waitForTestIdText(TID_BROWSER_DESIGN_SLIDE_COUNTER, "2 / 3"); await browser.waitUntil( async () => Number( await guestEval( fixtureOrigin, "Math.round(document.getElementById('s2').getBoundingClientRect().top)", ), ) === 0, { timeout: 10_000, timeoutMsg: "翻页后页面没有滚到第二个 slide" }, ); // 草稿首发:上游请求带元素引用块与截图附件占位(E2E 模型不支持图片时 CLI 只留占位) await typeAfterLastElementToken(" E2E_DESIGN_SEND make it bigger"); await clickV4Send(); await waitForUpstreamRequest( { includes: [ "E2E_DESIGN_SEND", "# Web page elements:", "E2E_DESIGN_CTA", "](zcode-element://", "[Attached image/png: element-1.png]", ], }, "草稿首发的上游请求里没有元素引用或 element-1.png 截图附件", 45_000, ); }); it("截图在途时换了选中:不加已取消选中的元素,只引用最后选中的", async function () { this.timeout(120_000); await prepareV4ConversationE2E(); await enableDesignPlugin(); await openFreshBrowserTab(); await navigate(fixtureUrl, "E2E design mode"); await $(`[data-testid="${TID_BROWSER_DESIGN_TOOLBAR}"]`).waitForDisplayed({ timeout: 15_000 }); await holdGuestCapture(fixtureOrigin); try { await selectGuestElement(fixtureOrigin, "#cta"); await browser.waitUntil(async () => (await readHeldCaptureCalls()) >= 1, { timeout: 10_000, timeoutMsg: "点选后没有开始截图", }); await selectGuestElement(fixtureOrigin, "#title"); } finally { await releaseHeldCapture(); } await waitForElementTokens(1, "截图回来后应只引用 #title"); await browser.pause(1_500); const tokens = await countElementTokens(); if (tokens !== 1) throw new Error(`截图在途时取消选中的元素不应进输入框,实际 ${tokens} 个`); }); it("属性面板改数值直接写回本机 HTML,页面不刷新;Cmd/Ctrl + Z 撤销、Shift 重做;页面变量写 :root;脚本生成的元素只读", async function () { this.timeout(240_000); await writeFile(localFile, FIXTURE_HTML, "utf-8"); await prepareV4ConversationE2E(); await enableDesignPlugin(); await openFreshBrowserTab(); await navigateBrowser(pathToFileURL(localFile).href, "E2E design mode", LOCAL_GUEST_PREFIX); await guestEval(LOCAL_GUEST_PREFIX, "window.__e2eMark = 1"); await selectGuestElement(LOCAL_GUEST_PREFIX, "#title"); await waitForWritable("true", "本机 HTML 里的标题应能写回"); await fillField("[data-design-field=fontSize]", "46"); await waitForSource( (source) => source === FIXTURE_HTML.replace('

', '

'), "改字号后源文件应只多了标题的 style", ); await waitForGuest("getComputedStyle(document.getElementById('title')).fontSize", "46px"); // 焦点还在字段上(没有未提交的输入):Cmd/Ctrl + Z 撤销的是写回 await browser.keys([MOD, "z"]); await waitForSource((source) => source === FIXTURE_HTML, "撤销后源文件应回到原样"); await waitForGuest("getComputedStyle(document.getElementById('title')).fontSize", "40px"); await waitForFieldValue("[data-design-field=fontSize]", "40"); await browser.keys([MOD, Key.Shift, "z"]); await waitForSource( (source) => source.includes('

'), "重做后源文件应再次是 46px", ); // 页面上的真实 Cmd/Ctrl + Z(guest preload 收 isTrusted 按键,!3450 CR-01):焦点在页面空白处时撤销 / 重做写回 const WRITTEN = FIXTURE_HTML.replace( '

', '

', ); await pressGuestUndo(LOCAL_GUEST_PREFIX); await waitForSource((source) => source === FIXTURE_HTML, "页面上按撤销应还原源文件"); await pressGuestUndo(LOCAL_GUEST_PREFIX, { redo: true }); await waitForSource((source) => source === WRITTEN, "页面上按重做应再次写回"); // 页面脚本伪造的按键(isTrusted 为 false)不触发写回 await guestEval( LOCAL_GUEST_PREFIX, `(() => { for (const target of [window, document, document.body]) target.dispatchEvent(new KeyboardEvent("keydown", { key: "z", metaKey: true, ctrlKey: true, bubbles: true })); return true; })()`, ); await browser.pause(1_500); if ((await readFile(localFile, "utf-8")) !== WRITTEN) { throw new Error("页面伪造的撤销按键不应改动源文件"); } // 焦点在页面自己的输入框里(含 open Shadow DOM 里的):Cmd/Ctrl + Z 归页面撤销输入,不撤写回 await guestEval( LOCAL_GUEST_PREFIX, `(() => { const input = document.createElement("input"); input.id = "e2e-page-input"; document.body.prepend(input); const host = document.createElement("div"); host.id = "e2e-shadow-host"; document.body.prepend(host); host.attachShadow({ mode: "open", delegatesFocus: true }).append(document.createElement("input")); return true; })()`, ); await pressGuestUndo(LOCAL_GUEST_PREFIX, { focusSelector: "#e2e-page-input" }); await pressGuestUndo(LOCAL_GUEST_PREFIX, { focusSelector: "#e2e-shadow-host" }); await browser.pause(1_500); if ((await readFile(localFile, "utf-8")) !== WRITTEN) { throw new Error("焦点在页面输入框里时按撤销不应改动源文件"); } // 插进去的两个节点要删掉:写回按 往下每级的兄弟数对源码,多出节点后整页元素都会判成写不回 await guestEval( LOCAL_GUEST_PREFIX, `(() => { document.getElementById("e2e-page-input")?.remove(); document.getElementById("e2e-shadow-host")?.remove(); return true; })()`, ); // 多选后改圆角:两个元素都写回 await selectGuestElement(LOCAL_GUEST_PREFIX, "#cta", { shift: true, selectedCount: "2" }); await waitForWritable("true", "两个元素都应能写回"); await fillField("[data-design-field=borderRadius]", "4"); await waitForSource( (source) => source.includes('

') && source.includes('', "utf-8", ); await navigateBrowser(pathToFileURL(styledFile).href, "E2E styled", LOCAL_GUEST_PREFIX); await selectGuestElement(LOCAL_GUEST_PREFIX, "#box"); await waitForWritable("true", "标题应能写回"); await fillField("[data-design-field=fontSize]", "30"); await waitForGuest("getComputedStyle(document.getElementById('box')).fontSize", "30px"); await browser.pause(1_000); await guestEval(LOCAL_GUEST_PREFIX, "window.__e2eStyledMark = 1"); await Promise.all([ writeFile(cssFile, "#box { color: rgb(255, 0, 0); }", "utf-8"), writeFile(join(localDir, "styled-other.txt"), "E2E_OTHER", "utf-8"), ]); await waitForGuest("getComputedStyle(document.getElementById('box')).color", "rgb(255, 0, 0)"); if ((await guestEval(LOCAL_GUEST_PREFIX, "window.__e2eStyledMark")) === 1) { throw new Error("自写之后外部改了 CSS,页面应自动刷新"); } // !3450 SG-01:按钮带 text-transform: uppercase,在面板里改字,源码按原来的大小写写回 await selectGuestElement(LOCAL_GUEST_PREFIX, "#cta"); await waitForWritable("true", "按钮应能写回"); await fillField("[data-design-field=text]", "Submit orders"); await browser.waitUntil( async () => (await readFile(styledFile, "utf-8")).includes(">Submit orders"), { timeout: 10_000, timeoutMsg: "面板改字应按输入的大小写写回(不是 SUBMIT ORDERS)" }, ); }); it("输入框不收元素引用时,每轮点选只提示一次:写回重读选中不再弹,退出点选后再选会再提示", async function () { this.timeout(180_000); await prepareV4ConversationE2E(); await enableDesignPlugin(); await openFreshBrowserTab(); const noticeFile = join(localDir, "notice.html"); await writeFile( noticeFile, 'E2E notice

E2E_A

E2E_B

', "utf-8", ); await navigateBrowser(pathToFileURL(noticeFile).href, "E2E notice", LOCAL_GUEST_PREFIX); // !3548 第五轮 CR-01:「加引用」一律按输入框不收处理;同时数提示弹了几次 await browser.execute(() => { const w = window as unknown as { __e2eNotices: number; __e2eDispatch: typeof window.dispatchEvent; }; w.__e2eNotices = 0; w.__e2eDispatch = window.dispatchEvent.bind(window); // 「加引用」事件不派发、按没人收下返回(dispatchEvent 未被 preventDefault 时返回 true) window.dispatchEvent = (event: Event) => event.type === "zcode:web-element-context-add-to-chat" ? true : w.__e2eDispatch(event); new MutationObserver((records) => { for (const record of records) { for (const node of record.addedNodes) { const text = node.textContent ?? ""; if (/当前没有可接收的输入框|No composer can receive/.test(text)) w.__e2eNotices += 1; } } }).observe(document.body, { childList: true, subtree: true }); }); const readNotices = async () => (await browser.execute( () => (window as unknown as { __e2eNotices: number }).__e2eNotices, )) as number; await selectGuestElement(LOCAL_GUEST_PREFIX, "#a"); await browser.waitUntil(async () => (await readNotices()) >= 1, { timeout: 10_000, timeoutMsg: "输入框不收引用时应提示一次", }); await waitForWritable("true", "标题应能写回"); await fillField("[data-design-field=fontSize]", "31"); await waitForGuest("getComputedStyle(document.getElementById('a')).fontSize", "31px"); await fillField("[data-design-field=fontSize]", "32"); await waitForGuest("getComputedStyle(document.getElementById('a')).fontSize", "32px"); await browser.pause(1_500); if ((await readNotices()) !== 1) { throw new Error(`同一轮点选里写回后不应再提示,实际弹了 ${await readNotices()} 次`); } // 退出点选再进来选:新一轮,再提示一次 await clickTestIdByDom(TID_BROWSER_ELEMENT_PICKER_BUTTON); await selectGuestElement(LOCAL_GUEST_PREFIX, "#b"); await browser.waitUntil(async () => (await readNotices()) === 2, { timeout: 10_000, timeoutMsg: "退出点选后重新选中应再提示一次", }); // 只拒一部分(#b):#a 进了输入框、#b 被拒时提示一次,之后写回重读选中仍不再弹(一次性提示的 guard)。 // 注:从界面点选每次对账只新增一个元素,同一次对账「有收有拒」走不到,accepted && !rejected 那条分支这里测不到。 await browser.execute(() => { const w = window as unknown as { __e2eDispatch: typeof window.dispatchEvent }; window.dispatchEvent = (event: Event) => event.type === "zcode:web-element-context-add-to-chat" && JSON.stringify((event as CustomEvent).detail ?? {}).includes("E2E_B") ? true : w.__e2eDispatch(event); }); await clickTestIdByDom(TID_BROWSER_ELEMENT_PICKER_BUTTON); await selectGuestElement(LOCAL_GUEST_PREFIX, "#a"); await selectGuestElement(LOCAL_GUEST_PREFIX, "#b", { shift: true, selectedCount: "2" }); await browser.waitUntil(async () => (await readNotices()) === 3, { timeout: 10_000, timeoutMsg: "部分不收时应提示一次", }); await fillField("[data-design-field=fontSize]", "33"); await waitForGuest("getComputedStyle(document.getElementById('b')).fontSize", "33px"); await fillField("[data-design-field=fontSize]", "34"); await waitForGuest("getComputedStyle(document.getElementById('b')).fontSize", "34px"); await browser.pause(1_500); if ((await readNotices()) !== 3) { throw new Error(`部分收下时写回后不应再提示,实际累计 ${await readNotices()} 次`); } await browser.execute(() => { const w = window as unknown as { __e2eDispatch: typeof window.dispatchEvent }; window.dispatchEvent = w.__e2eDispatch; }); }); it("再点一次「选择元素」退出点选:页面点击不再被拦截、引用留在输入框;普通网页也是同一套点选,调整面板只读", async function () { this.timeout(180_000); await prepareV4ConversationE2E(); await enableDesignPlugin(); await openFreshBrowserTab(); await navigate(fixtureUrl, "E2E design mode"); await $(`[data-testid="${TID_BROWSER_DESIGN_TOOLBAR}"]`).waitForDisplayed({ timeout: 15_000 }); // 在输入框正文里删掉元素 token:页面上同步取消选中 await selectGuestElement(fixtureOrigin, "#title"); await waitForElementTokens(1, "点选后没有元素引用"); await deleteLastElementToken(); await browser.waitUntil(async () => (await readInspectorSelectedCount()) === "0", { timeout: 5_000, timeoutMsg: "在输入框删掉引用后页面上应取消选中", }); await selectGuestElement(fixtureOrigin, "#cta"); await waitForElementTokens(1, "点选后没有元素引用"); await clickTestIdByDom(TID_BROWSER_ELEMENT_PICKER_BUTTON); await browser.waitUntil( async () => (await guestEval(fixtureOrigin, "document.documentElement.style.cursor")) === "", { timeout: 5_000, timeoutMsg: "再点「选择元素」后页面仍停在选择状态" }, ); await browser.waitUntil(async () => (await readInspectorSelectedCount()) === "0", { timeout: 5_000, timeoutMsg: "退出点选后应取消选中", }); await waitForElementTokens(1, "退出点选后输入框里的引用应留着"); await guestEval( fixtureOrigin, "window.__e2eClicks = 0; document.getElementById('cta').addEventListener('click', (e) => { if (!e.defaultPrevented) window.__e2eClicks += 1; }); true", ); await clickGuestElement(fixtureOrigin, "#cta"); await browser.waitUntil( async () => (await guestEval(fixtureOrigin, "window.__e2eClicks")) === 1, { timeout: 5_000, timeoutMsg: "退出点选后页面按钮的点击被拦截了", }, ); // 普通网页:不出设计工具行;「选择元素」和设计稿是同一套点选(11-06 统一): // 元素进输入框正文成 token、不出原来的元素标签,调整面板只读并说明交给 Agent await navigate(`${fixtureOrigin}/plain`, "E2E plain page"); await browser.pause(1_000); const tokensBefore = await countElementTokens(); await selectGuestElement(fixtureOrigin, "#plain", { plainPage: true }); await waitForElementTokens(tokensBefore + 1, "普通网页点选后元素应进输入框成 token"); if ((await readDataCount(TID_WEB_ELEMENT_CONTEXT_CHIP)) !== null) { throw new Error("普通网页点选后不应再出原来的元素标签"); } if (await $(`[data-testid="${TID_BROWSER_DESIGN_TOOLBAR}"]`).isExisting()) { throw new Error("普通网页不应出现设计工具行"); } // 普通网页点选只为加进聊天:不进完整视图、不弹调整面板挡页面(11-11 用户反馈) await browser.pause(1_000); await waitForFullView(false, "普通网页点选后不应进完整视图"); if (await $(`[data-testid="${TID_BROWSER_DESIGN_INSPECTOR}"]`).isExisting()) { throw new Error("普通网页点选后不应打开调整面板"); } // 点选开着换页:退出点选、面板收起,新页面不再拦截点击(!3548 CR-01) await navigate(fixtureUrl, "E2E design mode"); await $(`[data-testid="${TID_BROWSER_DESIGN_INSPECTOR}"]`).waitForExist({ reverse: true, timeout: 10_000, timeoutMsg: "换页后调整面板应收起", }); await browser.waitUntil( async () => (await $(`[data-testid="${TID_BROWSER_ELEMENT_PICKER_BUTTON}"]`).getAttribute( "aria-pressed", )) === "false", { timeout: 5_000, timeoutMsg: "换页后应退出点选" }, ); // 设计稿 A 选中后换到同样结构的设计稿 B 再刷新:不能把 A 的选中按选择器推回 B(!3548 第二轮 CR-01) await selectGuestElement(fixtureOrigin, "#cta"); await navigate(`${fixtureOrigin}/design-b`, "E2E design mode"); await $(`[data-testid="${TID_BROWSER_DESIGN_INSPECTOR}"]`).waitForExist({ reverse: true, timeout: 10_000, timeoutMsg: "换到设计稿 B 后调整面板应收起", }); await clickTestIdByDom(TID_BROWSER_REFRESH_BUTTON); await browser.pause(2_000); // 重新进点选、手动打开调整面板看宿主记着的选中:只能是 0 个 await clickTestIdByDom(TID_BROWSER_ELEMENT_PICKER_BUTTON); await clickTestIdByDom(TID_BROWSER_DESIGN_PANEL_BUTTON); await $(`[data-testid="${TID_BROWSER_DESIGN_INSPECTOR}"]`).waitForExist({ timeout: 10_000, timeoutMsg: "点工具行的「调整面板」后面板没有打开", }); if ((await readInspectorSelectedCount()) !== "0") { throw new Error("刷新设计稿 B 后宿主还留着设计稿 A 的选中"); } // 单页应用换路由(pushState)不重新注入:同样要退出点选、收起面板(!3548 第三轮) await selectGuestElement(fixtureOrigin, "#cta"); await guestEval(fixtureOrigin, "history.pushState({}, '', '/design-c'); true"); await $(`[data-testid="${TID_BROWSER_DESIGN_INSPECTOR}"]`).waitForExist({ reverse: true, timeout: 10_000, timeoutMsg: "单页换路由后调整面板应收起", }); await browser.waitUntil( async () => (await $(`[data-testid="${TID_BROWSER_ELEMENT_PICKER_BUTTON}"]`).getAttribute( "aria-pressed", )) === "false", { timeout: 5_000, timeoutMsg: "单页换路由后应退出点选" }, ); // 前进 / 后退(popstate)同样退出:后退回 /design-b await selectGuestElement(fixtureOrigin, "#cta"); await guestEval(fixtureOrigin, "history.back(); true"); await $(`[data-testid="${TID_BROWSER_DESIGN_INSPECTOR}"]`).waitForExist({ reverse: true, timeout: 10_000, timeoutMsg: "后退后调整面板应收起", }); await browser.waitUntil( async () => (await $(`[data-testid="${TID_BROWSER_ELEMENT_PICKER_BUTTON}"]`).getAttribute( "aria-pressed", )) === "false", { timeout: 5_000, timeoutMsg: "后退后应退出点选" }, ); }); }); const navigate = (url: string, title: string) => navigateBrowser(url, title, fixtureOrigin); async function waitForWritable(expected: "true" | "false", timeoutMsg: string): Promise { await browser.waitUntil( async () => (await $(`[data-testid="${TID_BROWSER_DESIGN_SOURCE_STATUS}"]`).getAttribute( "data-writable", )) === expected, { timeout: 10_000, timeoutMsg }, ); } /** 面板字段里输入值后回车提交(焦点留在字段上) */ async function fillField(selector: string, value: string): Promise { const field = await $(selector); await field.waitForEnabled({ timeout: 10_000, timeoutMsg: `${selector} 不可编辑` }); // 刚用 Esc 关掉的取色弹层收起时会把焦点还给它的触发按钮:等弹层真正卸掉再聚焦,免得敲到一半焦点被拿走 await browser.waitUntil( async () => (await browser.execute( () => document.querySelector("[data-radix-popper-content-wrapper]") === null, )) as boolean, { timeout: 5_000, timeoutMsg: "取色弹层没有收起" }, ); // 完整视图里浮动输入框可能盖住面板下沿,按 DOM 聚焦并全选,不靠坐标点击 // 焦点可能刚被页面(webview)或收起的弹层拿走:聚焦后停一下,焦点还在这个字段上再敲字 await browser.waitUntil( async () => { await browser.execute((target) => { const input = document.querySelector(target); input?.scrollIntoView({ block: "center" }); input?.focus(); input?.select(); }, selector); await browser.pause(150); return (await browser.execute( (target) => document.activeElement === document.querySelector(target), selector, )) as boolean; }, { timeout: 5_000, timeoutMsg: `${selector} 拿不到焦点` }, ); await browser.keys(value.split("")); await browser.keys("Enter"); } async function waitForFullView(expected: boolean, timeoutMsg: string): Promise { await browser.waitUntil( async () => { const mode = await $(`[data-testid="${TID_SIDE_PANE_FULL_VIEW_HOST}"]`).getAttribute( "data-side-pane-full-view-host", ); return (mode !== null && mode !== "normal") === expected; }, { timeout: 10_000, timeoutMsg }, ); } async function waitForFieldValue(selector: string, value: string): Promise { await browser.waitUntil(async () => (await $(selector).getValue()) === value, { timeout: 10_000, timeoutMsg: `${selector} 应显示 ${value}`, }); } async function waitForSource( predicate: (source: string) => boolean, timeoutMsg: string, ): Promise { let last = ""; try { await browser.waitUntil( async () => { last = await readFile(localFile, "utf-8"); return predicate(last); }, { timeout: 10_000 }, ); } catch { throw new Error(`${timeoutMsg};当前源文件:\n${last}`); } } async function waitForGuest(expression: string, expected: string): Promise { await browser.waitUntil( async () => (await guestEval(LOCAL_GUEST_PREFIX, expression)) === expected, { timeout: 10_000, timeoutMsg: `页面上 ${expression} 应为 ${expected}` }, ); }