Playwright Soft Assertions

Profile picture for user devraj

By default, whenever our assert fails, test execution will terminate. Sometimes, you may want to continue execution even when one or two assert fail. In such a scenario, you can use Playwright Soft Assertion. Soft assertions on failure do not terminate test execution, and test execution will continue to the next step. However, the test will be marked as failed even when a single assertion fails.

Table of Content

  1. Assertions with no failure
  2. Assertions with failure
  3. Soft assertion with failure
  4. Stop Execution on Soft Assertion failure
  5. Video Tutorial

1. Assertions with no failure

test.only('soft assertions', async({page})=>{
    await page.goto('https://www.programsbuzz.com/')
    const locator = page.locator('ul.we-mega-menu-ul>li').nth(-1);
    
    await expect(page).toHaveTitle('Online Technical Courses');
    await expect(locator).toHaveAttribute('data-element-type','we-mega-menu-li');
    await expect(locator).toHaveClass('we-mega-menu-li')
})

In the above code, all assertion will pass, and you can see in the below output all three assertions are executed.

playwright soft assertion

2. Assertions with failure

Let's change the expected title value to Online Technical Courseso that first assertion fail.

test.only('soft assertions', async({page})=>{
    await page.goto('https://www.programsbuzz.com/')
    const locator = page.locator('ul.we-mega-menu-ul>li').nth(-1);
    
    await expect(page).toHaveTitle('Online Technical Courses1');
    await expect(locator).toHaveAttribute('data-element-type','we-mega-menu-li');
    await expect(locator).toHaveClass('we-mega-menu-li')
})

In the above code, the first assertion will fail. You can observe in the below output that test execution terminated after the first assertion failure, and the remaining two assertions are not executed.

soft assertion in playwright

3. Soft assertion with failure

To use soft assertion, add a dot(.) with the soft keyword just after the expect statement.

test.only('soft assertions', async({page})=>{
    await page.goto('https://www.programsbuzz.com/')
    const locator = page.locator('ul.we-mega-menu-ul>li').nth(-1);
    
    await expect.soft(page).toHaveTitle('Online Technical Courses1');
    await expect.soft(locator).toHaveAttribute('data-element-type','we-mega-menu-li');
    await expect.soft(locator).toHaveClass('we-mega-menu-li')
})

You can observe in the below output in the same scenario that even though the first assertion fails, the remaining two assertions are executed.

how to use soft assertion in playwright

4. Stop Execution on Soft Assertion failure

By adding the statement below, you could avoid running further if there are soft assertion failures.

expect(test.info().errors).toHaveLength(0);

Here using test.info(), we are getting information about the currently running test, and using errors, we are getting the count of the number of errors thrown during the execution. Using expect, we are verifying error length is 0, and if it is not 0, the program will terminate because assert fails.

test.only('soft assertions', async({page})=>{
    await page.goto('https://www.programsbuzz.com/')
    const locator = page.locator('ul.we-mega-menu-ul>li').nth(-1);
    
    await expect.soft(page).toHaveTitle('Online Technical Courses1');
    await expect.soft(locator).toHaveAttribute('data-element-type','we-mega-menu-li');
    expect(test.info().errors).toHaveLength(0);
    await expect.soft(locator).toHaveClass('we-mega-menu-li')
})

You can see in the below output that execution stops after two assertions, and the third expect is not executed after adding the error length statement.

use soft assertion in playwright

Video Tutorial: Playwright Soft Assert