【问题标题】:Test Notification bar icons using uiautomator使用 uiautomator 测试通知栏图标
【发布时间】:2013-11-20 15:04:28
【问题描述】:

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

有什么方法可以测试顶部通知栏上新添加的图标吗?我看不到 uiautomatorviewer 的控制

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

【问题讨论】:

  • 我还没有任何解决方案。请让我知道从状态/通知栏获取图标信息的可能方法

标签: android notifications


【解决方案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 依赖

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

【讨论】:

    【解决方案2】:

    对于通知:

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

    【讨论】:

    • 不幸的是在我的情况下不起作用(MI 5s),为 exists() 断言随机失败。一定与时间问题有关,但找不到原因。
    【解决方案3】:

    拉下通知托盘,然后检查状态栏中的项目。然后将它们填充到 uiautomatorviewer 中。

    【讨论】:

    • 这意味着手动与状态栏交互。有点违背自动化测试的目的......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-31
    • 2015-04-11
    • 1970-01-01
    • 1970-01-01
    • 2014-07-15
    • 1970-01-01
    相关资源
    最近更新 更多