4

我正在與Nexus 5和Cyanogen One加上棒棒糖android操作系統的設備。我正在嘗試測試某些應用的各種通知。我成功地使用UiAutomator測試托盤通知和鎖定屏幕通知,但我無法通過擡頭通知獲得任何成功。我嘗試了下面的代碼,但未能檢測到它。如何檢測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中的HEADUP通知作爲運行自動化時尋找的對象?

+0

出於好奇,你是如何測試托盤通知和鎖屏通知的?我需要現在做同樣的... – 2016-10-25 16:31:38

回答

3
@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") 
     .className("android.view.ViewGroup") 
     .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(); 
} 
+0

在API 25'notification_stack_scroller'被識別爲'android.widget.ScrollView'而不是'android.view.ViewGroup',儘管ScrollView是從ViewGroup派生的,但這個檢查失敗了。爲了解決這個問題,可以刪除'className'選擇器並保留'packageName'和'res id'(實際上res ID也應該足夠了) – krossovochkin 2017-09-18 13:27:50