Testing Two-Step Verification via the Email Testing feature
  • 20 May 2022
  • 4 Minutes to read
  • Contributors
  • Dark

Testing Two-Step Verification via the Email Testing feature

  • Dark

※English in this website is translated using machine translation.Please note that the content may not be 100% accurate.

Two-step verification is often used in web application login and user registration processes. The user interacts with the browser, opens the email containing the verification code, and enters it in the appropriate field on the browser. This is a complicated process, making automating the test very challenging.

With Autify, you can😉

In this article, you will learn how to automate a two-step verification (2SV) test by using Autify's Email Testing function and a JS step.


To automate a test involving an email-based two-step verification (the security code is sent to the user's email address), the application under test (AUT) and the Autify environment must meet the following prerequisites:

Prerequisites for the application under test (AUT)

  • The AUT must be able to specify the email address that Autify's Email Testing feature issues to the account to be authenticated (domain: testmail.autify.com).
  • The AUT's authentication process must issue an authentication code via email.

Prerequisites for Autify

  • The Email Testing feature must be available

    • If the Email Test function is available, an email icon will be displayed on the Autify Recorder, as shown below. This indicates that you are ready to start recording!
    • If the Email Test function is unavailable, an email icon will not be displayed on the Autify Recorder, as shown below. In this case, please request a trial by contacting us on the contact form. We will prepare the Email Testing feature for you to try out👍
  • If using a fixed email address, the email address must be created in advance.

    • You can create a fixed email address on the Workspace Settings Page. If you would like to know more about the difference between a fixed email address and a random email address, both of which can be used in the Email Testing feature, please refer to this article.
    • To select which type of email to use, please use the following as a guide:
      • Testing a two-step verification process where the same member logs in every time
        Fixed email address
      • Testing a two-step verification process where a user registers using a unique email address each time
        Random email address

What will the Scenario look like?

Below is an example of a completed Scenario. The following processes are performed:

  • Step 2: Enter the email address issued by Autify's Email Testing function.
  • Step 5-8: Open the email in a new window. In Step 8, a JS step is used to obtain and return the two-step verification code.
  • Step 11: Enter the two-step verification code returned in Step 7 into the 2-Step verification form.
  • Step 12-13: Proceed with the two-step verification process and confirm that it is successful.

Scenario Details Page

Test Result Page

In Step 11, you can see that the two-step verification code obtained in Step 8 has been entered.

How to create the Scenario

Here is how to create the Scenario shown above.

The process can be divided into two stages:

  • Record the Scenario
  • Edit the recorded Scenario

Record the Scenario

  1. Create the Scenario. Record the operations until the web application sends out the email containing the two-step verification code.

Open the email address from the recorder and click on the "Copy to Clipboard" icon to copy the target email address.

Return to the web application and enter the copied email address into the email address field. Go ahead and record the first step of the authentication process.

  1. After recording the step where the two-step verification email is sent out, open a new window to open the email. This window must be separate from the window you are currently interacting with. (URL is optional.)

  2. In the new window, click on the appropriate email address on the recorder, and open the email containing the two-step verification code.

  3. Add the Element Should Be Visible assertion, which confirms that the email's two-step verification code is visible. Also, check the CSS selector for that element (we will use it later in a JS step. For more information on how to check CSS selectors, please see this article).
    Once confirmed, close the window.

  4. Return to the window with the authentication form. Enter the two-step verification code shown in the email to the form, and continue recording.

Edit the recorded Scenario

After recording, we need to prepare a JS step to receive a dynamic two-step verification code via email.

  1. Insert the JS step right after the step that opens the two-step verification email.
  2. In the JS step, return the character string obtained from the email. Below is a sample. Please replace <TODO: REPLACE SELECTOR> with content that follows the structure of the email.
* Locate the element
var selector = "<TODO: REPLACE SELECTOR>";
var element = document.querySelector(selector);

* Stop process if it does not exist
if (!element) {
throw new Error('Error: cannot find the element with selector(' + selector + ').');

* Return text content
return element.textContent;

In the step where the two-step verification code is entered, change the input value to "Other step's result" and enter the value returned from the JS step.


That's it! Each email will contain a unique verification code which will be entered into the form!

We hope this article gave you an idea of how Autify helps automate tests that involve two-step verification using email. If you have any questions about email testing and authentication, don't hesitate to get in touch with our support team at any time.😊

Further reading

First name must have atleast 2 characters. Numbers and special characters are not allowed.
Last name must have atleast 1 characters. Numbers and special characters are not allowed.
Enter a valid email
Enter a valid password
Your profile has been successfully updated.