Automate OTP verification via email
An application may require the user to authenticate their identity via a One-Time Password (OTP) received on their email. This is often used when the application needs to authenticate a user for a single transaction or login session, for example.
Sofy can help you test & automate the OTP verification process for your application and capture information from an email using Sofy’s API feature. Via Sofy, you can not just fetch the exact email which contains your OTP but using the Regex feature of the API step, you can extract the precise OTP too, and thereby automate the entire verification process.
Here's how you can automate the OTP verification from an email via Sofy:
- Start recording a test scenario & click on the API icon towards the bottom of the Action Bar:
- For demonstrative purposes, we will use the Mailinator APIs here to extract information from an email which contains the OTP via a two-step process. You may have your own set of APIs that return the OTP for your App.
- After clicking on the API Step icon, you will see the following modal where you can ask Sofy to send a GET request at the URL of your email client and fetch the latest emails for you.
- For example, here, we are sending a GET request at a Mailinator URL for retrieving the most recent email from the mailbox - https://mailinator.com/api/v2/domains/private/inboxes?limit=1&sort=descending (Note that, we first sort all the received emails in descending order, and set the limit to 1 to retrieve only the most recent email from the sorted list).
Once you update the header with the authorization token and click on Send, the response will be populated as shown here:
- If you expand the details contained within the Response received, you can locate the id of the email message as shown below:
- You can then click on the '+' icon next to the id of the email to create a variable for it (in our case, we call this variable as "message_id") :
- After creating the test variable, message_id, and saving its value, click on the API step icon again to retrieve the message and create a test variable for the OTP code. Follow the steps below:
- In the API modal which appears, you can send a GET request to retrieve email message body (which contains the OTP) by instructing Sofy to fetch the email message using the message_id variable that was saved earlier. We will add the message_id as a path variable. For example, we first enter the URL (https://mailinator.com/api/v2/domains/private/messages/ ):
- ... and then click on the Path Variables tab and on the on Add new option:
- You can select the name of the test variable containing the email message from the dropdown (in our case, "message_id") and click on the checkmark icon to enable it:
- Next, click on the Query Params tab & click on Add new option:
- You can then specify the details of the authentication token as shown here and send the GET request:
- Upon receiving the response, you can expand the details contained within it to view the body of the email and over this email body, you can click on the '+' sign & declare a test variable for it:
- At this step, you can also define a regex over this email body to extract just the OTP from it:
- In the end, you can click on the Add API Step button of the modal and will be able to see the 2 API steps working in conjunction to retrieve the latest email (containing the OTP) from your email client and also extract the OTP from it.