使用uiautomator测试通知栏图标

时间:2013-11-20 15:04:28

标签: android notifications

我是UI Automator的新手。我有一个作为服务运行的应用程序。它在通知栏上显示一个图标

有没有办法在顶部通知栏上测试这个新添加的图标?我看不到与uiautomatorviewer

的对比

或者如果有任何方法可以从NotificationMgr API获取图标信息,请告诉我这将有所帮助

3 个答案:

答案 0 :(得分:3)

对于通知:

    private UiObject getNotificationStackScroller()
{
    /*
     * 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());

    return notificationStackScrollerUiObject;
}

并通过以下方式访问任何“第i个”项目:

UiObject notificationUiObject = getNotificationStackScroller().getChild(new UiSelector().index(i));
        assertTrue(notificationUiObject.exists());

例如检查通知数是否为“3”退出:

            UiObject numberOfNotifications = notificationUiObject.getChild(new UiSelector().text("3"));
        assertTrue(numberOfNotifications.exists());

答案 1 :(得分:1)

API级别18中添加的UiDevice.openNotification method将以编程方式打开通知抽屉。

使用示例

@RunWith(AndroidJUnit4.class) 
public class ExampleInstrumentedTest {

    @Test
    public void useAppContext() throws Exception {

        Context appContext = InstrumentationRegistry.getTargetContext();
        assertEquals("com.mydomain.myuiautomatortests", appContext.getPackageName());

        appContext.startActivity(new Intent(appContext, MainActivity.class));

        UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
        mDevice.openNotification();
    }
}

Gradle Dependency

compile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'  // or higher 

答案 2 :(得分:0)

关闭通知托盘,然后检查状态栏中的项目。然后将在uiautomatorviewer中填充它们。