Videos
Check out our tutorial video series.
End-To-End Testing Multifactor Authentication with MailSlurp, NodeJS, and TestCafe - A Tutorial with Code Examples on GitHub".
TestCafe is a great NodeJS framework for end-to-end testing with built in browser support. You can use MailSlurp with TestCafe to test user sign-up multi-factor authentication with real email accounts. In this post we will show you how with examples. For full code see GitHub.
MFA is multifactor authentication. This is a common security layer to many web applications. Commonly this involves sending a verification code to a user's email address when they sign up in order to verify their account. For this example we can use a simple React App hosted at playground.mailslurp.com. This app has a common user sign up flow that allows a user to sign up with their email address and password. Upon sign up a user is sent a confirmation code by email that they must enter into the app to confirm their account.
This is what the email looks like:
It contains a numeric code that we will use MailSlurp and a regex pattern to extract.
TestCafe allows you to easily write end-to-end browser tests to test an application functionality. Using MailSlurp we can test application functionality using real email accounts. TestCafe will run a browser an click elements and inputs like a normal user.
Let's get started and set up a new project. Create an empty directory and add a package json.
Next we should install MailSlurp and TestCafe:
The file should look like this:
Now we are ready to write some tests.
Create a test file of any name - we will call ours test .
TestCafe expects us to define a fixture using tagged template strings. Let's do that and add a before method to instantiate MailSlurp.
MailSlurp lets you create real test email accounts on demand and send and receive emails in tests and code. MailSlurp is free for personal use but you need an API Key. Create a free account to get your API Key. When running the tests set as an environment variable to pass the API KEY to our test instance.
Now we can test the user sign up flow. To recap we need to perform the following steps:
The code for this is quite simple with TestCafe:
That's a bit of code so let's break it down:
We can create a test email address using MailSlurp like so:
Inboxes have an and an
. We can use the
to sign up for the test app:
Once signed up we need to receive the confirmation code to proceed. We can use the MailSlurp WaitForController to do so:
Then we can enter the code to confirm a user account. This is a common need for many user sign up flows:
Signing up with a confirmed user in our test app will show a happy dog:
TestCafe is an excellent choice for end-to-end testing. Use MailSlurp to add real email accounts to your end-to-end test suites. Create a free account today or see the developer page for more examples.
Check out our tutorial video series.
Email and SMS guides for automation and testing.
View github project code for multiple languages.
Latest posts from the MailSlurp team.
Test, build, and automate messaging with a free MailSlurp account.