如何在uiautomator中检测抬头通知?

时间:2015-02-11 00:53:45

标签: android notifications android-notifications uiautomator heads-up-notifications

我正在使用Nexus 5和Cyanogen One plus设备与Lollipop Android操作系统。我正在尝试测试某些应用的各种通知。我成功地能够使用UiAutomator测试托盘通知和锁定屏幕通知,但我无法通过抬头通知获得任何成功。我尝试了以下代码,但未能检测到它。

    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());
}

有没有办法在UiAutomator中检测抬头通知作为运行自动化时要查找的对象?

2 个答案:

答案 0 :(得分:3)

@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")
        .className("android.view.ViewGroup")
        .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();
}

答案 1 :(得分:0)

通过UiDevice中的swipe方法模拟滑动操作,通过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();
                
        }