簡體   English   中英

使用Espresso對Google Maps進行單元測試

[英]Using Espresso to Unit Test Google Maps

我正在使用Espresso在我的應用程序上進行一些UI測試。 我有一個帶有地圖的片段,並且在其中顯示了一些我通過后端調用獲得的項目。

當我點擊一個標記時,我正在做一些UI事情

我可以用espresso在地圖上進行單元測試嗎?

簡短的回答:真正不可能用濃縮咖啡。 一種解決方案可能是使用UIAutomator: https : //developer.android.com/tools/testing-support-library/index.html#UIAutomator https://developer.android.com/training/testing/ui-testing/uiautomator -testing.html

因此,您需要:

1)添加gradle依賴項:

dependencies {
androidTestCompile 'com.android.support.test:runner:0.2'
androidTestCompile 'com.android.support.test:rules:0.2'
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1' }

2)即使您沒有使用標題,也請確保至少在標題中添加標題。

3)編寫測試,代碼如下:

UiDevice device = UiDevice.getInstance(getInstrumentation());
UiObject marker = device.findObject(new UiSelector().descriptionContains("marker title"));
marker.click();

說明:

GoogleMap生成UI並使其可訪問,即,地圖內容可以看作是可訪問性節點信息的樹。

這是樹,是虛擬視圖樹,它不代表真實視圖樹。 我們稍后再來

默認情況下,地圖的contentDescription為“ Google Map”,而標記的contentDescription為“ {markerTitle}。{markerSnippet}”。

然后的問題是,為什么不使用濃縮咖啡:

onView(withContentDescription("marker title. ")).perform(click());

因為找不到它,所以:

onView(withContentDescription("Google Map")).perform(click());

會很好。

那么UIAutomator如何工作而Espresso沒有呢?

因為它們使用不同的視圖樹。

UIAutomator使用AccessibilityService提供的可訪問性節點信息樹,而Espresso使用視圖層次結構,從而處理任何ViewGroup的所有子級。 可訪問性節點信息和視圖層次結構可能一對一映射,也可能不一對一映射。 在這種情況下

onView(withContentDescription(“ Google Map”))

查找的不是ViewGroup而是TextureView,后者不知道有子級,因此Espresso無法知道其中繪制了什么。

瞧! :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM