Sign up with an email code

Turn a sign-up that emails a code into a test that signs up a new address on every run.

Parcel's sign-up asks for an email address and a password, then emails a code to finish. This example turns it into a test. A sign-up test needs a new address on every run, because a second run with the same address would find the account already there, and the test inbox gives it one each time.

Before you start Link to Before you start

There is nothing to add. A flow that signs up needs no test account: Re:agent opens its browser as a new user, with a new address in the test inbox and a password that Rehearsal keeps.

Ask Re:agent Link to Ask Re:agent

Open Re:agent, start a New chat and describe the sign-up and what it should end on:

Output
Sign up for Parcel with a new account. Parcel emails a code to finish. Check that the welcome page opens.

What Re:agent does Link to What Re:agent does

Re:agent walks the sign-up in a browser beside the chat, and each step appears in the chat as it happens:

  • Opened a browser on parcel.example as a new user
  • Filled Email
  • Filled Password
  • Clicked Create account
  • Read the test inbox
  • Filled Code
  • Clicked Verify

Re:agent types {{email}} and {{password}}, and Rehearsal puts the new address and the password into the fields. It reads the code from the inbox and types it, and the flow writes the code down as {{code}}, so the chat never keeps it.

Once the welcome page opens, Re:agent saves the flow, and a block in its reply follows the new test through Flow saved, Test written, First run passed and Verified in a fresh browser. Then the test's card appears with Run now, Open test and Watch recording.

The test Link to The test

The test Rehearsal writes looks something like this:

TypeScript
import { expect, inbox, secret, test } from '@rehearsal-labs/retest';test('A new visitor signs up with an email code', { apps: ['visitor'] }, async ({ visitor }) => {  await test.step('Open the sign-up page', async () => {    await visitor.goto('/sign-up');  });  await test.step('Sign up with a new address', async () => {    await visitor.getByLabel('Email').fill(secret('email'));    await visitor.getByLabel('Password').fill(secret('password'));    await visitor.getByRole('button', { name: 'Create account' }).click();    await expect(visitor.getByLabel('Code')).toBeVisible();  });  const message = await test.step('Read the email', () =>    inbox('visitor').latest({ subject: 'code', timeout: 60_000 }));  await test.step('Enter the code', async () => {    await visitor.getByLabel('Code').fill(message.code);    await visitor.getByRole('button', { name: 'Verify' }).click();  });  await test.step('Check the welcome page', async () => {    await expect(visitor.getByRole('heading', { name: 'Welcome to Parcel' })).toBeVisible();  });});

secret('email') types the run's new address and secret('password') its password, so neither is in the file. inbox('visitor').latest() waits up to a minute for an email that arrived after the test started, and message.code types the code from it, a new one on every run.

If your app emails a link instead of a code, Re:agent opens the link and the flow writes it down as {{link}}. The test then opens it with visitor.goto(message.link()).

Run it Link to Run it

Press Run now on the card, choose the screens and press Run, or from a terminal:

Terminal
rehearsal run --test <test-id>rehearsal run wait <run-id>

Every run signs up a real account in Parcel, with an address that starts with fresh- and ends in @inbox.rehearsal.dev. That address keeps receiving mail for 30 days, and you can clear the accounts out of your test data whenever you like.

Next steps Link to Next steps