java - Espresso ViewAssertions - Check If Not Null
问题描述
I am runnning Espresso testing and I want to test 2 items:
- Click of a button in Fragment X
- The TextView contains text in Activity Y
code:
@RunWith(AndroidJUnit4.class)
public class AsyncTest {
private static final String TEST_TEXT = "TEST_TEXT";
@Rule
public ActivityTestRule<MainActivity> mActivity = new ActivityTestRule(MainActivity.class);
@Test
public void testIfResultIsEmpty() {
Espresso.onView(ViewMatchers.withId(R.id.first_button))
.perform(ViewActions.click());
/* How do I check that text has populated? */
Espresso.onView(ViewMatchers.withId(R.id.random_joke))
.check();
}
解决方案
matches(isDisplayed())
would at least check if the view is being displayed - while one barely can check for the text of a random joke, where one would assume some random text... unless loading a specific text for testing purposes, which is then being known - and could be compared/matched (as explained recently in the RecyclerView answer, the ActivityTestRule
could be used to pass an ITEM_ID
, so that one would have a specific item to compare the views to).
onView(withId(R.id.random_joke)).check(matches(isDisplayed()));
and one does not have to use Espresso, but can try findViewById()
and then assertNotNull()
.
推荐阅读
- html - 引导模板适用于 index.html 但不适用于任何反应组件?
- python - 如何在循环中读取 csv 文件
- reactjs - 如何使我的 setValue +1 代码变为 12345 而不是 11111
- php - PHP调用python脚本和按钮在php中单击后消失
- azure - Linux VMSS 操作系统磁盘自动增长选项
- reactjs - 第 31:7 行:React Hook useEffect 缺少依赖项:'url'。包括它还是删除依赖数组?
- c++ - 为什么我的伺服电机在尝试前进时不会停止旋转?
- java - 在实体到多 JPA 之间创建关联
- javascript - React - 如何在我的表单内返回 Axios 捕获错误?
- python-3.x - 在类中查找选项卡错误的原因