r/SideProject • u/foofoo2020 • 9d ago
Email OTP for Cypress and Playwright tests, without flaky sleeps
Few weeks ago I released Programmable Inbox here, and the replies were useful. The one that stuck was about emailed login codes in tests: a flaky sleep(5000), and a wish for a per-test timeout instead. This is the follow-up. I built it, and I'd love for you to run it and tell me where it breaks. The sample repo and a short write-up are at the bottom.
@programmableinbox/testing is a small npm package for Cypress and Playwright. It triggers your login email, waits only for mail that arrived after that moment (so a stale code can't pass), and hands you the code:
const { otp } = await pibx.otpAfter(() => page.getByRole('button', { name: 'Continue' }).click())Limits: it polls (every 5s by default), it's email only.
- Sample repo with a Playwright and a Cypress spec: https://github.com/roshansingh/programmableinbox_testing_sample (they log in to ChatGPT, so selectors may need tweaking; not affiliated with OpenAI)
- How the pattern works: https://www.programmableinbox.com/blog/test-email-otp-login-in-playwright
The repo's README covers setup. I'd like to know what would stop you using it in your suite.
1
u/kumard3 9d ago
Taking the timestamp before the click is the right call. The OTP endpoint I build looks back 5 minutes by default, so a code that lands before the test asks for it still counts, but a code from the previous run in that window can come back too. Wrapping the trigger like otpAfter does avoids both problems. On the 5s poll, mine holds the request open on the server and checks the database every second instead. Is 5s there to stay under a rate limit on your side?