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

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

  • created

    Jan '17
  • last reply

    May '18
  • 5

    replies

  • 778

    views

  • 3

    users

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;
    }
1 year later

What is import com.xxxx.base.Page;

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