How to automate OTP Scenario using Appium - Codoid
Select Page
Automation Testing

How to automate OTP Scenario using Appium

In this blog, we will be explaining how you can automate the OTP scenario using Appium in 4 simple steps.

How to automate OTP Scenario using Appium

If you are wondering why we need to automate the OTP scenario using Appium when we can simply have the developer make the OTP static and proceed with our script. There is a big catch in such a method because the above solution will work only for the STAGE or DEV environment. But when it comes to a production environment, we can’t just run the same static OTP automation script as it’s simply not practical. Being a leading Test Automation Company we always believe in successfully implementing automation to make our process efficient. So in this blog, we will be explaining how you can automate the OTP scenario using Appium in 4 simple steps.

Ways to automate OTP scenario in Appium

There are two best ways to automate the OTP scenario using Appium in an Android device;

  • We can either make use of our notification bar to retrieve the OTP.
  • Or, we can open up our ‘messages’ app and retrieve the OTP.

In this blog, we will be focusing on how to retrieve the OTP from the notification bar, as it is a less complex approach that keeps the focus of testing on the intended app. Let’s start with the basic pre-requisites you will be needing to get the job done.

Pre-Requisite

  • Install the application you work with (Example., Amazon)
  • Connect your mobile (Android) to the PC either through cable or Wi-Fi.
  • Install Appium on your PC.

Steps to retrieve the OTP

We have mentioned the code that we have used for this example below. We have also broken down the main parts of the code and explained them one by one so that you will be able to get a crystal clear understanding of the process. Make sure you don’t skip the specific notes that we have mentioned below the explanations as well. Now let’s get started with the very first step.

1) Open the notification panel and clear the previous notifications.

driver.openNotifications();

try {
   AndroidElement notification = driver.findElementById("com.android.systemui:id/clear_notifications");
   
   if (notification.isDisplayed()) {
      notification.click();
      return new EnterPhoneNumber(driver);
   } 

} catch (Exception e) {
   System.out.println(e);
   driver.pressKey(new KeyEvent(AndroidKey.BACK));
}

Code Explanation

  • driver.openNotifications() – Opens Android notifications (Emulator only)
  • AndroidElement notification = driver.findElementById(“com.android.systemui:id/clear_notifications”) – This line will find and store the element in “notification”.
  • if (notification.isDisplayed()) – checks whether the “clear_notification” element is present.
  • notification.click() – if present, clear the notification by clicking on it.
  • driver.pressKey(new KeyEvent(AndroidKey.BACK)) – if no notification is present, then press back to the application.

Note: Kindly change the locator according to your mobile element. We have taken the “clear_notification” locator for the following element.

How to automate OTP in Appium

2) Write the code to the point where you are asked to enter the mobile number.

how to enter otp in appium

3) Once you have entered the mobile number, open the notification panel and wait for the OTP to appear and retrieve it.

String OTP = new String();
   
   try {

   driver.openNotifications();

   Thread.sleep(3000);

   List<AndroidElement> messageText =     driver.findElementsById("android:id/message_text");
   int Size = messageText.size();
   System.out.println("Size =" + Size);

   for(int i=0; i<=3; i++) {
      
      Thread.sleep(2000);
      if(OTP.length()==0) {
         OTP = OTPloop(Size, messageText);
      }else {
         System.out.println("OTP Found");
         break;
      }
   }  

   if(OTP.length()<6) {
      System.out.println("---- Failed to retrieve OTP ----");
      driver.pressKey(new KeyEvent(AndroidKey.BACK));
      return "";
   }else {
      OTP = extractOTP(OTP);
   }
   
   if(OTP.length()==0) {
      Assert.fail("OTP not received");
   }else {

      System.out.println("OTP is: " +  OTP);
   }
   
   driver.pressKey(new KeyEvent(AndroidKey.BACK));
   
   } catch (Exception e) {
      e.printStackTrace();
      return "";
   }
   return OTP;
}

private String OTPloop(int size, List<AndroidElement> element) {
   System.out.println("Inside OTP Loop method");
   for (int i = 0; i < size; i++) {
      System.out.println("Current position = " + i);
      if (element.get(i).getText().contains("OTP: ")) {
         return element.get(i).getText();
      }
   }
   return "";
}
private String extractOTP(String OTP) {
   
   Pattern p = Pattern.compile("\\d+");
   Matcher m = p.matcher(OTP);
   
   while(m.find()) {

      System.out.println(m.group().length());
      System.out.println(m.group());

      if(m.group().length()==6) {
         System.out.println("The OTP is: " + m.group());
         return m.group();
      }
   }return "";
}

