Dev diary - 26. June 2025

How to skip Azure login in UI tests using Playwright

header_image

Modern web apps often use Azure Active Directory (Azure AD) for authentication, which can be challenging for UI testing. You may wonder: How can you skip the Azure login screen entirely?

The answer is by injecting an Azure access token directly into your Playwright test session. This approach allows you to bypass the login flow and focus your testing where it really matters.

Why use an access token?

Testing a login flow through Azure (or any other identity provider) can be:

  1. 🐢 Slow (increased test runtime)
  2. 💣 Fragile (especially with MFA)
  3. 😫 Hard to automate

By using an access token, you skip the login screen and go straight to the actual test.

What you need

Make sure you have:

  1. A working Playwright setup (.ts or .js).
  2. An Azure AD access token (obtained via Azure CLI or a backend service).
  3. Knowledge of where your app stores tokens (usually localStorage or sessionStorage).

Getting the token

Use the Azure CLI to fetch an access token:

bash

>az account get-access-token --resource <your-app-client-id>
>

Or obtain it via an OAuth2 flow using MSAL or Client Credentials.

Injecting the token with Playwright

Here’s an example Playwright test in TypeScript that sets the access token in localStorage:

typescript

>import { test, expect } from '@playwright/test';
>

>test('Login via Azure access token', async ({ page }) => {
>// Navigate to your app
>await page.goto('https://your-app.com');
>

>// Inject the access token
>await page.evaluate((token) => {
>localStorage.setItem('access_token', token);
>// Add other tokens, like 'id_token' or 'expires_at', if needed
>}, 'YOUR_ACCESS_TOKEN_HERE');
>

>// Now visit an authenticated page
>await page.goto('https://your-app.com/dashboard');
>

>// Verify the user is logged in
>await expect(page.locator('text=Welcome')).toBeVisible();
>});
>

⚠️ Let’s talk about token expiry

By default:

  1. Azure AD access tokens expire in 1 hour.
  2. ID tokens often have even shorter lifetimes.

If you hardcode a token or cache it, your test may fail as soon as the token expires.

Why testers and developers need to discuss token validity

  1. Testers want long-lived tokens (e.g., up to 60 days) for stable CI and local test runs.
  2. Developers must balance test convenience with security and adhere to policies.
  3. CI/CD pipelines benefit from stable, reusable authentication states.

(Optional) Save Auth State for reuse

Playwright allows you to save a logged-in browser state and reuse it across test files:

typescript

>// After setting the token
>await context.storageState({ path: 'auth.json' });
>

>// Later
>const context = await browser.newContext({ storageState: 'auth.json' });
>

Don’t skip real login tests completely

While bypassing Azure AD login is great for speed, it’s still essential to test the actual login flow in at least one or two dedicated E2E test cases. Why?

✅ Ensure Azure AD integration is working (redirects, MFA, consent screens, etc.)

✅ Detect regressions in the authentication process

✅ Maintain full test coverage for critical flows

Summary

By using an access token in Playwright, you can:

✅ Skip Azure AD login screens

✅ Test authenticated flows faster

✅ Avoid flakiness from external dependencies

✅ Write more stable and maintainable UI tests

✅ Collaborate better with developers to balance test readiness and security

blog author
Author
Štefan Varšo

I'm a QA engineer with more than 10 years of experience. My working life is filled with many interesting projects, including audio/video QA, the medical field, and even mobile marketing applications. It was right here that I discovered the charm of testing with native mobile frameworks like Xcode. Leading the QA stream at Hotovo gives me great pleasure and satisfaction in the work I have done. I love to travel and have circled the globe — east to west. “Surprisingly”— yes! The Earth is not flat! I also enjoy testing good wine or craft beer, and when it’s too much, you can find me relaxing on a mountain peak or in freezing cold water.

Read more

Contact us

Let's talk

I hereby consent to the processing of my personal data.