As there are two options to read OTP:
- Start the activity of the messaging app and then read the otp from there.
- Read the OTP from the notification tray.
Below the function to read the OTP in Appium:
public static String readOTP()
{
//driver.startActivity("com.android.mms", "com.android.mms.ui.ConversationList");
driver.openNotifications();
String otp = driver.findElementByXPath("//*[contains(@text,'is')]").getText().split("code:" )[0];
return otp;
}
Hope this will help !!!