Authentication in Playwright

Anandhi K
5 min readJul 24, 2022

In most of the Web Applications, user must login into application to do any action and also for further interactions we may want to retain the same session. Same in case of automation testing for every scenario we must login into an application especially in e2e testing we may need to test with various user roles.

There are cases where we may repeat the Sign-in process like,

1. Before each Scenario,

2. Reuse the already Signed-in state,

3. Sign-in via API request using any Authorization methods

4. Or may want to Sign-in as multiple users for testing various roles.

All these can be implemented in Playwright without much coding. Playwright easily achieve this by using BrowserContext.

What is BrowserContext?

A BrowserContext is an isolated incognito-alike session within a browser instance. Browser contexts are fast and cheap to create. We recommend running each test scenario in its own new Browser context, so that the browser state is isolated between the tests. If you are using Playwright Test, this happens out of the box for each test. Otherwise, we can also create browser contexts manually.

Tests written with Playwright execute in isolated clean-slate environments called browser contexts

--

--

Anandhi K
Anandhi K

Written by Anandhi K

DevOps Test Automation Consultant, Trainer and Blogger in Cypress, Selenium, Cucumber, Playwright & CI/CD Tools.

Responses (1)