本文介绍了如何使用Espresso测试片段是否可见的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用按钮编写了一个非常简单的Activity
.单击该按钮后,我将启动一个新的Fragment
.现在,我想在Espresso UI测试中测试此逻辑.所以我为我的Activity
I wrote a very simple Activity
with a Button. When the button is clicked, I will start a new Fragment
. Now I want to test this logic in my Espresso UI Test. So I wrote this UI Test for my Activity
@RunWith(AndroidJUnit4::class)
@LargeTest
class MainMenuUiTest {
@get: Rule
val activityTestRule: ActivityTestRule<MainActivity> = ActivityTestRule(MainActivity::class.java)
@Test
fun switchToFragment() {
onView(withId(R.id.btn)).perform()
}
}
当单击按钮时,如何测试我的Fragment
是否显示?
How can I test that my Fragment
is displayed when the button is clicked?
推荐答案
尝试以下操作:
@Test
fun switchToFragment() {
onView(withId(R.id.btn)).perform(click())
onView(withId(R.id.fragment)).check(matches(isDisplayed()))
}
如果您只是在学习Espresso,则官方指南是开始的好地方.另外,这是浓缩咖啡秘籍表.
If you're just learning about Espresso, the official guide is a good place to start. Also, here's the Espresso cheat sheet.
这篇关于如何使用Espresso测试片段是否可见的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!