Code Explanation

  • List messageText = driver.findElementsById(“android:id/message_text”) – This code will get the OTP message from the notification bar
  • OTP = OTPloop(Size, messageText) – It is a method, which will search for the given text “OTP: ” in that “messageText”. If found, it will retrieve the text from that element.
  • if(OTP.length()<6) - Used for verifying the size of the OTP (Change it according to your OTP as it varies from app to app)
  • OTP = extractOTP(OTP); >> To extract the OTP using Regex

Note: Kindly change the locator according to your mobile element. We have taken the “messageText” locator for the following element.

how to test otp verification

4) Enter the retrieved OTP in the OTP textbox.

try {
   AndroidElement otpBox = driver.findElement(By.id("in.amazon.mShop.android.shopping:id/otp_edit_text"));
   if (otpBox.isDisplayed()) {
      System.out.println("--------- Entering OTP ---------");
      if(otp != null) {
         otpBox.sendKeys(otp);
      }
   }

} catch (NoSuchElementException e) {
   System.out.println("OTP textbox not displayed");
}

Code Explanation

  • AndroidElement otpBox = driver.findElement(By.id(“in.amazon.mShop.android.shopping:id/otp_edit_text”)) – This line will get the OTP textbox element and store it in the “otpBox”
  • otpBox.sendKeys(otp); – Enter the retrieved OTP to the “otpBox”.

Conclusion

So those are the 4 simple steps that you can use to successfully automate the OTP scenario using Appium. Thank you for your time, we hope this blog has been informative and that you have enjoyed reading it. Automating the OTP scenario using Appium is a must-know technique, and it has even been useful for us in providing the best Test Automation Services. We will constantly be updating such useful and technically sound blog articles on our website. So make sure that you subscribe to our blog so that you don’t miss out on any of it. If you have any doubts about the above-discussed methods, or if you have any inputs that could make the process more efficient, kindly head over to the comments section and let us know.

Written By

Submit a Comment

Your email address will not be published. Required fields are marked *


If you are wondering why we need to automate the OTP scenario using Appium when we can simply have the developer make the OTP static and proceed with our script. There is a big catch in such a method because the above solution will work only for the STAGE or DEV environment. But when it comes to a production environment, we can’t just run the same static OTP automation script as it’s simply not practical. Being a leading Test Automation Company we always believe in successfully implementing automation to make our process efficient. So in this blog, we will be explaining how you can automate the OTP scenario using Appium in 4 simple steps.

Ways to automate OTP scenario in Appium

There are two best ways to automate the OTP scenario using Appium in an Android device;

  • We can either make use of our notification bar to retrieve the OTP.
  • Or, we can open up our ‘messages’ app and retrieve the OTP.

In this blog, we will be focusing on how to retrieve the OTP from the notification bar, as it is a less complex approach that keeps the focus of testing on the intended app. Let’s start with the basic pre-requisites you will be needing to get the job done.

Pre-Requisite

  • Install the application you work with (Example., Amazon)
  • Connect your mobile (Android) to the PC either through cable or Wi-Fi.
  • Install Appium on your PC.

Steps to retrieve the OTP

We have mentioned the code that we have used for this example below. We have also broken down the main parts of the code and explained them one by one so that you will be able to get a crystal clear understanding of the process. Make sure you don’t skip the specific notes that we have mentioned below the explanations as well. Now let’s get started with the very first step.

1) Open the notification panel and clear the previous notifications.

driver.openNotifications();

try {
   AndroidElement notification = driver.findElementById("com.android.systemui:id/clear_notifications");
   
   if (notification.isDisplayed()) {
      notification.click();
      return new EnterPhoneNumber(driver);
   } 

} catch (Exception e) {
   System.out.println(e);
   driver.pressKey(new KeyEvent(AndroidKey.BACK));
}

