java - Android Espresso 无法输入 TYPE_TEXT_VARIATION_NORMAL?

标签 java android android-espresso

我的 Android 应用程序中有一个 TextInputEditText View ,我正在以编程方式(通过 Anko 和 Kotlin)设置它,如下所示:

            textInputEditText {
                id = R.id.text_input_id
                inputType = EditorInfo.TYPE_TEXT_VARIATION_NORMAL
                hint = resources.getText(R.string.text_hint)
            }

然后在 Espresso 测试中我调用这一行:

onView(withId(R.id.text_input_id)).perform(typeText(textToType))

失败并出现此错误:

Caused by: java.lang.RuntimeException: Action will not be performed because the target view does not match one or more of the following constraints:
((is displayed on the screen to the user) and (supports input methods or is assignable from class: class android.widget.SearchView))
Target view: "TextInputEditText{id=2131623954, res-name=text_input_id, visibility=VISIBLE, width=862, height=118, has-focus=false, has-focusable=true, has-window-focus=true, is-clickable=true, is-enabled=true, is-focused=false, is-focusable=true, is-layout-requested=false, is-selected=false, root-is-layout-requested=false, has-input-connection=false, x=0.0, y=0.0, text=, input-type=0, ime-target=false, has-links=false}"

我发现如果我从这里切换它:

/**
 * Default variation of {@link #TYPE_CLASS_TEXT}: plain old normal text.
 */
public static final int TYPE_TEXT_VARIATION_NORMAL = 0x00000000;

对此:

/**
 * Variation of {@link #TYPE_CLASS_TEXT}: entering a password, which should
 * be visible to the user.
 */
public static final int TYPE_TEXT_VARIATION_VISIBLE_PASSWORD = 0x00000090;

它突然神奇地起作用了!但这没有多大意义。普通文本听起来很合适,直到 Espresso 在我面前爆炸。这是 Espresso 的错误,还是我不理解的地方?

最佳答案

我的猜测是这发生在 typeText 调用中。请参阅 TypeTextAction.getConstraints() 中的约束列表。逻辑本质上是:

isDisplayed() &&
hasFocus() &&
( supportsInputMethods() || isAssignableFrom(SearchView.class) )

我的第一个猜测是它没有通过 supportsInputMethods() 约束。您可以在 ViewMatchers.supportsInputMethods() -> matchesSafely() 中放置一个断点并自行查看。

关于java - Android Espresso 无法输入 TYPE_TEXT_VARIATION_NORMAL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42983457/

相关文章:

java - 对于 API >= 26,我应该使用 StartService 还是 StartForegroundService 吗?

android - 如何在 Espresso 中测试 Android 通知

android - Espresso - 无法使用 Espresso 执行按钮点击

Android Studio Espresso 测试错误 : Empty Test Suite

android - 当从 firebase 推送通知时打开特定 Activity

java - 错误 : Parameter index out of range (39 > number of parameters, 即 38)

java - TabLayout 的问题

java - 基本上什么是 RandomAccess?为什么我们使用 RandomAccess?

android - 如何将我的 Activity 发送到后台并恢复?

java - 分配给变量时精度损失