
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;
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
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
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
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”.