Member-only story
How to Handle Authentication in E2E Testing with Playwright
*Knock-knock*, who’s at the door?
Authentication is a process of recognizing user identity. When a user enters their credentials (usually login & password) and, depending on an identification strategy, a user is being determined by a system. Then all necessary user permissions are verified, and they might have access to particular resources (aka authorization).
But that’s not the point right now. The goal is to explain how to deal with authentication on your e2e test setup with the playwright test library.
Created by Microsoft, playwright makes the process of writing e2e scenarios easier than we’ve ever imagined. It’s cross-platform, resilient, has an amazing set of tools like trace viewer, inspector, codegen and so on. And one of the major advantages is full isolation with browser context, which is really helpful for auth itself.
Let’s start with the requirements first and answer the question, “What do we want to achieve and see as an outcome of the exercise?”
- To check if the authentication works as expected;
- To store the state of the authentication (in our case, cookies-based) and reuse it by other scenarios;
- To authenticate a user at the beginning of all e2e scenarios executed.