|
| 1 | +import { test, expect } from "@playwright/test"; |
| 2 | +import { V3 } from "../v3"; |
| 3 | +import { v3TestConfig } from "./v3.config"; |
| 4 | +import { V3Context } from "../understudy/context"; |
| 5 | + |
| 6 | +const EXAMPLE_URL = "https://example.com"; |
| 7 | + |
| 8 | +test.describe("page.addInitScript", () => { |
| 9 | + let v3: V3; |
| 10 | + let ctx: V3Context; |
| 11 | + |
| 12 | + test.beforeEach(async () => { |
| 13 | + v3 = new V3(v3TestConfig); |
| 14 | + await v3.init(); |
| 15 | + ctx = v3.context; |
| 16 | + }); |
| 17 | + |
| 18 | + test.afterEach(async () => { |
| 19 | + await v3?.close?.().catch(() => {}); |
| 20 | + }); |
| 21 | + |
| 22 | + test("runs scripts on real network navigations", async () => { |
| 23 | + const page = await ctx.awaitActivePage(); |
| 24 | + |
| 25 | + await page.addInitScript(() => { |
| 26 | + (window as unknown as { __fromPageInit?: string }).__fromPageInit = |
| 27 | + "page-level"; |
| 28 | + }); |
| 29 | + |
| 30 | + await page.goto(EXAMPLE_URL, { waitUntil: "domcontentloaded" }); |
| 31 | + |
| 32 | + const observed = await page.evaluate(() => { |
| 33 | + return (window as unknown as { __fromPageInit?: string }).__fromPageInit; |
| 34 | + }); |
| 35 | + |
| 36 | + expect(observed).toBe("page-level"); |
| 37 | + }); |
| 38 | + |
| 39 | + test("scopes scripts to the page only", async () => { |
| 40 | + const first = await ctx.awaitActivePage(); |
| 41 | + |
| 42 | + await first.addInitScript(() => { |
| 43 | + function markScope(): void { |
| 44 | + const root = document.documentElement; |
| 45 | + if (!root) return; |
| 46 | + root.dataset.scopeWitness = "page-one"; |
| 47 | + } |
| 48 | + if (document.readyState === "loading") { |
| 49 | + document.addEventListener("DOMContentLoaded", markScope, { |
| 50 | + once: true, |
| 51 | + }); |
| 52 | + } else { |
| 53 | + markScope(); |
| 54 | + } |
| 55 | + }); |
| 56 | + |
| 57 | + await first.goto(`${EXAMPLE_URL}/?page=one`, { |
| 58 | + waitUntil: "domcontentloaded", |
| 59 | + }); |
| 60 | + |
| 61 | + const second = await ctx.newPage(); |
| 62 | + await second.goto(`${EXAMPLE_URL}/?page=two`, { |
| 63 | + waitUntil: "domcontentloaded", |
| 64 | + }); |
| 65 | + |
| 66 | + const firstValue = await first.evaluate(() => { |
| 67 | + return document.documentElement.dataset.scopeWitness ?? "missing"; |
| 68 | + }); |
| 69 | + const secondValue = await second.evaluate(() => { |
| 70 | + return document.documentElement.dataset.scopeWitness ?? "missing"; |
| 71 | + }); |
| 72 | + |
| 73 | + expect(firstValue).toBe("page-one"); |
| 74 | + expect(secondValue).toBe("missing"); |
| 75 | + }); |
| 76 | + |
| 77 | + test("supports passing arguments to function sources", async () => { |
| 78 | + const page = await ctx.awaitActivePage(); |
| 79 | + const payload = { greeting: "hi", nested: { count: 1 } }; |
| 80 | + |
| 81 | + await page.addInitScript((arg) => { |
| 82 | + function setPayload(): void { |
| 83 | + const root = document.documentElement; |
| 84 | + if (!root) return; |
| 85 | + root.dataset.pageInitPayload = JSON.stringify(arg); |
| 86 | + } |
| 87 | + if (document.readyState === "loading") { |
| 88 | + document.addEventListener("DOMContentLoaded", setPayload, { |
| 89 | + once: true, |
| 90 | + }); |
| 91 | + } else { |
| 92 | + setPayload(); |
| 93 | + } |
| 94 | + }, payload); |
| 95 | + |
| 96 | + await page.goto(`${EXAMPLE_URL}/?page=payload`, { |
| 97 | + waitUntil: "domcontentloaded", |
| 98 | + }); |
| 99 | + |
| 100 | + const observed = await page.evaluate(() => { |
| 101 | + const raw = document.documentElement.dataset.pageInitPayload; |
| 102 | + return raw ? JSON.parse(raw) : undefined; |
| 103 | + }); |
| 104 | + |
| 105 | + expect(observed).toEqual(payload); |
| 106 | + }); |
| 107 | +}); |
0 commit comments