Search code examples
typescriptplaywrightplaywright-typescript

Playwright : How to continue test execution after assertion failed?


I need to run multiple links on a single test. The test scenarios are almost the same for the 4 links, but will have different color or data.

I have tried this, but once link A has failed, it will fail link B,C and D (It will fail the whole test). Is there a way to run those 4 links independently from each other?

test("Validate pages", async () => {
    const browser = await chromium.launch({headless: false});
    const context = await browser.newContext();
    const page = await context.newPage()

const urls = ["linkA", "linkB", "linkC", "linkD"]
  for (let i = 0; i < urls.length; i++) {
     const url = urls[i];
     await page.goto(`${url}`); 

// insert test code here that needs to run on the 4 links.
}
}

Solution

  • Use soft Assertions. It will continue test even after one assertion failed.

    soft assertions do not terminate test execution immediately but mark the test as failed to report in results later along with all test steps results.

    You can do that in single test.Inside loop just verify the expected URL:

    for (const url of urls) {
      await page.goto(`${url}`)
      await expect.soft(page).toHaveURL(/.*PageA/)
    }
    

    Further Reading:

    https://timdeschryver.dev/blog/improve-your-test-experience-with-playwright-soft-assertions

    https://www.linkedin.com/pulse/soft-asserts-better-way-test-your-code-serhat-u%C3%A7ar