如何在uiautomator中检测悬浮通知?

8

我正在使用装有Lollipop Android操作系统的Nexus 5和Cyanogen One plus设备,并与某个应用程序的各种通知进行测试。我成功地使用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());
}

有没有办法在运行自动化时将headsup通知检测为要查找的对象?

1
出于好奇,你是如何测试托盘通知和锁屏通知的?我现在也有同样的需求... - IgorGanapolsky
2个回答

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

3
在API 25上,notification_stack_scroller 被识别为 android.widget.ScrollView 而不是 android.view.ViewGroup,尽管 ScrollView 是从 ViewGroup 派生而来,但这个检查失败了。要修复这个问题,可以只删除 className 选择器,保留 packageNameres id(实际上只有 res id 就足够了)。 - krossovochkin
1
这实际上测试了什么?似乎是在测试Android操作系统中的代码,而不是定制应用程序代码,因为它不会打开由自定义应用程序使用的通知。如何创建针对我的自定义应用程序的通知? - TimCO

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接