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.
Here are a few benefits of assertion in playwright.
- Check Expected Behavior: Assertions match actual vs. expected output to verify that the software works as it should.
- Support Regression Testing: They quickly show if changes affect other parts, giving fast feedback.
- Find Bugs: Failed assertions reveal mismatches, helping to spot bugs.
- Act as Documentation: Assertions document functionality, helping to clarify feature behavior.
Two type of Assertion we can using playwright framework.
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.
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 :
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.
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 :
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