android - 使用 Espresso 测试 TextView 值不为空,如果 TextView 值为空则失败

标签 android android-espresso

我是 android 测试的新手。如果用户单击按钮并且 TextView 为空,我试图让测试失败。有没有办法在 Espresso 中执行此操作,因此如果 TextView 为空,它将失败。我不想检查 TextView 是否包含特定文本,我想检查它是否不为空。

最佳答案

试试这个:

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.not;

@RunWith(AndroidJUnit4.class)
public class MainActivityInstrumentedTest {

    @Rule
    public ActivityTestRule<MainActivity> mActivityTestRule =
            new ActivityTestRule<>(MainActivity.class);

    @Test
    public void checkTextView_isDisplayed_and_notEmpty() throws Exception {
        // perform a click on the button
        onView(withId(R.id.button)).perform(click());

        // passes if the textView does not match the empty string
        onView(withId(R.id.textView)).check(matches(not(withText(""))));
    }
}

只有当 textView 包含任何文本时,测试才会通过。

关于android - 使用 Espresso 测试 TextView 值不为空,如果 TextView 值为空则失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46598149/

相关文章:

android - Android错误 “SQLiteException:near ” 1“: syntax error (code1)

Android AudioManager getParameters 和 setParameters 函数

android - 繁重布局的流畅动画

安卓 : GL error while resizing: 0x506 (ignored)

安卓 Espresso : Make assertion while button is kept pressed

android-intent - Espresso-Intents - 匹配器和 stub

Intellij Idea IDE 中的 Android Espresso 设置

android - Android的MultiDex支持问题

android - 将retrofit 2中的公共(public)路径参数替换为okhttp

testing - Gradle + Robolectric + Espresso : can't run separately