Code Explanation

  • driver.openNotifications() – Opens Android notifications (Emulator only)
  • AndroidElement notification = driver.findElementById(“com.android.systemui:id/clear_notifications”) – This line will find and store the element in “notification”.
  • if (notification.isDisplayed()) – checks whether the “clear_notification” element is present.
  • notification.click() – if present, clear the notification by clicking on it.
  • driver.pressKey(new KeyEvent(AndroidKey.BACK)) – if no notification is present, then press back to the application.

Note: Kindly change the locator according to your mobile element. We have taken the “clear_notification” locator for the following element.

How to automate OTP in Appium

2) Write the code to the point where you are asked to enter the mobile number.

how to enter otp in appium

3) Once you have entered the mobile number, open the notification panel and wait for the OTP to appear and retrieve it.

String OTP = new String();
   
   try {

   driver.openNotifications();

   Thread.sleep(3000);

   List<AndroidElement> messageText =     driver.findElementsById("android:id/message_text");
   int Size = messageText.size();
   System.out.println("Size =" + Size);

   for(int i=0; i<=3; i++) {
      
      Thread.sleep(2000);
      if(OTP.length()==0) {
         OTP = OTPloop(Size, messageText);
      }else {
         System.out.println("OTP Found");
         break;
      }
   }  

   if(OTP.length()<6) {
      System.out.println("---- Failed to retrieve OTP ----");
      driver.pressKey(new KeyEvent(AndroidKey.BACK));
      return "";
   }else {
      OTP = extractOTP(OTP);
   }
   
   if(OTP.length()==0) {
      Assert.fail("OTP not received");
   }else {

      System.out.println("OTP is: " +  OTP);
   }
   
   driver.pressKey(new KeyEvent(AndroidKey.BACK));
   
   } catch (Exception e) {
      e.printStackTrace();
      return "";
   }
   return OTP;
}

private String OTPloop(int size, List<AndroidElement> element) {
   System.out.println("Inside OTP Loop method");
   for (int i = 0; i < size; i++) {
      System.out.println("Current position = " + i);
      if (element.get(i).getText().contains("OTP: ")) {
         return element.get(i).getText();
      }
   }
   return "";
}
private String extractOTP(String OTP) {
   
   Pattern p = Pattern.compile("\\d+");
   Matcher m = p.matcher(OTP);
   
   while(m.find()) {

      System.out.println(m.group().length());
      System.out.println(m.group());

      if(m.group().length()==6) {
         System.out.println("The OTP is: " + m.group());
         return m.group();
      }
   }return "";
}

Code Explanation

  • List messageText = driver.findElementsById(“android:id/message_text”) – This code will get the OTP message from the notification bar
  • OTP = OTPloop(Size, messageText) – It is a method, which will search for the given text “OTP: ” in that “messageText”. If found, it will retrieve the text from that element.
  • if(OTP.length()<6) - Used for verifying the size of the OTP (Change it according to your OTP as it varies from app to app)
  • OTP = extractOTP(OTP); >> To extract the OTP using Regex

Note: Kindly change the locator according to your mobile element. We have taken the “messageText” locator for the following element.

how to test otp verification

4) Enter the retrieved OTP in the OTP textbox.

try {
   AndroidElement otpBox = driver.findElement(By.id("in.amazon.mShop.android.shopping:id/otp_edit_text"));
   if (otpBox.isDisplayed()) {
      System.out.println("--------- Entering OTP ---------");
      if(otp != null) {
         otpBox.sendKeys(otp);
      }
   }

} catch (NoSuchElementException e) {
   System.out.println("OTP textbox not displayed");
}

Code Explanation

  • AndroidElement otpBox = driver.findElement(By.id(“in.amazon.mShop.android.shopping:id/otp_edit_text”)) – This line will get the OTP textbox element and store it in the “otpBox”
  • otpBox.sendKeys(otp); – Enter the retrieved OTP to the “otpBox”.

Conclusion

So those are the 4 simple steps that you can use to successfully automate the OTP scenario using Appium. Thank you for your time, we hope this blog has been informative and that you have enjoyed reading it. Automating the OTP scenario using Appium is a must-know technique, and it has even been useful for us in providing the best Test Automation Services. We will constantly be updating such useful and technically sound blog articles on our website. So make sure that you subscribe to our blog so that you don’t miss out on any of it. If you have any doubts about the above-discussed methods, or if you have any inputs that could make the process more efficient, kindly head over to the comments section and let us know.