Skip to content

[Feature]: allow navigation to be intercepted BEFORE destroying the DOM #31596

Open

Description

🚀 Feature Request

per https://playwright.dev/docs/navigations#navigation-events

Navigation starts by changing the page URL or by interacting with the page (e.g., clicking a link). The navigation intent may be canceled, for example, on hitting an unresolved DNS address or transformed into a file download.

Ideally it would be useful for me to have the capability of

  • prevent commit of the navigation automatically
  • override the request navigation goes to
  • simulate an error in navigation

Example

test("disable form", async ({ page }) => {
  await page.goto("http://localhost:8080/");
  const submitButton = page.locator("input[type='submit']");
  await expect(submitButton).toBeEnabled();

  page.setNavigationMode(NavigationMode.PAUSE_FIRST);
  const navigationController = page.navigationController();
  await submitButton.click({ 
    noWaitAfter: true, // required, if not present `click` should throw an exception
  });

  // the following should work because the navigation is paused and the DOM context hasn't been removed
  await expect(submitButton).toBeDisabled();

  // release the navigation
  navigationController.continue();
  await expect(page)
    .toHaveURL("https://httpbun.com/mix/s=200/d=3/b64=dGVzdA==");
});

Motivation

https://stackoverflow.com/questions/78715069/how-do-i-verify-if-form-buttons-are-disabled-when-pressing-the-submit-button-in/78715251?noredirect=1#comment138794818_78715251

Notes

  • page.setNavigationMode should only apply to the current test context, so it shouldn't leak to other tests
  • NavigationMode.PAUSE_FIRST pauses the navigation on click or any other events that would perform navigation
  • Specifically on click , noWaitAfter: true must be set
  • PlaywrightNavigator.continue() should do the default (should not be a promise)
  • PlaywrightNavigator.nextRequest property get returns the NextRequestData being navigated to (should not be a promise)
  • PlaywrightNavigator.NextRequestDataStandard contains the following data (non-promise)
    • method : "GET" | "POST" | etc.
    • url
    • headers
    • requestBody
    • followRedirects: boolean = default to true
  • PlaywrightNavigator.NextRequestDataOverrideResponse contains the following data (non-promise)
    • statusCode: int
    • url: faked destination
    • headers: faked response headers
    • content: content
  • PlaywrightNavigator.NextRequestData = PlaywrightNavigator.NextRequestDataOverrideResponse | PlaywrightNavigator.NextRequestDataStandard
  • PlaywrightNavigator.nextRequest property setter can override the request or return a simulated response
  • NavigationMode.NORMAL would be the normal behaviour.
  • if followRedirects == false the test should fail when navigation goes to a 3xx series code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions