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:
driver.startActivity(AppPackageName, AppActivityName);
To Read OTP from notification we can open notification using below code:
driver.openNotifications()
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")[1]
Above code will return OTP into the string variable. Now you can use below code to come back to the application.
driver.navigate().back();
Note: This solution is for Android only.