Playwright

12 Playwright Tips and Tricks

Playwright is exceptional for end-to-end testing. It can do so much, from interacting with forms to mocking server requests and a heap of other things. In this article, we’ll go over 12 tips and tricks (some you might already be aware of). 1. Parallel Testing Playwright makes it easy to run multiple tests in parallel. This is done by creating multiple context objects, which are isolated and can run in parallel without interfering with each other. Here’s an example:

Reliably waiting for network responses in Playwright

Playwright makes what used to be complicated in end-to-testing almost too easily, especially waiting for network requests. However, in the case of single-page applications (SPA) where a click on a link or button maybe fire a request but not a page load, you can encounter a situation where Playwright encounters a race condition between clicking and waiting for a response. The issue is sometimes the waitForResponse in situations where the request might be faster than the click event finishing, it acts as if it never took place and times out. This issue is acknowledged in the Playwright documentation for page.waitForResponse.

How To Test Your Web Applications Using Playwright and Support 2fa Tokens

At work, I’ve been migrating us over from Cypress to Playwright for end-to-end tests. In that time, we’ve enabled two-factor authentication functionality in our application for security. These TOTP tokens are great for security but provide an additional challenge for testing. While Playwright supports saving state, our application tokens have a short expiry. I needed to log in to our application, enter a one-time password, and test. The logging-in part was easy. We created development environment credentials. But because 2fa is enabled in all environments, we still had the issue of needing a TOTP token.