Reading SMS using Appium on Android

Viewed 5293

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?

2 Answers

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

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.

Related