Skip to main content

PlayWright Custom Expect Message

We can add custom messages within the expect declaring syntax in playwright as a second argument.

const {test, expect} = require('@playwright/test');
test('Custom Expect', async ({page})=> 
{


const number = 100;
await expect(number, "Number value is low!!").toBeGreaterThanOrEqual(100);

});
  • This gets passed.
  • Let us see what happens when we intentionally fail the test.
const {test, expect} = require('@playwright/test');
test('Custom Expect', async ({page})=> 
{


const number = 60;
await expect(number, "Number value is low!!").toBeGreaterThanOrEqual(90);

});

  • So the assertion failed with our custom message.
const {test, expect} = require('@playwright/test');
test('Custom Expect', async ({page})=> 
{

await page.goto("https://www.programsbuzz.com/user/login");
const custExp = await page.locator('#edit-name--description');
const finTextExp = await custExp.textContent()
await expect(finTextExp, "Not A Precise Match!").toContain('Enter');

});

const {test, expect} = require('@playwright/test');
test('ElementHandle', async ({page})=> 
{

await page.goto("https://www.programsbuzz.com/user/login");
const custExp = await page.locator('#edit-name--description');
const finTextExp = await custExp.textContent()
await expect(finTextExp, "Not A Precise Match!").toContain('Blue');

});

Tags
Submitted by arilio666 on June 13, 2022

Ashwin is working with iVagus as a Technical Lead. He has experience in various technologies like Cypress, JavaScript, TypeScript, Rest Assured, Selenium, Cucumber, and Java.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.