How to read OTP from messages using Appium?

Android Platform:
Hi Team,
I have a scenario, Where i can read the OTP from messages and entered it in the app. For example, When we trying to login to the Paytm,It sends one OTP . I need to read it this OTP and enter it in the textbox using Appium. could you please let me know,how could we can read the OTP value in messages.
Thanks,
Shiva Oleti

Hi,

Android or iOS (and iOS using latests driver?)

see search button :slight_smile: - this question appears every month. there are many answers and even ‘code’ examples inside.

Hi,
Android Platform.
Regards,
Shiva Oleti

Or you can implement your own code to open native app to read SMS, in that case its trickier, because normally on each device there is a different SMS client

Hello @Telmo_Cardoso, you are right I am using the XCUITest driver.
I am using iphone 6 with IOS 10.3.2, after receiving the OTP the notification panel is not reachable to Appium (I have used driver.getPageSource()).
Thank you for you for your response, but it seems it does not works anymore with IOS > 10.3 (Apple has changed the SMS notification panel).
I also did not know I can read out of the native app I am testing (with XCUITest driver), Do you have an example how to implement it with the java driver?

With WebDriverAgent I’m able to see everything on iOS (working with iPhone5C - iOS10.3.1).

I’m just swiping down notification panel, reading OTP and swiping up to close panel.

Sorry, this is too general answer, please send a code example or a link.
I have tried both WebDriverAgent and “swiping up” but it just seems it is not working. (both iPhone 5c & 6 IOS 10.3.2)

To open notification panel I use:

GesturesHelper.gestSwipeVertical(driver, 0.01, 0.9, 0.5, 1000);

witch is the function:

public static void gestSwipeVertical(AppiumDriver driver, double startPercentage, double finalPercentage, double anchorPercentage, int duration) throws Exception {
    Dimension size = driver.manage().window().getSize();
    int anchor = (int) (size.width * anchorPercentage);
    int startPoint = (int) (size.height * startPercentage);
    int endPoint = (int) (size.height * finalPercentage);

    if (driver instanceof AndroidDriver) {
        new TouchAction(driver).press(anchor, startPoint).waitAction(Duration.ofMillis(duration)).moveTo(anchor, endPoint).release().perform();
    } else if (driver instanceof IOSDriver) {
        new TouchAction(driver).press(anchor, startPoint).waitAction(Duration.ofMillis(duration)).moveTo(0,endPoint-startPoint).release().perform();
    }
}

Then I make sure I’m on notification view (on iOS10 there are two views now). Finally I get the content from first XCUIElementTypeCell visible, because thats were the SMS I just received

You can watch this video : https://youtu.be/DW5VJzdICF8

Hello @Telmo_Cardoso ,

Thank you very much for fast response. But we still failed to catch the message
Here is part of our code

private boolean catchCode(){
  gestSwipeVertical(driver, 0.05, 0.9, 0.5, 1000);
  logger.debug("Page Source:\n" + driver.getPageSource() + "\n\n");
  try{
     WebElement elem =driver.findElement(By.xpath("//*[contains(text(),'Your code')]"));
     if ( elem == null){
              logger.info("Code SMS caught, trying to extract text");
              code = elem.getAttribute("name");
              logger.info("Code SMS " + code);
              //TODO - swipe up to close the panel - gestSwipeVertical(driver, 0.05, 0.9, 0.5, 1000);
              return true;
       } else {
         logger.error("Code SMS not caught ");
         return false;
       }
  } catch (Exception e){
        logger.error("Code SMS not caught ");
        return false;
  }

}

private void gestSwipeVertical(AppiumDriver<WebElement> driver, double startPercentage, double finalPercentage, double anchorPercentage, int duration) throws Exception {
        Dimension size = driver.manage().window().getSize();
        int anchor = (int) (size.width * anchorPercentage);
        int startPoint = (int) (size.height * startPercentage);
        int endPoint = (int) (size.height * finalPercentage);
       
        new TouchAction(driver).press(anchor, startPoint).waitAction(duration).moveTo(0,endPoint-startPoint).release().perform();
 }

I receive “Code SMS not caught” because of NoSuchElementException after line
WebElement elem =driver.findElement(By.xpath("//*[contains(text(),‘Your code’)]"));

On iPhone screen I can see the OTP window reached by the program, so I believe swipe works as expected, but the field with the code still not caught

Page Source printed in debug doesn’t show the OTP (it still shows only source code of my application under test) so I cannot use more specific xPath

Could you, please, suggest a solution?

My opinion: you should create an API to get the OTP code directly from server.

That’s what I did previously for many projects.

Unless the test is really about testing receiving OTP in SMS

Thank you, @chrisjywu, for suggestion.
Unfortunately, we need the code from received SMS since SMS is part of registration via application process and should be received after entering number on previous step

Hi,

you should have waits before finding element or it may be trying to find it before it being available (use FluentWait or WebDriverWait)

What I recommend is in debug mode, place a breakpoint on that line and use an inspector (p.e. https://github.com/mykola-mokhnach/Appium-iOS-Inspector) and see if you can see the SMS. If you can, then its all about implementation.

Thanks,

I actually use WebDriverWait, just here gave the simplified command. I’ll try inspector, hope, it’ll help

Hi,

We tried to use the suggested inspector.
OTP not seen by appium even when only SMS on the screen :frowning:

The same source code I receive when printing in log from my program (source doesn’t contain OTP)

We have no such problem in Android application testing. Sure there should be some way to solve it for IOS application as well

Hope to find the solution with your help :slight_smile:

Hi, did you get any solution for OTP?

No, still no solution :frowning:

Thanks for quick answer.
I have some questions, pls:

  1. Is that happens on all type of devices ?
  2. Do you think that this solution can help: https://appium.readthedocs.io/en/stable/en/advanced-concepts/wda-custom-server/ ?
  3. Maybe there is any capabilities that can help ?

Thank you very much

  1. It happens on iPhone 5 and 6, didn’t check on others. I need the same solution for all types of devices
  2. I’ll check the next week, working on other project currently. Will let you know, when checked

Thank you for suggestion