Skip to content

Commit 259f5f5

Browse files
committed
Fix flaky E2E test cases - hardened 404 capture logic in BasePage.ts: the listener now installs only once, records status/method/url for each 4xx response, and exposes reset404Errors() so each navigation starts from a clean slate, reworked the @ready main pages have no 404 errors coverage in critical-paths.spec.ts to enable the listener once, reset errors before every goto, and then assert. The loop no longer keeps stale entries from prior navigations or spins up redundant listeners, so the smoke check now exercises exactly one listener per Playwright page.
1 parent a2e910a commit 259f5f5

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

test/e2e/helpers/pageObjectModels/BasePage.ts

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export class BasePage {
2323
public errors404: string[] = []
2424
public navigationItems = navigationItems
2525
private lastAstroPageLoadCount = 0
26+
private has404Listener = false
2627

2728
protected constructor(protected readonly _page: Page) {
2829
this.page = _page
@@ -862,12 +863,32 @@ export class BasePage {
862863
/**
863864
* Verify 404 page is displayed for non-existent pages
864865
*/
866+
/**
867+
* Capture network responses that return 4xx status codes.
868+
* Listener is attached once per Playwright page instance.
869+
*/
865870
async enable404Listener(): Promise<void> {
866-
this._page.on('response', async response => {
867-
if (response.status() >= 400 && response.status() < 500) {
868-
this.errors404.push(this._page.url())
871+
if (this.has404Listener) {
872+
return
873+
}
874+
875+
this._page.on('response', response => {
876+
const status = response.status()
877+
if (status >= 400 && status < 500) {
878+
const method = response.request().method()
879+
const responseUrl = response.url()
880+
this.errors404.push(`${status} ${method} ${responseUrl}`)
869881
}
870882
})
883+
884+
this.has404Listener = true
885+
}
886+
887+
/**
888+
* Reset tracked 4xx network errors between navigations.
889+
*/
890+
reset404Errors(): void {
891+
this.errors404 = []
871892
}
872893

873894
/**

test/e2e/specs/01-smoke/critical-paths.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,9 @@ test.describe('Critical Paths @smoke', () => {
112112

113113
test('@ready main pages have no 404 errors', async ({ page: playwrightPage}) => {
114114
const page = await BasePage.init(playwrightPage)
115+
await page.enable404Listener()
115116
for (const { url: path } of page.navigationItems) {
116-
page.enable404Listener()
117+
page.reset404Errors()
117118
await page.goto(path)
118119
expect(page.errors404, `Received 404 errors for:\n${page.errors404.join("\n")}`).toHaveLength(0)
119120
}

0 commit comments

Comments
 (0)