Skip to content

[BUG] Infinite waiting for resolving waitForLoadState('networkidle') #19835

Closed

Description

@gamesterr

Context:

  • Playwright Version: 1.29.1
  • Operating System: Mac
  • Node.js version: 16.13.0
  • Browser: Chromium

With waitForLoadState('networkidle') inside the Promise.all, playwright is waiting until timeout even though no request is sent. Noticed it in my app, but I was able to reproduce it with the example page I passed in the code.

Code Snippet

test('test', async ({ page }) => {
  await page.goto('https://practice.automationtesting.in/');
  await Promise.all([page.waitForLoadState('networkidle')]);
  await page.waitForTimeout(5000);
});

Activity

  1. lyatziv commented on Jan 3, 2023

    @lyatziv

    This may just be an implementation issue, consider the following change to the code.

    test('test', async ({ page }) => {
      await Promise.all([
        page.waitForLoadState('networkidle'),
        page.goto('https://practice.automationtesting.in/')
      ]);
      await page.waitForTimeout(5000);
    });
    
  2. mxschmitt commented on Jan 3, 2023

    @mxschmitt
    Contributor

    I tried to run your code and for me it works as expected, it executes successfully.

  3. mxschmitt commented on Jan 9, 2023

    @mxschmitt
    Contributor

    Closing since it seems stale and we were not able to reproduce. Please re-file with more information aka. logs which you can set when setting the DEBUG=pw:api,pw:protocol env var.

  4. peterholcomb commented on Jan 31, 2023

    @peterholcomb

    FWIW, we are also seeing this in 1.29.2 but were not seeing it in 1.27.1. In some tests, the waitLoadState('networkidle') seems to never return

  5. tonai commented on Feb 10, 2023

    @tonai

    Also ran into issues with waitLoadState('networkidle') not being fired (using 1.30.0).
    But When I run chromium not in headless mode, open the devtools, check the network, then in that case waitLoadState('networkidle') is fired as expected.

  6. mxschmitt commented on Feb 10, 2023

    @mxschmitt
    Contributor

    Feel free to file a bug report with a repro, happy to take a look then.

  7. rihaskh commented on Mar 15, 2023

    @rihaskh

    Still the issue with page.waitForLoadState('networkidle')

  8. rihaskh commented on Mar 15, 2023

    @rihaskh

    and related this my code ,

    await Promise.all([
    this.page.waitForLoadState('domcontentloaded'),
    this.page.waitForLoadState(),
    this.page.waitForResponse(response => response.url().includes('/currentstatuses'), { timeout: 95000 }),
    this.page.locator("//button[.='Update']").click(),

    ]); works well in debugging but when i ran whole script it throws error and close the browser

  9. kwiniarski97 commented on Mar 24, 2023

    @kwiniarski97

    Is there any solution for it?
    I tried to migrate page.waitForLoadState('networkidle') with other ways to tell if a given page, or action has ended, but there are simply some things that must use it.

  10. creichlin commented on Sep 11, 2023

    @creichlin

    in my case
    page.waitForLoadState('networkidle')
    hangs forever, but only if my api endpoints set the header
    Cache-control=no-store
    I have not found out yet why this happens, might be custom to my setup.

  11. taschik commented on Oct 10, 2023

    @taschik

    Chris (@creichlin) have you ever found out the reason and a potential fix for your problem?

  12. brutaldev commented on Jan 13, 2024

    @brutaldev

    A scenario that led me here was using the SlowMo launch option which makes it hang on waiting for network idle state 100% of the time.

  13. daniel442li commented on Mar 28, 2024

    @daniel442li

    getting this error too

  14. Radivarig commented on Mar 29, 2025

    @Radivarig

    It either works or more often doesn't, even though all network requests are done, tested from code and from UI network tab.
    Removing no-store header did nothing, maybe there's another header blocking but I need this to not depend on headers.

    Using this as a replacement:

    import {waitForNetworkIdle} from './waitForNetworkIdle'
    
    test('page loads with fetched title', async ({page}) => {
      await page.goto('...')
      await waitForNetworkIdle(page) // <-
      const title = await page.title()
      expect(title).toBe('some title')
    })
    import {Page, Request} from '@playwright/test'
    
    export const waitForNetworkIdle = async (
      page: Page,
      idleTime = 500,
      timeout = 10000,
    ): Promise<void> => {
      const pending = new Set<Request>()
      let lastActivity = Date.now()
    
      const onRequest = (req: any) => {
        pending.add(req)
        lastActivity = Date.now()
      }
    
      const onDone = (req: any) => {
        pending.delete(req)
        lastActivity = Date.now()
      }
    
      page.on('request', onRequest)
      page.on('requestfinished', onDone)
      page.on('requestfailed', onDone)
    
      return new Promise((resolve, reject) => {
        const start = Date.now()
    
        const check = () => {
          const now = Date.now()
          const timeSinceLastActivity = now - lastActivity
    
          if (pending.size === 0 && timeSinceLastActivity >= idleTime) {
            cleanup()
            resolve()
          }
          else if (now - start > timeout) {
            cleanup()
            reject(new Error(`waitForNetworkIdle timed out after ${timeout}ms`))
          }
        }
    
        const interval = setInterval(check, 100)
    
        const cleanup = () => {
          clearInterval(interval)
          page.off('request', onRequest)
          page.off('requestfinished', onDone)
          page.off('requestfailed', onDone)
        }
      })
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions