Rayrun

How do I create a basic test assertion using Playwright?

Answer

To create a basic test assertion with @playwright/test, use the expect function and choose a suitable matcher. For instance, if you want to check if a variable success is truthy, write:

expect(success).toBeTruthy();

This assertion throws an error if success is falsy.

For web-specific async matchers, use await with expect. To check if an element with test id 'status' has text 'Submitted', write:

await expect(page.getByTestId('status')).toHaveText('Submitted');

Playwright re-tests the element until the condition is met or a timeout occurs. The default timeout is 5 seconds, but you can configure it in your test configuration file or pass it directly when making assertions.

Thank you!
Was this helpful?
Still have questions?

If you still have questions, please ask a question and I will try to answer it.

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].