test用于检查用户不应在文本字段中包含特殊字符的情况

时间:2016-12-09 05:47:41

标签: android automated-tests android-espresso

我有一个测试用例,它将检查用户不应该在文本字段中有特殊字符。例如: - 如果我在名字中输入user1并单击提交按钮,它会给我一个错误消息我不应该有文本字段中的特殊字符,我应该如何在浓缩咖啡中自动化。

此错误消息我想自动化

enter image description here

@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());
    }
}

2 个答案:

答案 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 AndroidAndroid Espresso. How to check ErrorText in TextInputLayout

我强烈建议您为布局中的每个EditText编写一个测试方法。这样您就可以单独验证每个工作正常。