2014-05-21 24 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