Friday, April 11, 2025

Playwright Hard Assertions vs Soft Assertions

 You might come across a number of scenarios where a decision needs to be taken regarding what happens if the test fails. If the error (or issue) being encountered is a minor one, the test execution should continue.

In case of major errors, it is better to abort the execution of the test case (or test suite). This can be achieved using assertion concept in playwright.

Playwright Hard Assertions vs Soft Assertions


Here are a few benefits of assertion in playwright.

  1. Check Expected Behavior: Assertions match actual vs. expected output to verify that the software works as it should.
  2. Support Regression Testing: They quickly show if changes affect other parts, giving fast feedback.
  3. Find Bugs: Failed assertions reveal mismatches, helping to spot bugs.
  4. Act as Documentation: Assertions document functionality, helping to clarify feature behavior.


Two type of Assertion we can using playwright framework.

1. Hard Assertions
A Hard Assertion is a type of assertion that throws an exception immediately when an assert statement fails. Test steps after hard assertion will not be executed and the next test in the test suite will start.

2. Soft Assertions
Soft Assertions are the type of assertions that do not throw an exception immediately when an assertion fails. Therefore, all steps and soft assertions in the automated test will execute before failing the test.

Let's deep dive into playwright assertion with example.

Hard Assertions in Playwright

A hard assertion immediately stops the test execution if the assertion fails.


Syntax :

await expect(page.getByTestId('status')).toHaveText('Submitted');

OR
 
expect(success).toBeTruthy();


In this example we are asserting following wrong values while running script, once assert fail it will stop test execution.

Playwright Hard Assertions vs Soft Assertions


Example for Hard Assertions :

// @ts-check
import { test, expect } from '@playwright/test';
import { chromium } from 'playwright';

test('Hard Assert Example - Search skptricks in google search engine', async () => {

  const browser = await chromium.launch({ headless: false,  args: ['--disable-blink-features=AutomationControlled'],})  
  const page = await browser.newPage();

  await page.goto('https://www.google.com/');

  // Check page URL.
  const pageURL = await page.url();
  console.log("Page URL is : "+ pageURL)
  await expect(page).toHaveURL('https://www.google.com/');

  await page.locator("[name='q']").fill("skptricks");
  await page.keyboard.press('Enter');
  await expect(await page.locator("[name='q']")).toHaveValue('skptricks Example');
  
  // Check Page Title.
  const pageTitle = await page.title();
  console.log("Page Title is : "+ pageTitle)
  await expect(page).toHaveTitle('skptricks - Google Search Example');

  // Check page partial URL check.
  const pagePartialURLCheck = await page.url();
  console.log("Page URL is : "+ pagePartialURLCheck)
  await expect(pagePartialURLCheck).toContain('https://www.google.com/search?q=skptricks&');

  await browser.close();
    
});

Output :

Playwright Hard Assertions vs Soft Assertions

Soft Assertions in Playwright

A soft assertion lets the test continue even if the assertion fails. It collects all failures and reports them after the test ends.

Syntax :

// Make a few checks that will not stop the test when failed...
await expect.soft(page.getByTestId('status')).toHaveText('Success');
await expect.soft(page.getByTestId('eta')).toHaveText('1 day');

In this example we are asserting following wrong values while running script, once assert fail it will continue test execution.


Playwright Hard Assertions vs Soft Assertions

Example for Soft Assertions :

// @ts-check
import { test, expect } from '@playwright/test';
import { chromium } from 'playwright';

test('Soft Assert Example - Search skptricks in google search engine', async () => {

  const browser = await chromium.launch({ headless: false,  args: ['--disable-blink-features=AutomationControlled'],})  
  const page = await browser.newPage();

  await page.goto('https://www.google.com/');

  // Check page URL.
  const pageURL = await page.url();
  console.log("Page URL is : "+ pageURL)
  await expect(page).toHaveURL('https://www.google.com/');

  await page.locator("[name='q']").fill("skptricks");
  await page.keyboard.press('Enter');
  await expect.soft(await page.locator("[name='q']")).toHaveValue('skptricks Example');
  
  // Check Page Title.
  const pageTitle = await page.title();
  console.log("Page Title is : "+ pageTitle)
  await expect.soft(page).toHaveTitle('skptricks - Google Search Example');

  // Check page partial URL check.
  const pagePartialURLCheck = await page.url();
  console.log("Page URL is : "+ pagePartialURLCheck)
  await expect(pagePartialURLCheck).toContain('https://www.google.com/search?q=skptricks&');

  await browser.close();
    
});

Output :

Playwright Hard Assertions vs Soft Assertions

Playwright Hard Assertions vs Soft Assertions

This is all about assertions in playwright.

When to Use

  • Hard asserts: Use when it's critical and you don’t want to proceed if the check fails (e.g., page load, navigation).
  • Soft asserts: Use when you want to gather multiple issues in one run, e.g., for UI validations or form field checks.



No comments:

Post a Comment