> For the complete documentation index, see [llms.txt](https://help.autify.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://help.autify.com/nocode-web/en/autify-university/advanced/authentication/how-to-test-two-step-verificat.md). Agents can also search and read this documentation through its [MCP server](https://help.autify.com/~gitbook/mcp).

# How to Test Two-Step Verification Using the Email Testing feature

Two-step verification is often used when logging into or creating a user account for a web application. 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 quite 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](https://help.autify.com/nocode-web/en/autify-university/advanced/email-testing-feature/email-test.md) feature and a [JS Step](https://help.autify.com/nocode-web/en/autify-university/advanced/js-steps/javascript-steps.md).

## Prerequisites

To automatically test an email-based 2SV process where the security code is sent to the user's email address, the application under test (AUT) and Autify environment must meet the following prerequisites.

### Prerequisites for the application under test (AUT)

* The AUT must allow you to specify the email address Autify's Email Testing feature issues to the target account (domain: testmail.autify.com).
* The AUT's verification process must issue an authentication code via email.

### Prerequisites for Autify

* The [Email Testing](https://help.autify.com/nocode-web/en/autify-university/advanced/email-testing-feature/email-test.md) feature must be available.
  * **If the Email Test feature is available**, you will see an email icon on the Autify Recorder, as shown below. You are ready to start recording!

    ![](https://1964297753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVdZPTgwISYb3LhjgKnw6%2Fuploads%2Fgit-blob-ea343b071e5cc3f72ff967ed0cbd7a23f4793af1%2Fzndsk-Screenshot_2022-03-10_19.46.37.png?alt=media)
  * **If the Email Test feature is unavailable**, the email icon will not be displayed on the Autify Recorder, as shown below. In this case, please request a trial by contacting us via the [contact form](https://autify.com/contact). We will set up the Email Testing feature for you to try out👍

    ![](https://1964297753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVdZPTgwISYb3LhjgKnw6%2Fuploads%2Fgit-blob-3d9ef47ce4a40fe07123a8af53212f80d41c9da1%2Fzndsk-Screenshot_2022-03-11_15.24.52.png?alt=media)
* 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 see [**this** **article**](https://help.autify.com/nocode-web/en/tips-1/mail-test-tips/random-and-fixed-email-addresses.md).
  * Refer to the following rule of thumb to decide which type of email to use:
    * Testing a 2SV process where the same member logs in every time\
      → *Fixed* email address
    * Testing a 2SV 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 actions are performed in each step:

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

### Scenario Details Page

![](https://1964297753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVdZPTgwISYb3LhjgKnw6%2Fuploads%2Fgit-blob-9f5b807dceed3c4e4fda583c7e5c3c41f238e70e%2Fzndsk-Screenshot_2022-03-10_20.16.17.png?alt=media)

### Test Result Page

![](https://1964297753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVdZPTgwISYb3LhjgKnw6%2Fuploads%2Fgit-blob-a9c054aa6f0a18327b9f569f95bea1e41f500a50%2Fzndsk-Screenshot_2022-03-10_20.10.21.png?alt=media)

![](https://1964297753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVdZPTgwISYb3LhjgKnw6%2Fuploads%2Fgit-blob-324c5ab34e98c0ab18abe1f7b714139e1dd8591d%2Fzndsk-Screenshot_2022-03-10_20.10.39.png?alt=media)

In Step 11, you can see that the 2SV code obtained in Step 8 has been entered.

![](https://1964297753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVdZPTgwISYb3LhjgKnw6%2Fuploads%2Fgit-blob-7b58de978c6c288dfc8b14f2625c3ff8ebd0c7fa%2Fzndsk-Screenshot_2022-03-10_20.14.58.png?alt=media)

## How to create the Scenario

Now, let's take a look at 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 actions until the web application sends the email containing the 2SV code.

Open the email address on the Autify Recorder and click on the **Copy to Clipboard** icon to copy the target email address.

![](https://1964297753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVdZPTgwISYb3LhjgKnw6%2Fuploads%2Fgit-blob-f7bcc98192c600dcddf836e71af52a21994aa304%2Fzndsk-Screenshot_2022-03-10_20.22.44.png?alt=media)

![](https://1964297753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVdZPTgwISYb3LhjgKnw6%2Fuploads%2Fgit-blob-aa7b5d138d11c92b272291e3dddc6e92314be8cf%2Fzndsk-Screenshot_2022-03-10_20.24.27.png?alt=media)

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 verification process.

![](https://1964297753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVdZPTgwISYb3LhjgKnw6%2Fuploads%2Fgit-blob-5b2c246824fef30c6d6da913048c6d5e9b589ecc%2Fzndsk-Screenshot_2022-03-10_20.27.24.png?alt=media)

2. After you've recorded the step where the 2SV 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.)

   ![](https://1964297753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVdZPTgwISYb3LhjgKnw6%2Fuploads%2Fgit-blob-76e1f90257d205c919c83041fab9256618888f1e%2Fzndsk-Screenshot_2022-03-10_20.29.21.png?alt=media)
3. In the new window, click on the appropriate email address on Autify Recorder, and open the email containing the 2SV code.

   ![](https://1964297753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVdZPTgwISYb3LhjgKnw6%2Fuploads%2Fgit-blob-6ad113c998173e6fe4bb669061b569bae1c6ab81%2Fzndsk-Screenshot_2022-03-10_20.34.17.png?alt=media)
4. Add the *Element Should Be Visible* [assertion](https://help.autify.com/nocode-web/en/autify-university/foundation/creating-a-scenario/assertions.md) to confirm that the email's 2SV 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](https://help.autify.com/nocode-web/en/autify-university/advanced/locator-feature/how-to-get-css-selectors.md)). Then, close the window.

   ![](https://1964297753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVdZPTgwISYb3LhjgKnw6%2Fuploads%2Fgit-blob-9db3cfa1148f7f325a230b52eb01f8d7ba55eaee%2Fzndsk-Screenshot_2022-03-10_20.48.39.png?alt=media)
5. Return to the verification form window. Enter the 2SV 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 2SV code via email.

1. Insert a JS Step right after the step that opens the 2SV email.

   ![](https://1964297753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVdZPTgwISYb3LhjgKnw6%2Fuploads%2Fgit-blob-4fc6956b1762b72e3068eda0d377a887f8217970%2Fzndsk-Screenshot_2022-03-10_20.49.48.png?alt=media)
2. In the JS step, return the character string obtained from the email. A sample code is shown below. Please replace \<TODO: REPLACE SELECTOR> with content that follows the structure of the email.

```javascript
/**
* 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 2SV code is entered, change the input value to "Other step's result" and enter the value returned from the JS Step.

![](https://1964297753-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FVdZPTgwISYb3LhjgKnw6%2Fuploads%2Fgit-blob-e5d81913554010b6c959245b096cf51ca47c369d%2FScreenShot2021-06-24at8.37.22_en.png?alt=media)

That's it! Autify can now receive an email that contains a unique verification code and enter it into the form!

We hope this article has helped you understand how to automate tests that involve an email-based two-step verification using Autify. If you have any questions about email testing and authentication, feel free to get in touch with our support team😊

## Further reading

* [Testing various authentication methods](https://help.autify.com/nocode-web/en/autify-university/advanced/authentication/testing-various-authentication.md)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://help.autify.com/nocode-web/en/autify-university/advanced/authentication/how-to-test-two-step-verificat.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
