Recently I have automated 2-factor authorization for our organization. Although it is not simple. Once you will do login on the second layer you have to get OTP from google auth service or other similar services OR through Mobile SMS.
OTP we received on the second layer is, TOTP stands for Time-based
One-time Passwords and is a common form of Two-factor
Authentication. Unique numeric passwords are generated with
an algorithm that uses the current time as an input. The time-based
passwords expire in 30-60 seconds.
I have listed few ways below:
We have to talk to the Development team if there is an API or the library how they are calculating OTP. If there is any way to fetch OTP from DB. Luckily I got API to calculate the 2F authentication code. But I have tried in many ways to reach the solution.
Almost all API's to receive SMS are paid, if your project has budget you can choose Nexmo or Twilio
You can utilize some external libraries to calculate 2F OTP like here but it always not successful as it Time based OTP so after a few seconds it can be expired.
Unsecure but free way to get SMS is to use some service provider as mentioned here. These service providers give a phone number which is public and all SMS received on this number will be displayed on their dashboard so it is not secure. This SMS can be read using the Selenium script.
We can get SMS through appium script but it is hectic as you need to write the separate script for this and needs a mobile number with the device.