How to navigate to the Message box in android device and launch the same

Hi All…

I would be need to navigate to the message box to read the OTP message on my android device…
can anyone tell me please how to launch the message box in the device…

Appreciate for the help in advance

Thanks

@willosser @slipy12 @Aleksei @crujzo … any one please suggest any solution on same…

Thank you

valid for android only. Google pageObject.

Notification object:

package com.xxxx.pages.android;

import com.xxxx.base.Page;
import io.appium.java_client.MobileBy;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.pagefactory.AndroidFindAll;
import io.appium.java_client.pagefactory.AndroidFindBy;
import io.appium.java_client.pagefactory.AndroidFindBys;
import org.openqa.selenium.WebDriver;

import java.util.List;

/**

  • Created by Aleksei on 23/02/16.
    */
    public class NativeNotificationPage extends Page {
@AndroidFindBy(id= "com.android.systemui:id/notification_panel")
private List<AndroidElement> notificationPanel;
//settings data
@AndroidFindAll({
        @AndroidFindBy(id = "com.android.systemui:id/clear_all_button"),
        @AndroidFindBy(id = "com.android.systemui:id/dismiss_text")
})
private List<AndroidElement> clearAllBtn;
//last items
@AndroidFindBy(id = "com.android.systemui:id/latestItems")
private List<AndroidElement> lastItemsContainer;
//events data
@AndroidFindBy(id = "android:id/status_bar_latest_event_content")
private List<AndroidElement> lastItemsContent;
@AndroidFindBy(id = "android:id/title")
private List<AndroidElement> itemTitle;
String itemTitle_Locator_Text = "android:id/title";
@AndroidFindBys({
        @AndroidFindBy (id = "android:id/big_text"),
        @AndroidFindBy (id = "android:id/text")
})
private List<AndroidElement> itemText;
String itemText_Phone_Locator_Text = "android:id/text";
String itemText_Tablet_Locator_Text = "android:id/big_text";
@AndroidFindBy(id = "android:id/time")
private List<AndroidElement> itemTime;
public NativeNotificationPage(WebDriver driver) {
    super(driver);
}
public boolean isNativeNotificationPage() {
    System.out.println("  check 'Notification' Screen loaded");
    boolean bool;
    setFastLookTiming();
    bool = !notificationPanel.isEmpty();
    setDefaultTiming();
    return bool;
}
public boolean isClearAllBtnLoaded() {
    System.out.println("  check 'Clear' button loaded");
    boolean bool;
    setLookTiming(3);
    bool = !clearAllBtn.isEmpty();
    setDefaultTiming();
    return bool;
}
public int getLastItemsContentSize() {return lastItemsContent.size();}
public String getItemTitle(int num) {
    try {
        return lastItemsContent.get(num).findElements(MobileBy.className("android.widget.TextView")).get(0).getText();
    } catch (Exception e) {
        return null;
    }
}
public String getItemText(int num) {
    //System.out.println(lastItemsContent.get(num).findElements(MobileBy.className("android.widget.TextView")).size());
    if (isPhone()) {
        List<MobileElement> item = lastItemsContent.get(num).findElements(MobileBy.className("android.widget.TextView"));
        String tmp = null;
        for (int i=1;i<item.size();i++) {
            if (tmp == null)
                tmp = item.get(i).getText();
            else
                tmp = tmp + "," +item.get(i).getText();
        }
        return tmp;
    } else {
        setLookTiming(3);
        if (lastItemsContent.get(num).findElements(MobileBy.id(itemText_Tablet_Locator_Text)).isEmpty()) {
            setDefaultTiming();
            return lastItemsContent.get(num).findElement(MobileBy.id(itemText_Phone_Locator_Text)).getText();
        } else {
            setDefaultTiming();
            return lastItemsContent.get(num).findElement(MobileBy.id(itemText_Tablet_Locator_Text)).getText();
        }
    }
}
public boolean tapClearAllBtn() {
    System.out.println("  tap 'Clear' button");
    try {
        return tapElement(clearAllBtn.get(0));
    } catch (Exception e) {
        return false;
    }
}
public boolean tapNotificationByNum(int num) {
    try {
        return tapElement(lastItemsContent.get(num));
    } catch (Exception e) {
        return false;
    }
}

}

clear all notification before:

