Android 如何在浓缩咖啡测试中获取edittext的输入类型?
我有一个切换按钮在我的密码编辑文本,使其输入类型或文本或密码取决于什么是检查。我能够执行键入和单击切换按钮,但我不确定哪一个应该是espresso matcher函数中的ViewAssertion 我的代码如下所示Android 如何在浓缩咖啡测试中获取edittext的输入类型?,android,unit-testing,android-espresso,Android,Unit Testing,Android Espresso,我有一个切换按钮在我的密码编辑文本,使其输入类型或文本或密码取决于什么是检查。我能够执行键入和单击切换按钮,但我不确定哪一个应该是espresso matcher函数中的ViewAssertion 我的代码如下所示 @Test public void checkToggleButton(){ String password="anuran123"; onView(withId(R.id.passET)).perform(typeText(password
@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.*/)))
}
只需评估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;
}
只需评估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;
}
通过很少的实验,我发现有一个名为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))));
}
通过很少的实验,我发现有一个名为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))));
}
显示/隐藏密码的正确方法是设置密码,因此在测试中,您可能希望让匹配器检查:
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;
}
};
}
显示/隐藏密码的正确方法是设置密码,因此在测试中,您可能希望让匹配器检查:
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;
}
};
}
有一副没用的样子。我能用文本写作。我想要的是改变它的输入类型。看看这没有帮助。我能用文本写作。我想要的是改变它的输入类型。谢谢你的回答。我会试试看。谢谢你的回答。我将尝试一下。此解决方案不会为您提供有关编辑文本输入类型的线索。我的意思是你不知道密码是否被泄露。这个解决方案不会给你关于编辑文本输入类型的线索。我的意思是你不知道密码是否被泄露了。