November 10, 2026

Testing email one-time passwords end to end with Cypress

CypressE2EOTPTest automation

The goal

Many apps ask users to enter a verification code (one-time password) sent by email during sign-up or sign-in. To test that flow end to end in Cypress, you need three things:

  1. A fresh email address for every test
  2. A way to wait until the email your app sent arrives
  3. A way to pull the code out of the body

@mailbrew/cypress provides these as Cypress custom commands.

Setup

npm install -D @mailbrew/cypress @mailbrew/sdk

Add one line to cypress/support/e2e.js:

require('@mailbrew/cypress');

Pass your API key as an environment variable:

CYPRESS_MAILBREW_API_KEY=mb_your_key_here npx cypress run

An owner creates API keys on the dashboard's API Keys page. This test needs the scopes addresses:write (create addresses), addresses:delete (cleanup), and emails:read (read emails and extract codes).

Writing the test

describe('Sign-up', () => {
  it('completes registration with the emailed code', () => {
    cy.mailbrewCreateAddress().then((addr) => {
      // 1. Sign up with the new address
      cy.visit('/signup');
      cy.get('#email').type(addr.address);
      cy.get('#submit').click();

      // 2. Wait for the email (30 seconds by default)
      cy.mailbrewWaitForEmail(addr.address).then((email) => {
        expect(email.subject).to.include('verification code');

        // 3. Extract the code and type it in
        cy.mailbrewExtractOtp(addr.address, email.id).then((code) => {
          cy.get('#otp').type(code);
          cy.get('#verify').click();
          cy.url().should('include', '/dashboard');
        });
      });
    });
  });
});

Addresses created with cy.mailbrewCreateAddress() are deleted automatically after each test (a failed deletion does not fail the test). Because every test uses its own address, parallel runs never pick up another test's email.

How codes are extracted

cy.mailbrewExtractOtp() first looks right after words such as "code", "verification", or "OTP" for a 4 to 8 digit number or a hyphenated code like AB-12CD. If none is found, it looks for a 6 to 8 digit number in the body, then a 4 to 5 digit one, skipping numbers that look like dates or times. It returns null when nothing matches.

If your codes have a fixed shape, passing a regular expression is the most reliable option. With a capture group, only the captured part is returned.

cy.mailbrewExtractOtp(addr.address, email.id, { pattern: 'code: ([A-Z0-9]{6})' })

Checking subject and sender in one call

cy.mailbrewAssertEmail() checks conditions on the subject, sender, and body in one call. subject and from must match exactly; subject_contains and body_contains match partially.

cy.mailbrewAssertEmail(addr.address, email.id, {
  subject: 'Welcome to MyApp',
  from: '[email protected]',
  body_contains: 'verification code',
}).then((result) => {
  expect(result.passed).to.be.true;
});

Adjusting the wait

cy.mailbrewWaitForEmail() waits 30 seconds by default. Set timeout in milliseconds, up to 60 seconds.

cy.mailbrewWaitForEmail(addr.address, { timeout: 60000 })

If your app sends email through a queue, CI is often slower than your machine. When a test fails only in CI, try a longer wait first.

Running in CI

In GitHub Actions, pass the API key from your secrets as an environment variable:

- name: Run Cypress
  run: npx cypress run
  env:
    CYPRESS_MAILBREW_API_KEY: ${{ secrets.MAILBREW_API_KEY }}

Two things to watch out for:

  • The custom commands call the MailBrew API from the browser under test, so the API key is passed to the test browser. Do not use a key that can do more than this test needs; use a test key with only the scopes above.
  • If your organization uses IP restrictions, API key calls are restricted too. GitHub-hosted runners get a different IP address on every run and will get 403. In that case, run the tests on runners with a fixed outbound IP address.

Summary

Task Command
Create a test address (cleaned up automatically) cy.mailbrewCreateAddress()
Wait for an email cy.mailbrewWaitForEmail(address)
Extract a verification code cy.mailbrewExtractOtp(address, emailId)
Extract links cy.mailbrewExtractLinks(address, emailId)
Check subject, sender, and body cy.mailbrewAssertEmail(address, emailId, assertions)

Using Playwright instead? See Playwright OTP E2E testing. Full API details are in the API documentation.

← Back to blog