我有一个测试用例,它将检查用户不应该在文本字段中有特殊字符。例如: - 如果我在名字中输入user1并单击提交按钮,它会给我一个错误消息我不应该有文本字段中的特殊字符,我应该如何在浓缩咖啡中自动化。
此错误消息我想自动化
@LargeTest
@RunWith(AndroidJUnit4.class)
public class T2W_0014 {
@Rule
public ActivityTestRule<SplashScreenActivity> mActivityTestRule = new ActivityTestRule<>(SplashScreenActivity.class);
@Test
public void t2W_0014() {
ViewInteraction customButton2 = onView(
allOf(withId(R.id.btn_signup), withText("Sign up"), isDisplayed()));
customButton2.perform(click());
ViewInteraction customEditText = onView(
allOf(withId(R.id.first_name), isDisplayed()));
customEditText.perform(click());
ViewInteraction customEditText2 = onView(
allOf(withId(R.id.first_name), isDisplayed()));
customEditText2.perform(replaceText("abhisek1"), closeSoftKeyboard());
ViewInteraction customEditText3 = onView(
allOf(withId(R.id.last_name), isDisplayed()));
customEditText3.perform(replaceText("numeric2"), closeSoftKeyboard());
ViewInteraction customButton3 = onView(
allOf(withId(R.id.btn_signup), withText("Sign up"), isDisplayed()));
customButton3.perform(click());
}
}
答案 0 :(得分:0)
使用此方法检查可用的特殊字符!
public boolean isAlpha(String name) {
return name.matches("[a-zA-Z]+");
}
如果名称中有任何特殊字符,将返回false!
答案 1 :(得分:0)
您可以使用Espresso验证EditText
是否显示正确的错误消息,请使用hasErrorText()
:
customEditText.check(matches(hasErrorText("Name should only contain characters")));
另请参阅Testing EditText errors with Espresso on Android和Android Espresso. How to check ErrorText in TextInputLayout。
我强烈建议您为布局中的每个EditText
编写一个测试方法。这样您就可以单独验证每个工作正常。