Rayrun
← Back to videos

Playwright - Retry Blocks of code using this new Assertion Method!!

In version 1.29 of playwright we can now retry blocks of code until they are passing successfully.

In this video, we focus on a new improvement added to the Playwright version 1.2 testing tool: the assertion method. This allows us to retry code blocks until all assertions successfully pass. This handy feature helps to make the most unreliable tests significantly more stable.

The presenter, Jared, shows us the updates on version 1.2. He performs a basic test on the Computer Database website, demonstrating how to add a computer and run assertions to verify the computer has been added. The test initially fails because he intentionally includes the wrong name for the computer in the code. Jared then introduces the two pass assertion method, which retries this block of code until the assertion successfully passes or a predetermined time-out is reached.

The main advantage of this feature, compared to test retries, is that you can focus it on specific blocks of your code. In real-world scenarios, this approach is more efficient and helps reduce the time wasted on retrying entire tests from scratch. You can single out sections of your test that are potentially problematic due to some flaky behavior and wrap them in this assertion retry to prevent flakiness and save on testing time.

Playwright also provides an option to customize the test interval and time-out settings. By default, the intervals are set at 100ms, 250ms, 500ms, then 1000ms. However, you can overwrite these to suit your needs. For time-outs, it's important to understand the difference between default test time-outs and web-first assertion time-outs, which typically wait for up to 5 seconds.

All in all, this new assertion method not only improves the reliability of your tests but it also saves you valuable testing time. This can be particularly beneficial for UI testing, where instability can be an issue. Despite its advantages, the aim is to always have stable tests and, ideally, minimize the need for these types of methods. However, it's also essential to understand the real-world application of these tools and adapt accordingly.

Related Videos

Playwright Tutorial | Get Call for API Testing with Playwright | Pass BaseURL and Headers
Playwright Tutorial | Get Call for API Testing with Playwright | Pass BaseURL and Headers
Get started with end-to-end testing: Playwright | Episode 2 - Getting Started
Get started with end-to-end testing: Playwright | Episode 2 - Getting Started
#16 - ARIA and how Playwright is making use of it to identify UI controls?
#16 - ARIA and how Playwright is making use of it to identify UI controls?
Playwright Version 1.14 | Playwright Tutorial - Part 45
Playwright Version 1.14 | Playwright Tutorial - Part 45
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 luc@ray.run.