【问题标题】:How to get input type of edittext in Espresso Testing?如何在 Espresso 测试中获取编辑文本的输入类型?
【发布时间】:2018-01-23 06:10:38
【问题描述】:

我的密码editText 中有一个切换button,它可以根据检查的内容输入文本或密码。我能够执行输入并单击切换按钮,但我不确定哪个应该是浓缩咖啡匹配器功能中的 ViewAssertion。

我的代码如下所示。

@Test
    public void checkToggleButton(){
        String password="anuran123";

        onView(withId(R.id.passET)).perform(typeText(password), closeSoftKeyboard());

        onView(withId(R.id.visibilityToggle)).perform(click());

        onView(withId(R.id.passET)).check(matches(allOf(/*NOT SURE WHAT TO DO HERE AS isDiplayed() won't work here.*/)))
    }

【问题讨论】:

标签: android unit-testing android-espresso


【解决方案1】:

显示/隐藏密码的正确方法是设置TransformationMethod,因此在您的测试中,您可能希望有一个匹配器检查:

private Matcher<View> isPasswordHidden() {
    return new BoundedMatcher<View, EditText>(EditText.class) {
        @Override
        public void describeTo(Description description) {
            description.appendText("Password is hidden");
        }

        @Override
        public boolean matchesSafely(EditText editText) {
            //returns true if password is hidden
            return editText.getTransformationMethod() instanceof PasswordTransformationMethod;
        }
    };
}

并像这样使用它:

@Test
public void testIsPasswordHidden() throws InterruptedException {
    onView(withId(R.id.password)).perform(typeText("password"));
    onView(withId(R.id.password)).check(matches(isPasswordHidden()));
}

【讨论】:

    【解决方案2】:

    通过很少的实验,我发现有一个名为 withInputType 的 ViewMactcher 可以完成这项工作。所以最终代码如下所示

    @Test
        public void checkToggleButton(){
            String password="anuran123";
    
            onView(withId(R.id.passET)).perform(typeText(password), closeSoftKeyboard());
    
            onView(withId(R.id.visibilityToggle)).perform(click());
    
            onView(withId(R.id.passET)).check(matches(allOf(withInputType(InputType.TYPE_CLASS_TEXT))));
        }
    

    【讨论】:

    • 此解决方案不会为您提供有关编辑文本输入类型的线索。我的意思是你不知道密码是否被泄露。
    【解决方案3】:

    只需评估 getInputType() 等于您想要的类型。

     /**
         * Get the type of the editable content.
         *
         * @see #setInputType(int)
         * @see android.text.InputType
         */
        public int getInputType() {
            return mEditor == null ? EditorInfo.TYPE_NULL : mEditor.mInputType;
        }
    

    【讨论】:

    • 感谢您的回答。我会试一试的。
    【解决方案4】:

    以下匹配密码字段:

            onView(withId(R.id.passET))
                    .check(
                            matches(
                                    withInputType(
                                            InputType.TYPE_TEXT_VARIATION_PASSWORD
                                            | InputType.TYPE_CLASS_TEXT
                                    )
                            )
                    );
    

    对于普通的文本字段,您可以使用:

            onView(withId(R.id.passET))
                    .check(
                            matches(
                                    withInputType(
                                            InputType.TYPE_CLASS_TEXT
                                    )
                            )
                    );
    

    获取所有应用类型的一种简单方法是使用 Android Studio 调试器中的 Evaluate Expression 功能来获取:passET.getInputType()。然后在https://developer.android.com/reference/android/text/InputType中使用适当的常量组合

    【讨论】:

      猜你喜欢
      • 2019-02-13
      • 2010-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-01
      • 1970-01-01
      • 2015-01-28
      相关资源
      最近更新 更多