(
'[data-state="open"] input[type="color"]',
);
if (!picker) throw new Error("自定义取色器没有出现");
Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")?.set?.call(
picker,
"#ff0000",
);
picker.dispatchEvent(new Event("input", { bubbles: true }));
});
await browser.keys("Escape");
await waitForSource(
(source) => source.includes("--veil: rgba(255, 0, 0, 0.5);"),
"半透明变量只改颜色,写回应保留 0.5 的不透明度",
);
// 内联 style 里有 CSS 注释(声明之间的带分号、冒号,值里也有一段):改别的属性,注释和其余声明原样保留(!3450 CR-03)
await selectGuestElement(LOCAL_GUEST_PREFIX, "#note");
await waitForWritable("true", "带注释 style 的元素应能写回");
await fillField("[data-design-field=fontSize]", "20");
await waitForSource(
(source) =>
source.includes(
'',
),
"带注释的内联 style 写回后注释与其余声明应原样保留",
);
await waitForGuest("getComputedStyle(document.getElementById('note')).paddingTop", "8px");
await waitForGuest(
"getComputedStyle(document.getElementById('note')).color",
"rgb(17, 17, 17)",
);
// 尾部注释没闭合:补上 */ 再追加,追加的声明不能被吞进注释(源码里也要生效,!3450 SG-01)
// 原来带 !important 的内联声明:改值保留 !important,页面上仍压过样式表里的 !important(!3450 CR-01)
await selectGuestElement(LOCAL_GUEST_PREFIX, "#imp");
await waitForWritable("true", "带 !important 的元素应能写回");
await fillField("[data-design-field=fontSize]", "22");
await waitForSource(
(source) => source.includes('
'),
"改带 !important 的声明,写回应保留 !important",
);
await waitForGuest("getComputedStyle(document.getElementById('imp')).fontSize", "22px");
// 简写带 !important(background: red !important)时改填充:新值也带 !important 并写在后面,源码渲染与预览一致(!3450 SG-01)
await selectGuestElement(LOCAL_GUEST_PREFIX, "#bgimp");
await waitForWritable("true", "简写带 !important 的元素应能写回");
await fillField("[data-design-field=background]", "0000FF");
await waitForSource(
(source) =>
source.includes(
'"),
{ 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 noticeE2E_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}` },
);
}