How to detect headsup notification in uiautomator?
Asked Answered
D

2

8

I am working with Nexus 5 and Cyanogen One plus devices with Lollipop android OS. I am trying to test various notifications of certain app. I was successfully able to test tray notification and lock screen notification with UiAutomator but I am not able to have any success with headsup notification. I tried following code but it failed to detect it.

    public void test_HeadsupTitle() throws InterruptedException, UiObjectNotFoundException, IOException
{
    //some code to bring up headsup notification
    UiObject maxHeadsUp = new UiObject(new UiSelector().packageName("com.android.systemui").resourceId("android:id/status_bar_latest_event_content"));
    // code to add sleep so that it waits for heads up notification to show up
    assertTrue(maxHeadsUp.exists());
}

Is there a way to detect headsup notifications in UiAutomator as an object to look for when running automation?

Doctorate answered 11/2, 2015 at 0:53 Comment(1)
Out of curiosity, how did you test tray notification and lockscreen notification? I have a need to do the same now...Flatten
M
7
@Before
public void setUp() throws Exception
{
    super.setUp();
    injectInstrumentation(InstrumentationRegistry.getInstrumentation());
    mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
}


@Test
public void testNoti() throws UiObjectNotFoundException
{
    mDevice.openNotification();
    mDevice.wait(Until.hasObject(By.pkg("com.android.systemui")), 10000);

    /*
     * access Notification Center through resource id, package name, class name.
     * if you want to check resource id, package name or class name of the specific view
     * in the screen, run 'uiautomatorviewer' from command.
     */
    UiSelector notificationStackScroller = new UiSelector()
        .packageName("com.android.systemui")
        .resourceId("com.android.systemui:id/notification_stack_scroller");
    UiObject notificationStackScrollerUiObject = mDevice.findObject(notificationStackScroller);
    assertTrue(notificationStackScrollerUiObject.exists());

    /*
     * access top notification in the center through parent
     */
    UiObject notiSelectorUiObject = notificationStackScrollerUiObject.getChild(new UiSelector().index(0));
    assertTrue(notiSelectorUiObject.exists());

    notiSelectorUiObject.click();
}
Monikamoniker answered 4/11, 2015 at 6:36 Comment(2)
On API 25 notification_stack_scroller is recognized as android.widget.ScrollView not android.view.ViewGroup though ScrollView is derived from ViewGroup, this check fails. To fix one can just remove className selector and leave packageName and res id (actually just res id should be enough as well)Secretary
what does this actually test? Seems to test code in the Android OS, not custom app code, because it doesn't open a notification that is used by a custom app. How to create a notification that is for my custom app?Peroxidize
N
1

Simulate the sliding operation through the swipe method in UiDevice and slide down from the status bar by UiDevice.swipe(startX, startY, endX, endY, steps)

/**
         * Open the notification bar with gestures
         * @throws UiObjectNotFoundException
         */
        public void testViewNotification() throws UiObjectNotFoundException{
                
                device.pressHome();
                
                device.swipe(300, 0, 300, 800, 50);
                device.waitForIdle(2000);
                device.pressBack();
                
        }
Neural answered 25/3, 2021 at 20:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.