<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/automating-otp-login-in-playwright-e2e-tests-mn72ermmg" -->

---
title: Automating OTP Login in Playwright E2E Tests | daily.dev
description: A practical walkthrough of automating OTP-based email login in Playwright E2E tests without mocking or bypassing auth. The solution uses imapflow to connect...
canonical: https://daily.dev/posts/automating-otp-login-in-playwright-e2e-tests-mn72ermmg
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Automating OTP Login in Playwright E2E Tests | daily.dev
og:description: A practical walkthrough of automating OTP-based email login in Playwright E2E tests without mocking or bypassing auth. The solution uses imapflow to connect...
og:url: https://daily.dev/posts/automating-otp-login-in-playwright-e2e-tests-mn72ermmg
og:image: https://api.daily.dev/og/posts/mN72ERmmg.png
og:image:alt: Automating OTP Login in Playwright E2E Tests
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Automating OTP Login in Playwright E2E Tests

**[Medium](https://daily.dev/sources/medium_js)** · 4 min read · 1 upvotes · 0 comments

## Summary

A practical walkthrough of automating OTP-based email login in Playwright E2E tests without mocking or bypassing auth. The solution uses imapflow to connect directly to a real test mailbox over IMAP, mailparser to extract the OTP from the email body, and a polling loop with stale-email filtering to ensure only fresh codes are used. Key reliability features include mailbox locking, guaranteed IMAP cleanup in a finally block, automatic retry on expired codes, and support for accounts with OTP disabled. A noted constraint is that parallel test runs cannot share the same OTP-enabled account due to email race conditions.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://medium.com/@kia.sami8/automating-otp-login-in-playwright-e2e-tests-c9a65f0f6095>

## Questions this post answers

### How do I automate email-based OTP login in Playwright end-to-end tests without mocking authentication?

Connect Playwright to the real test mailbox over IMAP using imapflow, then parse incoming messages with mailparser and extract the code with a regex like /OTP code is:\s*(\d{4})/i. Poll the mailbox every two seconds until timeout, filter to messages from the app's known sender with the exact subject, and reject any email sent before the current login attempt to avoid reusing stale codes.

_Automating login flows reliably is easier with daily.dev's steady stream of testing techniques from real practitioners._

### Why does my Playwright test type OTP digits one by one instead of filling each PinInput box directly for Chakra UI's PinInput component?

Typing the full code with a short delay between keystrokes, rather than filling each digit box independently, is required because Chakra UI's PinInput component manages internal state and focus based on sequential keystroke behavior. Filling boxes directly can break that internal state tracking and produce unreliable test results that don't reflect real user interaction.

_Developers wrestling with component-specific test quirks can find more edge-case fixes on daily.dev._

### How do you prevent Playwright OTP tests from failing when running multiple test accounts in parallel against the same mailbox?

Concurrent OTP requests against the same email account can race for the same message and end up selecting or invalidating each other's code, so OTP-enabled test accounts need to be dedicated to a single active test run at a time. This constraint shapes how accounts are allocated across a parallel test suite to avoid collisions.

_Anyone designing parallel test suites can track practical concurrency lessons like this via daily.dev._

## Similar posts on daily.dev

- [Improper OTP Implementation to Full Account Takeover](https://daily.dev/posts/improper-otp-implementation-to-full-account-takeover-okroyeruh) · InfoSec Write-ups · 0 upvotes · 0 comments
- [My experience writing automated tests for a SPA](https://daily.dev/posts/my-experience-writing-automated-tests-for-a-spa-xevl2tw5c) · Lobsters · 0 upvotes · 0 comments
- [5 Ways to Bypass Email Verification Without Using Any Tool](https://daily.dev/posts/5-ways-to-bypass-email-verification-without-using-any-tool-bohenk6yt) · InfoSec Write-ups · 1 upvotes · 0 comments
- [Putting email in its place with Emacs and Mu4e](https://daily.dev/posts/putting-email-in-its-place-with-emacs-and-mu4e-i9uvubu5u) · Hacker News · 0 upvotes · 0 comments

---

Tags: [#javascript](https://daily.dev/tags/javascript), [#testing](https://daily.dev/tags/testing)

[View this post on daily.dev](https://daily.dev/posts/automating-otp-login-in-playwright-e2e-tests-mn72ermmg)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Automating OTP Login in Playwright E2E Tests","url":"https://daily.dev/posts/automating-otp-login-in-playwright-e2e-tests-mn72ermmg","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/automating-otp-login-in-playwright-e2e-tests-mn72ermmg"},"datePublished":"2026-07-27T20:43:24.275Z","dateModified":"2026-09-14T09:29:40.348Z","description":"A practical walkthrough of automating OTP-based email login in Playwright E2E tests without mocking or bypassing auth. The solution uses imapflow to connect...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/86fb6ec393959fed0d7aaaa5ad3813ec?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/86fb6ec393959fed0d7aaaa5ad3813ec?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Medium","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Medium","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/medium","url":"https://daily.dev/sources/medium_js"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/automating-otp-login-in-playwright-e2e-tests-mn72ermmg","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"javascript,testing","timeRequired":"PT4M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Medium","item":"https://daily.dev/sources/medium_js"},{"@type":"ListItem","position":3,"name":"Automating OTP Login in Playwright E2E Tests"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/automating-otp-login-in-playwright-e2e-tests-mn72ermmg#faq","mainEntity":[{"@type":"Question","name":"How do I automate email-based OTP login in Playwright end-to-end tests without mocking authentication?","acceptedAnswer":{"@type":"Answer","text":"Connect Playwright to the real test mailbox over IMAP using imapflow, then parse incoming messages with mailparser and extract the code with a regex like /OTP code is:\\s*(\\d{4})/i. Poll the mailbox every two seconds until timeout, filter to messages from the app's known sender with the exact subject, and reject any email sent before the current login attempt to avoid reusing stale codes. Automating login flows reliably is easier with daily.dev's steady stream of testing techniques from real practitioners."}},{"@type":"Question","name":"Why does my Playwright test type OTP digits one by one instead of filling each PinInput box directly for Chakra UI's PinInput component?","acceptedAnswer":{"@type":"Answer","text":"Typing the full code with a short delay between keystrokes, rather than filling each digit box independently, is required because Chakra UI's PinInput component manages internal state and focus based on sequential keystroke behavior. Filling boxes directly can break that internal state tracking and produce unreliable test results that don't reflect real user interaction. Developers wrestling with component-specific test quirks can find more edge-case fixes on daily.dev."}},{"@type":"Question","name":"How do you prevent Playwright OTP tests from failing when running multiple test accounts in parallel against the same mailbox?","acceptedAnswer":{"@type":"Answer","text":"Concurrent OTP requests against the same email account can race for the same message and end up selecting or invalidating each other's code, so OTP-enabled test accounts need to be dedicated to a single active test run at a time. This constraint shapes how accounts are allocated across a parallel test suite to avoid collisions. Anyone designing parallel test suites can track practical concurrency lessons like this via daily.dev."}}]}
```

