Skip to content

Commit 721600e

Browse files
author
Dusty Greif
committed
Stabilize browser tests around navigation
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: d6d3b35e-d82a-424a-b0e1-ab8b46814964
1 parent 2716818 commit 721600e

4 files changed

Lines changed: 24 additions & 17 deletions

File tree

src/tests/functional/rendering_tests.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ test("test reloads when tracked elements change", async ({ page }) => {
6767
})
6868

6969
test("test reloads when tracked elements change due to failed form submission", async ({ page }) => {
70-
await page.click("#tracked-asset-change-form button")
70+
await Promise.all([page.waitForNavigation(), page.click("#tracked-asset-change-form button")])
7171
await page.evaluate(() => {
7272
window.addEventListener(
7373
"turbo:reload",
@@ -86,7 +86,7 @@ test("test reloads when tracked elements change due to failed form submission",
8686
)
8787
})
8888

89-
await page.click("#tracked-asset-change-form button")
89+
await Promise.all([page.waitForNavigation(), page.click("#tracked-asset-change-form button")])
9090

9191
const reason = await page.evaluate(() => localStorage.getItem("reason"))
9292
const unloaded = await page.evaluate(() => localStorage.getItem("unloaded"))

src/tests/functional/visit_tests.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ test("test programmatically visiting a same-origin location", async ({ page }) =
4444

4545
test("skip programmatically visiting a cross-origin location falls back to window.location", async ({ page }) => {
4646
const urlBeforeVisit = page.url()
47-
await visitLocation(page, "about:blank")
47+
await Promise.all([page.waitForNavigation(), visitLocation(page, "about:blank")])
4848

4949
const urlAfterVisit = page.url()
5050
assert.notEqual(urlBeforeVisit, urlAfterVisit)
@@ -53,8 +53,7 @@ test("skip programmatically visiting a cross-origin location falls back to windo
5353

5454
test("test visiting a location served with a non-HTML content type", async ({ page }) => {
5555
const urlBeforeVisit = page.url()
56-
await visitLocation(page, "/src/tests/fixtures/svg.svg")
57-
await nextBeat()
56+
await Promise.all([page.waitForNavigation(), visitLocation(page, "/src/tests/fixtures/svg.svg")])
5857

5958
const url = page.url()
6059
const contentType = await contentTypeOfURL(url)

src/tests/helpers/page.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -161,17 +161,25 @@ export function propertyForSelector(page: Page, selector: string, propertyName:
161161
}
162162

163163
async function readArray<T>(page: Page, identifier: string, length?: number): Promise<T[]> {
164-
return page.evaluate(
165-
({ identifier, length }) => {
166-
const records = (window as any)[identifier]
167-
if (records != null && typeof records.splice == "function") {
168-
return records.splice(0, typeof length === "undefined" ? records.length : length)
169-
} else {
170-
return []
171-
}
172-
},
173-
{ identifier, length }
174-
)
164+
try {
165+
return await page.evaluate(
166+
({ identifier, length }) => {
167+
const records = (window as any)[identifier]
168+
if (records != null && typeof records.splice == "function") {
169+
return records.splice(0, typeof length === "undefined" ? records.length : length)
170+
} else {
171+
return []
172+
}
173+
},
174+
{ identifier, length }
175+
)
176+
} catch (error) {
177+
if (!(error instanceof Error) || !error.message.includes("Execution context was destroyed")) {
178+
throw error
179+
}
180+
await nextBeat()
181+
return readArray(page, identifier, length)
182+
}
175183
}
176184

177185
export function readEventLogs(page: Page, length?: number): Promise<EventLog[]> {

src/tests/helpers/trusted_type_setup.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ test.beforeEach(async ({ page }, workerInfo) => {
1515
})
1616

1717
test.afterEach(async ({ page }) => {
18-
await page.evaluate(() => window.Turbo.setCSPTrustedTypesPolicy(null))
18+
await page.evaluate(() => window.Turbo?.setCSPTrustedTypesPolicy(null))
1919
})

0 commit comments

Comments
 (0)