Reputation: 175
In project I use node js 20.5.1, playwright 1.37. with cucumber 8.0.0. I want to add own customer message if expected method return me a failed. I thought I could do it like below:
expect(3),"My custom message").toBeLessThanOrEqual(2);
And in test result I should for that step Error: My custom message,
,but I only see
Error: expect(received).toBeLessThanOrEqual(expected)
Do you know how I could handled with that ? :) Thanks for any tips :)
Upvotes: 3
Views: 6059
Reputation: 1
I know this is a bit of an old question, but I had faced the same issue recently and came up with a "solution". Since Playwright native messages for assertions isn't supported by Cucumber as test runner, I created the following function to help with that:
export async function executeAssertion(assertion: () => void | Promise<void>, message: string) {
try {
await assertion()
}
catch (err) {
throw new Error(`${message}\n${(err as Error).message}`)
}
}
To use, you can do like this
// for a single assertion
await executeAssertion(() => expect(3).toBeLessThanOrEqual(2), 'Three was not less than or equal to two!')
// for multiple assertions
await executeAssertion(
async () => {
await expect(something).toBeVisible()
await expect(somethingElse).toBeHidden()
},
'Locator was not visible!'
)
The failure log would then look something like this:
Three was not less than or equal to two!
Error: expect(received).toBeLessThanOrEqual(expected)
// the rest of the error stack trace would also be shown here
Upvotes: 0
Reputation: 1
It seems like your error refers to another line of code. Anyway, in order to add a custom message to expect statement you should use:
await expect(page.getByText('Name'), 'should be logged in').toBeVisible();
as you can see in their official documentation here
Upvotes: 0
Reputation: 1
Custom error message are being published using Chai
Note:- Chai 5.x is esm only
Using chai 4.5 with cucumber 10.x and playwright 1.4
Upvotes: -1
Reputation: 41
I have reproduced your issue : The custom message will only show if you are using the Playwright test framework and reporter. If using Cucumber one like you, the custom error message will not show, and I don't see any solution so that it shows.
Upvotes: 0