How to automate SMS based two factor authentication(2FA)


Prerequisites

  • A Testsigma account. If you do not have one already, sign up for a free trial
  • Understanding of creating and running test cases.
  • Provisioned phone numbers.
  • Familiarity with regular expressions. If not, refer to Regex guide - MDN Docs.

  • Steps to automate SMS based OTP verification

    Step 1: Record elements

    Before moving on to the test case, you must capture elements on the login screen of your application. For more information on how to record elements using Testsigma chrome recorder extension, refer to

  • Web application
  • Android
  • iOS
  • Step 2: Configuring your phone number

    Testsigma provides you with a valid mobile phone number on request, with which you can author any test case that requires a valid phone number.

    NOTE:

    The 2FA testing (email and phone number) is offered as part of the enterprise plan. The feature is available for the below mentioned countries & supported for SMS OTP.
    Australia, Canada, USA, Czech Republic, Denmark, Hungary, Indonesia, Mexico, Netherlands, Romania, Slovakia, Sweden, United Kingdom. For other regions, contact sales.

    To view the phone numbers allocated to your account, navigate to Settings> Phone numbers.

    Step 3: Create a test case

    If you are not familiar on how to create a simple test case, refer to create a testcase.

    Step 4: Create a test step to enter the phone number to which the OTP will be sent

    We will enter the phone number provided by Testsigma using the Phone Number test data type, using a test step.

    1. In your test case, add a test step to enter the phone number in the phone number field on your application page.
    2. Clear the < test data > placeholder and select the %|Phone Number| test data type from the suggestions list. An overlay showing the list of available phone numbers (allocated to your account) would open up.
    3. Select one of the numbers from the list. This is the number where the OTP SMS will be sent.
    4. Next, use the NLP Click on SendOTP

    Step 5: Create a test step to fetch the OTP from the SMS received on the Phone number

    We will use the test data generators available in Testsigma to fetch the OTP from the phone number selected in the previous step.

    1. Add a test step to enter the OTP code in the OTP Field
    2. Clear the < test data > placeholder and select the !|Function| test data type
    3. Search and select the test data generator function !|PhoneNumberFunctions - getOTP|
    4. Enter the following arguments for the function and create the test step.
      • Regex: The Regex command specifies the format of the OTP i.e alphanumeric or pure number or pure alphabetical etc. For example: for an OTP which is a number with a length of 4 to 8, we can use the regular expression \d{4,8}.
      • Phone number: This field specifies one of the pre-configured phone numbers, in this case its +12082045810
      • Timeout: Duration of the wait time for the OTP to be generated
    5. Once these values are passed the PhoneNumberFuntion will use the parameters to read the generated OTP. The below GIF depicts the method to automate email based OTP verification in real time using a sample application:

    sms based 2fa automation