How to write a code to read the OTP message from message inbox and switch back to native app in appium

Can anybody help me to write a code to read the OTP message from message inbox and switch back to native app.

Android or iOS? or both? :slight_smile:

Currently I am working for Android app but same will be required for ios too.

So for Android, everything is visible to appium, so you can hit home button (adb shell input keyevent 3), navigate to your SMS default app and read the OTP. The hard part in here will be making a code broad enough to cover most of Android models.

On iOS go to Settings --> Notifications --> Messages and choose the Alerts style for your SMS. Once the OTP reaches the device, an alert is displayed containing the initial part of your SMS. With lucky your OTP will be visible there and appium can see that message. Downside is that for any SMS your device receives that alert will be triggered.

1 Like

I would suggest do not, try to validate OTP for IOS, as @Telmo_Cardoso said it is feasible by generating notification in style of alert message, it has uncertainity and it has high chances of failures.

@Telmo_Cardoso I support the Android solution suggested above. But how can you read OTP from the message?

Message text is visible in appium. Imagine SMS text is “Your OTP code is 4459”, you just need to read the String into code and treat it in order to retrieve the OTP, with substrings or replacing all non digits with nothing:

str = str.replaceAll("\\D+","");

Thats just code, nothing to do with appium.

Then what would be the code for switch back to native app in appium after reading OTP

In Android you can launch your launcher activity:

adb shell am start -n

or tap on home button and tap on your apps icon (in case its there) or go to currently running apps and select yours, etc. I normally launch my app activity.

OTP is a part of authentication process. Just like captcha , you can request your dev to make a constant value of OTP for your application.

I have implemented this to use contant value, but how can i extract the prospect id(generated after api call is done) to appium in order to use the logic our developer has build?

Can you please explain how to go currently running apps?
Currently I am saving the otp screen activity and launching the same after reading OTP. but sometimes it is displaying that “Unable to launch the app: Error: Permission to start activity denied. (WARNING: The server did not provide any stacktrace information)” error message.


You can use:

adb shell input keyevent KEYCODE_APP_SWITCH

to open recent apps


Thank You…I will check.


I need the same requirement to read and extract OTP from outlook mail. Can anyone help me?

Appium for Android & iOS
Selenium Webdriver with TestNG framework.
Native App

Login into the app with username & password.
=>It will open an OTP passcode form.
Then, Find the Outlook mail inbox for the OTP mail.
Extract the OTP.
Enter into the Passcode form Text box.
Click on Submit button.

Thanks in advance!

Will that be possible to read OTP from a mobile browser (chrome/uc/opera or native). Not an APP. If yes, how and what need to be done.

I am having the same challenge
Have you find any solution for this

Hi Telmo, I would like to copy the OTP from messages and paste it on Anndroid. how is that possible?
Can you please tell a way?

Your didital OTP is ‘6789’.