An Interest In:
Web News this Week
- April 28, 2024
- April 27, 2024
- April 26, 2024
- April 25, 2024
- April 24, 2024
- April 23, 2024
- April 22, 2024
April 16, 2023 01:09 pm GMT
Original Link: https://dev.to/zt4ff_1/playwright-tutorial-for-beginners-9-assertions-2g5c
Playwright Tutorial for Beginners 9 - Assertions
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 contentexpect(await page.locator('.welcomeMsg').textContent()).toEqual('Welcome pal');// assert inner textexpect(await page.locator('.goodbyeMsg').innerText()).toBe('Goodbye pal');// assert inner htmlexpect(await page.locator('.button-container').innerText()).toEqual( '<button>Submit</button>' );// assert attributeexpect(await page.locator('css=#name-input')).toHaveAttribute('type', 'text');// assert urlexpect(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.
Original Link: https://dev.to/zt4ff_1/playwright-tutorial-for-beginners-9-assertions-2g5c
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To