Android 使用 Espresso 更新 EditText
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23780857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Updating an EditText with Espresso
提问by jgm
I'm attempting to update an EditText
as part of an Espresso test with:
我正在尝试使用以下内容更新EditText
作为 Espresso 测试的一部分:
onView(allOf(withClassName(endsWith("EditText")), withText(is("Test")))).perform(clearText())
.perform(click())
.perform(typeText("Another test"));
However I receive the following error:
但是我收到以下错误:
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")
By breaking down the test line I can see that this occurs after performing clearText()
, so I assume that the matchers are being re-run prior to each perform
and fail the prior to the second action. Although this makes sense, it leaves me somewhat confused as to how to update the EditText
using Espresso. How should I do this?
通过分解测试线,我可以看到这是在执行之后发生的clearText()
,所以我假设匹配器在每个之前重新运行perform
并且在第二个操作之前失败。虽然这是有道理的,但它让我对如何更新EditText
using Espresso感到有些困惑。我该怎么做?
Note that I cannot use a resource ID or similar in this scenario and have to use the combination as shown above to identify the correct view.
请注意,在这种情况下我不能使用资源 ID 或类似的资源,必须使用如上所示的组合来识别正确的视图。
回答by Abhinav Manchanda
You can use the replaceText
method.
您可以使用该replaceText
方法。
onView(allOf(withClassName(endsWith("EditText")), withText(is("Test"))))
.perform(replaceText("Another test"));
回答by James Davis
Three things to try:
尝试三件事:
1.You can run performs in succession.
1.可以连续运行执行。
onView(...)
.perform(clearText(), typeText("Some Text"));
2.There is a recorded issue on the Espresso pagewhich was marked as invalid (but is still very much a bug). A workaround for this is to pause the test in-between performs.
2.在 Espresso 页面上有一个记录的问题被标记为无效(但仍然是一个非常严重的错误)。一种解决方法是在执行之间暂停测试。
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.Are you absolutely sure that your EditText contains the text, "Test"?
3.您确定您的 EditText 包含文本“Test”吗?
回答by voghDev
I was having a similar issue and solved it using the containsString matcher and Class.getSimpleName(). Like this:
我遇到了类似的问题并使用 containsString 匹配器和 Class.getSimpleName() 解决了它。像这样:
onView(withClassName(containsString(PDFViewPagerIVZoom.class.getSimpleName()))).check(matches(isDisplayed()));
You can see the full code here
你可以在这里看到完整的代码
回答by Kharis Azhar
To set value in EditText with Espresso simple like this
使用 Espresso 在 EditText 中设置值,就像这样简单
onView(withId(R.id.yourIdEditText)).perform(typeText("Your Text"))
onView(withId(R.id.yourIdEditText)).perform(typeText("Your Text"))
回答by Maxwell
You could try two things. First I would try to use
你可以尝试两件事。首先我会尝试使用
onView(withId(<id>).perform...
This way you would always have access to the EditText field even when other EditText fields are on the screen.
这样,即使屏幕上有其他 EditText 字段,您也始终可以访问 EditText 字段。
If that's not an option, you could split up your perform calls.
如果这不是一个选项,您可以拆分您的执行调用。
onView(allOf(withClassName(endsWith("EditText")),withText(is("Test")))).perform(clearText());
onView(withClassName(endsWith("EditText"))).perform(click());
onView(withClassName(endsWith("EditText"))).perform(typeText("Another Test");