public void clearNotifications_Android() {
    System.out.println("  clearNotifications_Android()");
    //clear all notifications
    ((AndroidDriver) driver).openNotifications();
    nativeNotificationPage = PageFactory.initElements(driver, NativeNotificationPage.class);
    assertTrue("Native notification page is NOT loaded", nativeNotificationPage.isNativeNotificationPage());
    if (nativeNotificationPage.isClearAllBtnLoaded()) {
        nativeNotificationPage.tapClearAllBtn();
    } else {
        tapBackKey((AndroidDriver)driver);
        sleep(1);
    }
    System.out.println("  notifications cleared");
}

get 6 digit SMS verification code from notification:

public String getSMSPhoneVerificationCodeByText_Android(String txt) {
        ((AndroidDriver) driver).openNotifications();
        sleep(1);
        nativeNotificationPage = PageFactory.initElements(driver, NativeNotificationPage.class);
        assertTrue("Native notification page is NOT loaded", nativeNotificationPage.isNativeNotificationPage());

        String verificationCode = null;
        int itemsListSize = nativeNotificationPage.getLastItemsContentSize();
        System.out.println("  number of notifications is: " + itemsListSize);
        assertTrue("Number of notifications is 0", itemsListSize != 0);
        String title, text;

        for (int i = 0; i < itemsListSize; i++) {
            title = nativeNotificationPage.getItemTitle(i);
            text = nativeNotificationPage.getItemText(i);
            System.out.println("   notification title is: " + title);
            System.out.println("   notification text is: " + text);
            if (text.contains(txt)) {
                //Integer.parseInt(text.substring(0,6).replaceAll("[\\D]", ""));
                //remove all non number chars
                List<String> textArrayIn = Arrays.asList(text.split(","));
                for (String textItem : textArrayIn) {
                    try {
                        System.out.println("textItem = "+textItem);
                        String tmp = textItem.replaceAll("[^0-9]+", "");
                        System.out.println("tmp_1 = "+tmp);
                        tmp = tmp.substring(tmp.length() - 6, tmp.length());
                        System.out.println("tmp_2 = "+tmp);
                        if (verificationCode == null)
                            verificationCode = tmp;
                        else
                            verificationCode = verificationCode + "," + tmp;
                    } catch (Exception e) {
                        //ignore.
                    }
                }
                System.out.println("   verification code is: " + verificationCode);
            }
        }

        //close notification
        if (verificationCode!=null && nativeNotificationPage.isClearAllBtnLoaded()) {
            nativeNotificationPage.tapClearAllBtn();
        } else {
            tapBackKey((AndroidDriver) driver);
        }
        sleep(1);

        return verificationCode;
    }

What is import com.xxxx.base.Page;

@dduphorn mine Page class which i extend with AndroidPage or iOSPage

Thanks, I figured it out. I don’t use page class, but I will look into it.

I was able to get the notification text using parts of your code. - android:id/big_text

What is isPhone() Method?

just same custom function to check are we with tablet or phone… already do not remember the implementation

@Aleksei could you please help me with page class for android I am not able to find it.

Could you pls help with this page class

BasePage in my code contains simple things like:

    final public static int DEFAULT_WAIT_TIME = 20; // default look for elements

    public void setDefaultTiming() {
        PageFactory.initElements(new AppiumFieldDecorator(driver, Duration.ofSeconds(DEFAULT_WAIT_TIME)), this);
    }

    public void setLookTiming(int sec) {
        PageFactory.initElements(new AppiumFieldDecorator(driver, Duration.ofSeconds(sec)), this);
    }

    // plus any implementation of used commands: tap, getText, setValue, hideKeyboard and so on
    // as far as implementation  changes quite often (specially touch/tap) plus there are several way how to do it you can use any latest that you are comfortable with

    protected <T> boolean clearInput(T el) {
        try {
            ((WebElement) el).clear();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    protected <T> boolean setValue(T el, String text) {
        Logger.log("text '" + text + "'");
        boolean result = false;
        try {
            ((WebElement) el).sendKeys(text);
            result = true;
        } catch (Exception ex) {
            Logger.logError(ex.getMessage());
        }
        return result;
    }

    public <T> String getText(T el) {
        try {
            return ((WebElement) el).getText();
        } catch (Exception e) {
            return null;
        }
    }

   // .....

PS minor note: code that 7 years old may by too old for today and possibly needs update.