Skip to content

MyUstaadG

Learn to Serve

  • Computer Science
    • Software
    • Computer Tips
    • Coding
      • Conditional Structures Exercises
      • Loops Exercises
      • Arrays Exercises
      • Python Exercises
    • Microsoft Office
      • MS Excel
      • MS Word
  • Videos
    • Funny Videos
    • Health and Fitness Videos
    • PUBG Videos
    • Technology Videos
  • News
  • Funny
  • Memes
  • Playwright Tutorials
  • Tutorials
  • Toggle search form
playwright Tutorial No. 09

Playwright Tutorial for Beginners 9 – Assertions

Posted on February 20, 2022February 20, 2022 By myustaadg.com No Comments on Playwright Tutorial for Beginners 9 – Assertions
Spread the love

Playwright Assertions

Playwright make use of expect library (provided by Jest) for assertions. The library provides different types of matchers like toEqual, toContain, toMatch, toMatchSnapshots and many more.

To make use of the library, import it into your test script.

const { expect } = require('@playwright/test')

You can extend with async matchers that will wait until the specified assertion condition is met.

expect(value).toEqual(anotherValue);

await expect(value).toBeTruthy();

Common patterns

// assert text content
expect(await page.locator('.welcomeMsg').textContent()).toEqual('Welcome pal');

// assert inner text
expect(await page.locator('.goodbyeMsg').innerText()).toBe('Goodbye pal');

// assert inner html
expect(await page.locator('.button-container').innerText()).toEqual(
    '<button>Submit</button>'
  );

// assert attribute
expect(await page.locator('css=#name-input')).toHaveAttribute('type', 'text');

// assert url
expect(await page.url()).toMatch(/\/users\/test/);

// and many more!

Custom assertions

To have custom assertions, you can extend the expect provided by Jest. For instance, we will create a custom matcher called toBeBetween10And100:

const { expect, default: test } = require('@playwright/test');

expect.extend({
  toBeBetween10And100(num) {
    const pass = num > 10 && num < 100;

    if (pass) {
      return {
        message: () => `expected ${num} to be not within 10 and 100`,
        pass: true
      };
    }
    return {
      message: () => `expected ${num} to be within 10 and 100`,
      pass: false
    };
  }
});

test('simple test', async () => {
    // pass
  expect(25).toBeBetween10And100();
});

There are a bunch of matchers you can use with Playwright depending on what your test suites are.

Hope You will learn Something. Happy Coding😍

Visit my YouTube Channel as Well https://www.youtube.com/watch?v=JuIHQ9cLSEw&list=PLbIhkHxfUIItTdcyCb34uRIrPJbXBndIl&index=4
Playwright Tutorials Tags:Everything about the Playwright framework, Getting started | Playwright, Getting started with Playwright, How do I run a playwright test in applitools?, How do I use playwright with Node JS?, how to install playwright, playwright automation tool, playwright documentation, playwright example, playwright python tutorial, playwright test, playwright test framework, playwright tutorial, Playwright tutorial example

Post navigation

Previous Post: Playwright Tutorial for Beginners 8 – Screenshots
Next Post: Simple Data Type Exercises in Python | Python Programming

More Related Articles

playwright Tutorial No. 03 Playwright Tutorial for Beginners 3 – Trace Viewer Playwright Tutorials
playwright Tutorial No. 02 Playwright Tutorial for Beginners 2 – Code Generator Playwright Tutorials
playwright Tutorial No. 05 Playwright Tutorial for Beginners 5 – Performing Actions Playwright Tutorials
playwright Tutorial No. 04 Playwright Tutorial for Beginners 4 – Selectors and Locators Playwright Tutorials
playwright Tutorial No. 06 Playwright Tutorial for Beginners 6 – Demo 1 Playwright Tutorials
playwright Tutorial No. 01 Playwright Tutorial for Beginners 1 – Getting Started Computer Tips

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2022 MyUstaadG.

Powered by PressBook Blog WordPress theme