Skip to content

[Question] Wait for XHR triggered by action #3976

@sedenardi

Description

@sedenardi

I'm attempting to use playwright for e2e testing of our web application. One of the pages is a form input that automatically saves user input, using the onBlur event on the form <input>s.

The workflow we're testing is:

  • Navigate to the page
  • Grab the value of a form input
  • Append a string to the value
  • Click away from the input to trigger the XHR request to the server to save the input
  • Refresh page
  • Verify that the form input is the same as what was previously inputted

Playwright code:

await page.goto(URL);
const selector = '[data-testid="name"]';
let element = await page.$(selector);
const originalName = await element.getAttribute('value');
const testTag = ' TestingTag';
await page.fill(selector, originalName + testTag);
await page.click('h1'); // <-- triggers onBlur, initiating XHR request
await page.reload();
element = await page.$(selector);
const alteredName = await element.getAttribute('value');
expect(alteredName === originalName + testTag);

The issue we're seeing is that after the page is reloaded with the original value. This is because page.reload() happens immediately, not waiting for the XHR request to initiate or resolve.

We've tried to use page.waitForLoadState to detect and wait for network connections, but that hasn't worked.

await page.fill(selector, originalName + testTag);
await page.click('h1');
await page.waitForLoadState('networkidle');
await page.reload();
/** also doesn't work **/
await page.fill(selector, originalName + testTag);
await Promise.all([
  page.waitForLoadState('networkidle'),
  page.click('h1')
]);
await page.reload();

We realize that it may be because the XHR request isn't initiated by the time page.waitForLoadState is executed, so we're wondering if we're either not using page.waitForLoadState properly, not using the appropriate API to wait for XHR requests, or what we're trying to do isn't possible (at least not out of the box) with playwright.

Thanks for your help in advance.

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