E2EHIRING Logo
Jobs
Jobs
courses
Courses
mentorship
Mentorship
more
Moredropdown
E2EHIRING Logo
more
Jobs
Jobs
courses
Courses
mentorship
Mentorship
HomeSepratorIconBlogsSepratorIconAutomate the OTP scenario using Appium in an Android deviceSepratorIcon

Automate the OTP scenario using Appium in an Android device

Han SoloSujatha Jupaka
calendar5 Jul 2022
poster

In this blog, I will be explaining how can we 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.

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

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. I have taken the “clear_notification” locator for the following element.

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

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 otp (change it according to your as varies from app)
  • OTP = extractOTP(OTP); >> To extract the OTP using Regex

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

4) Enter the retrieved OTP in the OTP textbox.


 try {

TouchAction t = new TouchAction(driver);

            waitForVisibility(OTPBoxes);

            t.tap(ElementOption.element(driver.findElement(By.xpath("//android.widget.EditText[1]")))).perform();


            AndroidElement otpBox = (AndroidElement) driver.findElement(By.xpath("//android.widget.EditText[1]"));

            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.xpath("//android.widget.EditText[1]")) – This line will get the OTP textbox element and store it in the “otpBox”

otpBox.sendKeys(otp); – Enter the retrieved OTP to the “otpBox”.


Recent Posts

Rapid Changes in the Coding World: Need for High Skilled Programmers e2eHiring Bridges this Gap with the Mentorship Program April 2023

Rapid Changes in the Coding World: Need for High Skilled Programmers e2eHiring Bridges this Gap with the Mentorship Program April 2023

How to publish your Android app on Google Play Store

How to publish your Android app on Google Play Store

Creating Dynamic User Interfaces with Android Motion Layout

Creating Dynamic User Interfaces with Android Motion Layout

Bean Life Cycle

Bean Life Cycle

Pom.XML

Pom.XML

copycopycopycopy

Han Solo

Recent Posts

Rapid Changes in the Coding World: Need for High Skilled Programmers e2eHiring Bridges this Gap with the Mentorship Program April 2023

Rapid Changes in the Coding World: Need for High Skilled Programmers e2eHiring Bridges this Gap with the Mentorship Program April 2023

How to publish your Android app on Google Play Store

How to publish your Android app on Google Play Store

Creating Dynamic User Interfaces with Android Motion Layout

Creating Dynamic User Interfaces with Android Motion Layout

Bean Life Cycle

Bean Life Cycle

Pom.XML

Pom.XML