【问题标题】:How to detect headsup notification in uiautomator?如何在 uiautomator 中检测提示通知?
【发布时间】:2015-04-11 06:11:49
【问题描述】:

我正在使用带有 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());
}

有没有办法将 UiAutomator 中的提示通知检测为运行自动化时要查找的对象?

【问题讨论】:

  • 出于好奇,您是如何测试托盘通知和锁屏通知的?我现在也需要这样做……

标签: android notifications android-notifications android-uiautomator heads-up-notifications


【解决方案1】:
@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();
}

【讨论】:

  • 在 API 25 notification_stack_scroller 被识别为 android.widget.ScrollView 而不是 android.view.ViewGroup 虽然 ScrollView 派生自 ViewGroup,但此检查失败。要修复一个可以删除className选择器并留下packageNameres id(实际上只要res id就足够了)
  • 这实际上测试了什么?似乎是在 Android 操作系统中测试代码,而不是自定义应用程序代码,因为它不会打开自定义应用程序使用的通知。如何为我的自定义应用创建通知?
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多