























@@ -6,18 +6,12 @@ import "../test-support/browser-security.mock.js";
66import {
77type AriaSnapshotNode,
88captureScreenshot,
9-captureScreenshotPng,
109createTargetViaCdp,
11-type DomSnapshotNode,
1210evaluateJavaScript,
1311formatAriaSnapshot,
14-getDomText,
1512normalizeCdpWsUrl,
16-type QueryMatch,
17-querySelector,
1813type RawAXNode,
1914snapshotAria,
20-snapshotDom,
2115snapshotRoleViaCdp,
2216} from "./cdp.js";
2317@@ -164,27 +158,6 @@ describe("cdp internal", () => {
164158expect(buf.toString("utf8")).toBe("PNGDATA");
165159});
166160167-it("captureScreenshotPng forwards to the png captureScreenshot flow", async () => {
168-const server = await startMockWsServer((msg, socket) => {
169-if (msg.method === "Page.enable") {
170-socket.send(JSON.stringify({ id: msg.id, result: {} }));
171-return;
172-}
173-if (msg.method === "Page.captureScreenshot") {
174-expect(msg.params?.format).toBe("png");
175-socket.send(
176-JSON.stringify({
177-id: msg.id,
178-result: { data: Buffer.from("WRAPPED").toString("base64") },
179-}),
180-);
181-}
182-});
183-wss = server.wss;
184-const buf = await captureScreenshotPng({ wsUrl: server.wsUrl });
185-expect(buf.toString("utf8")).toBe("WRAPPED");
186-});
187-188161it("clamps out-of-range JPEG quality values into [0, 100]", async () => {
189162const { observed } = await captureScreenshotAndObserveParams({
190163format: "jpeg",
@@ -730,257 +703,6 @@ describe("cdp internal", () => {
730703});
731704});
732705733-describe("snapshotDom", () => {
734-it("returns the nodes array from the evaluated expression", async () => {
735-const server = await startMockWsServer((msg, socket) => {
736-if (msg.method === "Runtime.enable") {
737-socket.send(JSON.stringify({ id: msg.id, result: {} }));
738-return;
739-}
740-if (msg.method === "Runtime.evaluate") {
741-const fake: DomSnapshotNode[] = [{ ref: "n1", parentRef: null, depth: 0, tag: "html" }];
742-socket.send(
743-JSON.stringify({
744-id: msg.id,
745-result: { result: { value: { nodes: fake } } },
746-}),
747-);
748-}
749-});
750-wss = server.wss;
751-const snap = await snapshotDom({ wsUrl: server.wsUrl, limit: 10, maxTextChars: 200 });
752-expect(snap.nodes[0]?.tag).toBe("html");
753-});
754-755-it("returns an empty nodes array when the value is not an object", async () => {
756-const server = await startMockWsServer((msg, socket) => {
757-if (msg.method === "Runtime.enable") {
758-socket.send(JSON.stringify({ id: msg.id, result: {} }));
759-return;
760-}
761-if (msg.method === "Runtime.evaluate") {
762-socket.send(
763-JSON.stringify({
764-id: msg.id,
765-result: { result: { value: null } },
766-}),
767-);
768-}
769-});
770-wss = server.wss;
771-const snap = await snapshotDom({ wsUrl: server.wsUrl });
772-expect(snap.nodes).toStrictEqual([]);
773-});
774-775-it("returns an empty nodes array when nodes is not an array", async () => {
776-const server = await startMockWsServer((msg, socket) => {
777-if (msg.method === "Runtime.enable") {
778-socket.send(JSON.stringify({ id: msg.id, result: {} }));
779-return;
780-}
781-if (msg.method === "Runtime.evaluate") {
782-socket.send(
783-JSON.stringify({
784-id: msg.id,
785-result: { result: { value: { nodes: "not-an-array" } } },
786-}),
787-);
788-}
789-});
790-wss = server.wss;
791-const snap = await snapshotDom({ wsUrl: server.wsUrl });
792-expect(snap.nodes).toStrictEqual([]);
793-});
794-795-it("uses default DOM snapshot budgets for non-finite options", async () => {
796-const server = await startMockWsServer((msg, socket) => {
797-if (msg.method === "Runtime.enable") {
798-socket.send(JSON.stringify({ id: msg.id, result: {} }));
799-return;
800-}
801-if (msg.method === "Runtime.evaluate") {
802-const expression =
803-typeof msg.params?.expression === "string" ? msg.params.expression : "";
804-expect(expression).toContain("const maxNodes = 800;");
805-expect(expression).toContain("const maxText = 220;");
806-socket.send(JSON.stringify({ id: msg.id, result: { result: { value: { nodes: [] } } } }));
807-}
808-});
809-wss = server.wss;
810-811-const snap = await snapshotDom({
812-wsUrl: server.wsUrl,
813-limit: Number.NaN,
814-maxTextChars: Number.NaN,
815-});
816-817-expect(snap.nodes).toStrictEqual([]);
818-});
819-});
820-821-describe("getDomText", () => {
822-it("returns the evaluated string for text format", async () => {
823-const server = await startMockWsServer((msg, socket) => {
824-if (msg.method === "Runtime.enable") {
825-socket.send(JSON.stringify({ id: msg.id, result: {} }));
826-return;
827-}
828-if (msg.method === "Runtime.evaluate") {
829-socket.send(
830-JSON.stringify({
831-id: msg.id,
832-result: { result: { value: "plain body text" } },
833-}),
834-);
835-}
836-});
837-wss = server.wss;
838-const res = await getDomText({ wsUrl: server.wsUrl, format: "text", maxChars: 100 });
839-expect(res.text).toBe("plain body text");
840-});
841-842-it("returns the html outerHTML for html format with a selector", async () => {
843-const server = await startMockWsServer((msg, socket) => {
844-if (msg.method === "Runtime.enable") {
845-socket.send(JSON.stringify({ id: msg.id, result: {} }));
846-return;
847-}
848-if (msg.method === "Runtime.evaluate") {
849-socket.send(
850-JSON.stringify({
851-id: msg.id,
852-result: { result: { value: "<div>html</div>" } },
853-}),
854-);
855-}
856-});
857-wss = server.wss;
858-const res = await getDomText({
859-wsUrl: server.wsUrl,
860-format: "html",
861-selector: "#foo",
862-});
863-expect(res.text).toBe("<div>html</div>");
864-});
865-866-it("coerces numeric/boolean values to strings and falls back to empty for objects", async () => {
867-const responses: unknown[] = [42, true, { shape: "object" }];
868-let i = 0;
869-const server = await startMockWsServer((msg, socket) => {
870-if (msg.method === "Runtime.enable") {
871-socket.send(JSON.stringify({ id: msg.id, result: {} }));
872-return;
873-}
874-if (msg.method === "Runtime.evaluate") {
875-socket.send(
876-JSON.stringify({
877-id: msg.id,
878-result: { result: { value: responses[i++] } },
879-}),
880-);
881-}
882-});
883-wss = server.wss;
884-const num = await getDomText({ wsUrl: server.wsUrl, format: "text" });
885-expect(num.text).toBe("42");
886-const bool = await getDomText({ wsUrl: server.wsUrl, format: "text" });
887-expect(bool.text).toBe("true");
888-const obj = await getDomText({ wsUrl: server.wsUrl, format: "text" });
889-expect(obj.text).toBe("");
890-});
891-892-it("uses the default text budget for non-finite maxChars", async () => {
893-const server = await startMockWsServer((msg, socket) => {
894-if (msg.method === "Runtime.enable") {
895-socket.send(JSON.stringify({ id: msg.id, result: {} }));
896-return;
897-}
898-if (msg.method === "Runtime.evaluate") {
899-const expression =
900-typeof msg.params?.expression === "string" ? msg.params.expression : "";
901-expect(expression).toContain("const max = 200000;");
902-socket.send(JSON.stringify({ id: msg.id, result: { result: { value: "ok" } } }));
903-}
904-});
905-wss = server.wss;
906-907-const res = await getDomText({
908-wsUrl: server.wsUrl,
909-format: "text",
910-maxChars: Number.NaN,
911-});
912-913-expect(res.text).toBe("ok");
914-});
915-});
916-917-describe("querySelector", () => {
918-it("returns the matches array from the evaluated expression", async () => {
919-const server = await startMockWsServer((msg, socket) => {
920-if (msg.method === "Runtime.enable") {
921-socket.send(JSON.stringify({ id: msg.id, result: {} }));
922-return;
923-}
924-if (msg.method === "Runtime.evaluate") {
925-const matches: QueryMatch[] = [{ index: 1, tag: "button", text: "OK" }];
926-socket.send(JSON.stringify({ id: msg.id, result: { result: { value: matches } } }));
927-}
928-});
929-wss = server.wss;
930-const out = await querySelector({
931-wsUrl: server.wsUrl,
932-selector: "button",
933-limit: 5,
934-maxTextChars: 100,
935-maxHtmlChars: 500,
936-});
937-expect(out.matches[0]?.tag).toBe("button");
938-});
939-940-it("returns an empty array when the value is not an array", async () => {
941-const server = await startMockWsServer((msg, socket) => {
942-if (msg.method === "Runtime.enable") {
943-socket.send(JSON.stringify({ id: msg.id, result: {} }));
944-return;
945-}
946-if (msg.method === "Runtime.evaluate") {
947-socket.send(JSON.stringify({ id: msg.id, result: { result: { value: "not-array" } } }));
948-}
949-});
950-wss = server.wss;
951-const out = await querySelector({ wsUrl: server.wsUrl, selector: "button" });
952-expect(out.matches).toStrictEqual([]);
953-});
954-955-it("uses default query budgets for non-finite options", async () => {
956-const server = await startMockWsServer((msg, socket) => {
957-if (msg.method === "Runtime.enable") {
958-socket.send(JSON.stringify({ id: msg.id, result: {} }));
959-return;
960-}
961-if (msg.method === "Runtime.evaluate") {
962-const expression =
963-typeof msg.params?.expression === "string" ? msg.params.expression : "";
964-expect(expression).toContain("const lim = 20;");
965-expect(expression).toContain("const maxText = 500;");
966-expect(expression).toContain("const maxHtml = 1500;");
967-socket.send(JSON.stringify({ id: msg.id, result: { result: { value: [] } } }));
968-}
969-});
970-wss = server.wss;
971-972-const out = await querySelector({
973-wsUrl: server.wsUrl,
974-selector: "button",
975-limit: Number.NaN,
976-maxTextChars: Number.NaN,
977-maxHtmlChars: Number.NaN,
978-});
979-980-expect(out.matches).toStrictEqual([]);
981-});
982-});
983-984706describe("normalizeCdpWsUrl fill-in", () => {
985707it("respects an already-non-loopback ws hostname (no-rewrite branch)", () => {
986708// Covers the else side of the loopback/wildcard-guard in normalizeCdpWsUrl.
@@ -1287,21 +1009,4 @@ describe("cdp internal", () => {
12871009});
12881010});
128910111290-describe("getDomText branch coverage", () => {
1291-it("coerces a missing evaluated value to an empty string", async () => {
1292-// Covers the right-hand side of `evaluated.result?.value ?? ""`.
1293-const server = await startMockWsServer((msg, socket) => {
1294-if (msg.method === "Runtime.enable") {
1295-socket.send(JSON.stringify({ id: msg.id, result: {} }));
1296-return;
1297-}
1298-if (msg.method === "Runtime.evaluate") {
1299-socket.send(JSON.stringify({ id: msg.id, result: { result: {} } }));
1300-}
1301-});
1302-wss = server.wss;
1303-const res = await getDomText({ wsUrl: server.wsUrl, format: "text" });
1304-expect(res.text).toBe("");
1305-});
1306-});
13071012});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。