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?

2 Answers2

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();
}
Yuliia Ashomok
  • 8,336
  • 2
  • 60
  • 69
leechoohyoung
  • 91
  • 2
  • 8
  • 3
    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) – krossovochkin Sep 18 '17 at 13:27
  • 1
    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? – TimCO Mar 26 '18 at 16:21
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();
                
        }
Yuliia Ashomok
  • 8,336
  • 2
  • 60
  • 69