2014-05-21 23 views
12

我試圖更新EditText作爲咖啡與測試的一部分:更新一個EditText與咖啡

onView(allOf(withClassName(endsWith("EditText")), withText(is("Test")))).perform(clearText()) 
                     .perform(click()) 
                     .perform(typeText("Another test")); 

不過,我收到以下錯誤:

com.google.android.apps.common.testing.ui.espresso.NoMatchingViewException: No views in hierarchy found matching: (with class name: a string ending with "EditText" and with text: is "Test") 

通過分解測試我可以看到在執行clearText()之後會發生這種情況,所以我假定匹配器在每個perform之前重新運行,並且在第二個動作之前失敗。雖然這是有道理的,但是對於如何使用Espresso更新EditText,我還是有點困惑。我應該怎麼做?

請注意,在此場景中,我無法使用資源ID或類似信息,因此必須使用上述組合來識別正確的視圖。

回答

1

你可以嘗試兩件事。首先,我會嘗試使用

onView(withId(<id>).perform... 

這樣,你總是有機會獲得現場的EditText即使其他領域的EditText在屏幕上。

如果這不是一個選項,你可以拆分你的執行調用。

onView(allOf(withClassName(endsWith("EditText")),withText(is("Test")))).perform(clearText()); 
onView(withClassName(endsWith("EditText"))).perform(click()); 
onView(withClassName(endsWith("EditText"))).perform(typeText("Another Test"); 
+0

感謝您的答覆。不幸的是,在活動中有多個EditTexts,編程式生成時沒有ID,所以這些建議都不起作用。 – jgm

+0

我明白了。也許你可以爲你創建的每個EditText設置一個標籤,並根據該標籤找到視圖。該標籤只能用於測試,但它至少會給你一些區別EditText字段的東西。然後再次,這可能會違反你在問題結尾處提到的情況(我在回答之前忽略了那部分內容)。 – Maxwell

8

三件事嘗試:

可以連續運行執行。

onView(...) 
    .perform(clearText(), typeText("Some Text")); 

2.有其標記爲無效(但仍然是非常錯誤)一recorded issue on the Espresso page。解決方法是暫停執行間測試。

public void test01(){ 
    onView(...).perform(clearText(), typeText("Some Text")); 
    pauseTestFor(500); 
    onView(...).perform(clearText(), typeText("Some Text")); 
} 

private void pauseTestFor(long milliseconds) { 
    try { 
     Thread.sleep(milliseconds); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } 
} 

3.你絕對確保你的EditText包含文本, 「測試」?

20

您可以使用replaceText方法。

onView(allOf(withClassName(endsWith("EditText")), withText(is("Test")))).perform(replaceText("Another test")); 
+0

replaceText()函數的工作原理!這救了我。似乎typeText()在輸入鍵盤上存在一些問題。 – herbertD

2

我有一個類似的問題,並使用containsString匹配器和Class.getSimpleName()來解決它。就像這樣:

onView(withClassName(containsString(PDFViewPagerIVZoom.class.getSimpleName()))).check(matches(isDisplayed())); 

你可以看到完整的代碼here