You can automate OTP validation from Appium in two ways.
- By opening messages application in android and reading OTP from there.
- By opening notifications and read OTP.
To open messages application we need to open the app using below code:
To Read OTP from notification we can open notification using below code:
Now you can inspect OTP text element and read the text. For example, if your OTP text is: Your OTP is 3357
You can get OTP as below:
string otp = driver.findElementByXpath("//*[contains(@text,"Your OTP is")]").getText().split(" is")
Above code will return OTP into the string variable. Now you can use below code to come back to the application.
Note: This solution is for Android only.