Urgent.News

What's breaking now, across thousands of outlets.

Tech

Automate Email Verification in Playwright using Microsoft Graph API & TypeScript published: true

Automating end-to-end flows like user registration, password resets, or OTP verifications often hits a wall when dealing with emails. Automating UI login for webmail providers like Gmail or Outlook is flaky and frequently blocked by 2FA or CAPTCHAs. Using Microsoft Graph API with an Azure App Registration (Client Credentials Flow) lets you fetch target user inbox messages programmatically in your…

Automating email verification in Playwright tests can be challenging due to issues like 2FA or CAPTCHAs blocking UI login for webmail providers such as Gmail or Outlook. Microsoft Graph API, combined with an Azure App Registration using the Client Credentials Flow, provides a solution to programmatically retrieve inbox messages in Playwright tests without any UI interaction.

To implement this approach, follow these steps:

1. Set up your Azure Entra ID (formerly Azure AD) by registering an app, navigating to API permissions to add the Microsoft Graph Mail.Read permission, granting admin consent, and creating credentials with a client secret. Collect the necessary environment variables: AZURE_TENANT_ID, AZURE_CLIENT_ID, and AZURE_CLIENT_SECRET.

2. Install the required dependencies, including the Azure Identity SDK and Microsoft Graph Client, using npm.

3. Create a helper service (graphService.ts) to handle authentication, inbox polling, and token extraction. This service includes methods like getLatestEmail to poll the user's inbox for an email with a specific subject within a timeout period, and extractPattern to extract OTP codes or links from HTML content using regular expressions.

4. Integrate the Graph email service into your Playwright tests. Initialize the GraphService, register for a test email address, trigger the signup action in the web application, fetch the latest email using the service, extract the OTP code from the email content, complete the verification process on the page, and assert successful navigation to a dashboard.

By following these steps, you can automate email verification in Playwright tests without relying on UI elements that are prone to flakiness, thereby reducing the chances of test failures due to factors like 2FA or CAPTCHAs.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Framework Comparison Report: ZIM vs. PixiJS

NOTE: Prompted by Dr Abstract who also requested article from Gemini. When building interactive 2D canvas experiences on the web—whether mini-games, educational interactives, infographics, or visual…

More from Saturday 15 August →