android - 如何检测 uiautomator 中的抬头通知?

标签 android notifications android-notifications android-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 中的提醒通知作为运行自动化时要查找的对象?

最佳答案

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

关于android - 如何检测 uiautomator 中的抬头通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28444765/

相关文章:

java - 无法给变量赋值

android - 单击文本必须打开一个对话框

android - Android 10 上的自定义通知中没有展开按钮

android - 从其他应用程序的通知栏中删除通知

android - Retrofit 在错误回调中添加自定义对象。失败时

laravel - 调用未定义的方法 Illuminate\Database\Eloquent\Relations\BelongsToMany::routeNotificationFor()

html - 简单的通知电子邮件模板

java - 如何向超过 2 天未进入应用程序的用户发送推送通知?

android - 在android中点击通知进入我的应用

android - 我想播放音频。我使用 Thread、AsyncTask 还是 Service?