vivek
vivek

Reputation: 147

Reading SMS using Appium on Android

I am trying to automated a flow where I can read sms and then parse the SMS to read the OTP and enter it in another webapp. Looked through all forums but couldn't find an answer.

I am writing code in java and trying to run a webapp on android device using appium.

Can someone suggest how to do it?

Upvotes: 2

Views: 7533

Answers (3)

SNOSeeds
SNOSeeds

Reputation: 176

You can add a key to store last OTP in an in-memory storage like Redis or closured Dictionary in your backend language, you will then update this key with the value of the OTP you want to send out via an SMS service provider. The last sent OTP stored in this key should then be exposed via an additional api endpoint in your service, which you would then call as part of your automated script after doing an action that will make the OTP to be sent.

A more reliable and robust modification of this will be to expose an API that retrieves the OTP stored against each phone number, you must have this stored already of course before you could do the verification in-app. This endpoint must only be exposed within a test environment, that's the precaution to pay attention to, or you make the endpoint available only to authorized user roles.

Upvotes: 0

anandhu
anandhu

Reputation: 780

This approach worked for me

Suppose your OTP sms is like "OTP is 12345 for your order", and it shows up in your notification panel,

  1. Reach the OTP Screen
  2. Pull down the notification bar ((AndroidDriver) driver).openNotifications();
  3. Wait for this element otpElement with xpath //android.widget.TextView[contains(@text,'OTP is')] is displayed (ie, While the sms arrives)
  4. Use OTPmessage = otpElement.getText() to get the OTP message text
  5. Extract OTP using this code : String OTP = StringUtils.substringBetween(OTPmessage, "OTP is ", " for ");
  6. Close notification panel using ((AndroidDriver) driver).pressKey(new KeyEvent(AndroidKey.BACK));

I dont think this is a good approach if you are testing the app. It might take lot of time for the OTP to arrive thereby failing your test unnecessarily. Better ask your devs keep the OTP fixed so you don't need to receive it through the sms everytime.

Upvotes: 1

dmle
dmle

Reputation: 3658

If you want to read sms from device in Appium session there is a good approach described in discuss.appium.io

I think you can create several sessions in one test: 1 - to get sms with native app, 2 - open webApp in mobile browser and do whatever you need

Upvotes: 1

Related Questions