Repository: vercel/next.js
Pull request: Stabilize back-forward-cache test cases

This attempts to stabilize the back-forward-cache test assertions by adding retry to the initial button incrementing as I have a theory this was attempting to click before the event listeners were attached causing the clicks to be missed which makes the successive test cases fail. 

x-ref: https://github.com/vercel/next.js/actions/runs/14766354734/job/41458564789#step:33:481
x-ref: https://github.com/vercel/next.js/actions/runs/14766354734/job/41459734942#step:33:191

Changed files:
filename: test/e2e/app-dir/back-forward-cache/back-forward-cache.test.ts
patch: @@ -1,4 +1,5 @@
 import { nextTestSetup } from 'e2e-utils'
+import { retry } from 'next-test-utils'
 
 describe('back/forward cache', () => {
   const { next } = nextTestSetup({
@@ -9,10 +10,17 @@ describe('back/forward cache', () => {
     const browser = await next.browser('/page/1')
 
     // Accumulate some state on page 1.
-    const incrementButton1 = await browser.elementById('increment-button-1')
+    await retry(async () => {
+      // we do this inside of retry in case button event handler
+      // isn't ready yet
+      await browser.elementById('increment-button-1').click()
+
+      const counterDisplay1 = await browser.elementById('counter-display-1')
+      expect(await counterDisplay1.text()).toBe('Count: 1')
+    })
+    await browser.elementById('increment-button-1').click()
+
     const counterDisplay1 = await browser.elementById('counter-display-1')
-    await incrementButton1.click()
-    await incrementButton1.click()
     expect(await counterDisplay1.text()).toBe('Count: 2')
 
     // Navigate to page 2. Accumulate some state here, too.
@@ -48,10 +56,18 @@ describe('back/forward cache', () => {
     const browser = await next.browser('/page/1')
 
     // Accumulate some state on page 1.
-    const incrementButton1 = await browser.elementById('increment-button-1')
+    // Accumulate some state on page 1.
+    await retry(async () => {
+      // we do this inside of retry in case button event handler
+      // isn't ready yet
+      await browser.elementById('increment-button-1').click()
+
+      const counterDisplay1 = await browser.elementById('counter-display-1')
+      expect(await counterDisplay1.text()).toBe('Count: 1')
+    })
+    await browser.elementById('increment-button-1').click()
+
     const counterDisplay1 = await browser.elementById('counter-display-1')
-    await incrementButton1.click()
-    await incrementButton1.click()
     expect(await counterDisplay1.text()).toBe('Count: 2')
 
     // Navigate to page 2. Accumulate some state here, too.
@@ -88,10 +104,17 @@ describe('back/forward cache', () => {
     const browser = await next.browser('/page/1')
 
     // Accumulate some state on page 1.
-    const incrementButton1 = await browser.elementById('increment-button-1')
+    await retry(async () => {
+      // we do this inside of retry in case button event handler
+      // isn't ready yet
+      await browser.elementById('increment-button-1').click()
+
+      const counterDisplay1 = await browser.elementById('counter-display-1')
+      expect(await counterDisplay1.text()).toBe('Count: 1')
+    })
+    await browser.elementById('increment-button-1').click()
+
     const counterDisplay1 = await browser.elementById('counter-display-1')
-    await incrementButton1.click()
-    await incrementButton1.click()
     expect(await counterDisplay1.text()).toBe('Count: 2')
 
     // Navigate to page 2.
@@ -103,27 +126,38 @@ describe('back/forward cache', () => {
       'a[href="/page/1?param=true"]'
     )
     await linkToPage1WithSearchParam.click()
-    const counterDisplay1AfterNav =
-      await browser.elementById('counter-display-1')
-    const hasSearchParam = await browser.elementById('has-search-param-1')
-    expect(await counterDisplay1AfterNav.text()).toBe('Count: 2')
-    expect(await hasSearchParam.text()).toBe('Has search param: yes')
+
+    await retry(async () => {
+      const counterDisplay1AfterNav =
+        await browser.elementById('counter-display-1')
+      const hasSearchParam = await browser.elementById('has-search-param-1')
+      expect(await counterDisplay1AfterNav.text()).toBe('Count: 2')
+      expect(await hasSearchParam.text()).toBe('Has search param: yes')
+    })
   })
 
   it('bfcache only preserves up to N entries', async () => {
     // The current limit is hardcoded to 3.
     const browser = await next.browser('/page/1')
 
     // Accumulate some state on page 1.
-    const incrementButton1 = await browser.elementById('increment-button-1')
+    await retry(async () => {
+      // we do this inside of retry in case button event handler
+      // isn't ready yet
+      await browser.elementById('increment-button-1').click()
+
+      const counterDisplay1 = await browser.elementById('counter-display-1')
+      expect(await counterDisplay1.text()).toBe('Count: 1')
+    })
+    await browser.elementById('increment-button-1').click()
+
     const counterDisplay1 = await browser.elementById('counter-display-1')
-    await incrementButton1.click()
-    await incrementButton1.click()
     expect(await counterDisplay1.text()).toBe('Count: 2')
 
     // Navigate to page 2. Accumulate some state here, too.
     const linkToPage2 = await browser.elementByCss('a[href="/page/2"]')
     await linkToPage2.click()
+
     const incrementButton2 = await browser.elementById('increment-button-2')
     const counterDisplay2 = await browser.elementById('counter-display-2')
     await incrementButton2.click()
@@ -153,22 +187,32 @@ describe('back/forward cache', () => {
     expect(await counterDisplay2AfterNav.text()).toBe('Count: 9')
 
     // Navigate back to page 1 to confirm its state is not preserved.
-    const linkToPage1 = await browser.elementByCss('a[href="/page/1"]')
-    await linkToPage1.click()
-    const counterDisplay1AfterNav =
-      await browser.elementById('counter-display-1')
-    expect(await counterDisplay1AfterNav.text()).toBe('Count: 0')
+    await retry(async () => {
+      const linkToPage1 = await browser.elementByCss('a[href="/page/1"]')
+      await linkToPage1.click()
+
+      const counterDisplay1AfterNav =
+        await browser.elementById('counter-display-1')
+      expect(await counterDisplay1AfterNav.text()).toBe('Count: 0')
+    })
   })
 
   it('navigate back and forth repeatedly between the same pages without evicting', async () => {
     // The current limit is hardcoded to 3.
     const browser = await next.browser('/page/1')
 
     // Accumulate some state on page 1.
-    const incrementButton1 = await browser.elementById('increment-button-1')
+    await retry(async () => {
+      // we do this inside of retry in case button event handler
+      // isn't ready yet
+      await browser.elementById('increment-button-1').click()
+
+      const counterDisplay1 = await browser.elementById('counter-display-1')
+      expect(await counterDisplay1.text()).toBe('Count: 1')
+    })
+    await browser.elementById('increment-button-1').click()
+
     const counterDisplay1 = await browser.elementById('counter-display-1')
-    await incrementButton1.click()
-    await incrementButton1.click()
     expect(await counterDisplay1.text()).toBe('Count: 2')
 
     // Navigate to page 2. Accumulate some state here, too.