Search code examples
testingplaywrightplaywright-testautomation-testing

Check playwright doesnt redirect to another page when a redirect button is clicked


I got a form which takes user data. I am building a simple test to check that the continue button will not redirect to another page if the data inputs are invalid or empty.

any ideas?

Many thanks,

:-)

I have tried using waitForNavigatiopn(), but it is now deprecated.


Solution

  • You'd better check for error messages on UI, and API calls if you need to check form data.

    // Error on UI
    await expect(page.locator("error")).toBeVisible();
    
    // Form still opened - page not redirected
    await expect(page.locator("#username")).toBeVisible();
    
    // Url not changed
    const url = page.url();
    await page.click("text=Continue");
    await expect(page).toHaveURL(url)
    
    // Wait for the error on redirect
    expect(await page.waitForURL(/success/)).toThrowError();
    
    // Wait for responses
    await page.waitForResponse(/error/);
    await page.waitForResponse(r => r.body.toString().includes("error"));
    ...