This site is in read only mode. Please continue to browse, but replying, likes, and other actions are disabled for now.
21 / 30
Nov 2017

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

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

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

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

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:

2 months later
  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

2 months later

Hi,

I hoped after upgrade to Appium 1.7.0 problem will be solved, but still no success

I tried to find solution in the article you provided, but the problem is that on my real device I can catch everything related to application under test. OTP is not caught since it’s a separate application. Did you mean some particular step?

Hi
I install the desctop last version of appium for ios
It is work wonderful and I can get otp code from notification with swipe
there is a problem with search element by xpath, it works very slow

Hi,

Could you, please, provide your code?
As I described, I see that swipe code works, but cannot catch the OTP text that contains the code
How do you do it?

Hi
I can find the msg by XPATH (I have the title of msg)
for example:
MobileBy.XPath("//XCUIElementTypeTextField[contains(@value,‘title’)]");

Sorry for stupid question: after your swipe do you see code of OTP in inspector?
I don’t :frowning:
Can it depend on application under test permissions?

if you dont see the msg in notification
go to your app setting
and set how you can recieve msg in notification

Hi,
Asked development engineer of application under test. He said he have no idea about such a setting.
Can you explain, please, what do you mean?

Thanks in advance

Hi
Do you see the OTP msg on Notification screen after swipe?

19 days later
1 year later

As there are two options to read OTP:

  1. Start the activity of the messaging app and then read the otp from there.
  2. 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 !!!