使用uiautomator测试通知栏图标

7

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

有没有办法测试最近添加的通知栏顶部的图标?我在uiautomatorviewer中看不到任何控件

或者如果有从NotificationMgr API获取图标信息的方法,请让我知道,这将非常有帮助


我还没有找到解决方案。请告诉我从状态/通知栏获取图标信息的可能方法。 - user3013582
3个回答

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

不幸的是,在我的情况下(MI 5s)无法工作,exists()的断言会随机失败。这一定与时间问题有关,但我找不到原因。 - tomrozb

3
UiDevice.openNotification方法是在API 18中添加的,它可以以编程方式打开通知抽屉。 使用示例
@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 

0

下拉通知栏,然后检查状态栏中的项目。它们将在uiautomatorviewer中显示。


1
这意味着需要手动与状态栏进行交互。这有点违背了自动化测试的目的... - IgorGanapolsky

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