solutions
CodeceptJS Email Testing
Create mailboxes in Codecept-js tests to send and receive email, attachments, and two factor authentication codes.
MailSlurp is a free service for creating test mailboxes during tests. Using the official codeceptjs plugin @codeceptjs/mailslurp-helper you can send and receive emails (and SMS/TXT messages) in end-to-end tests to verify accounts, validate application sign-up, and check transactional emails.
How it works?
Simply add the codecept js helper like so:
npm i @codeceptjs/mailslurp-helper mailslurp-client --save-dev
Then configure your codecept.conf.js to add the MailSlurp helper:
exports.config = {
tests: './*_test.js',
output: './output',
helpers: {
MailSlurp: {
apiKey: process.env.API_KEY || 'YOUR_MAILSLURP_API_KEY',
require: '@codeceptjs/mailslurp-helper'
},
},
name: 'codeceptjs'
};
Then you can create and access email accounts instantly in your tests using native helpers like:
const mailbox = await I.haveNewMailbox()
Test email for real
Combining helper functions you can test end-to-end functionality like user sign-up and verification:
Scenario('Test user sign up', async ({ I }) => {
// create a dummy inbox with MailSlurp
const emailAccount = await I.haveNewMailbox()
const password = 'test-password';
// load application
I.amOnPage(MY_APPLICATION);
// fill signup form
I.click('[data-test="sign-in-create-account-link"]')
I.fillField('[name="email"]', emailAccount.emailAddress);
I.fillField('[name="password"]', password);
I.click('[data-test="sign-up-create-account-button"]');
// wait for confirmation email
const email = await I.waitForEmailMatching({
subject: "Please confirm your email address"
})
// extract content use regex pattern
const [_, code] = /verification code is (\d+)/.exec(email.body)
// submit verification code
I.fillField('[name="code"]', code)
I.click('[data-test="confirm-sign-up-confirm-button"]');
// now login with verified account
I.fillField('[name="username"]', emailAccount.emailAddress);
I.fillField('[name="password"]', password);
I.click('[data-test="sign-in-sign-in-button"]');
// see welcome message
I.waitForElement('img[src*="welcome"]', 30);
});