我正在使用(新)ActivityScenarioRule进行Android应用程序上的UI测试。我正在测试多个片段,每个片段都在其单独的类中。在@Before中,我将导航至要测试的屏幕。我之前尝试过新的FragmentScenario,但是由于没有明显的原因,测试存在很多问题,因此我改回默认的Activity。
这是大多数测试类的外观:
@RunWith(AndroidJUnit4::class)
class EventDetailsFragmentTest {
@get:Rule
var activityScenarioRule = ActivityScenarioRule<LoginOrRegisterActivity>(LoginOrRegisterActivity::class.java)
private lateinit var uiDevice: UiDevice
@Before
fun goToScreen() {
NavigationUtil.goToNewEventScreen()
uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
}
@Test
fun eventDetailsFragment_buttonsOpenCorrectScreen() {
onView(withId(R.id.btn_event_details_audio)).perform(click())
...
}
@Test
fun eventDetailsFragment_enterEventTitle() {
...
}
@Test
fun eventDetailsFragment_saveEventWithoutTitleEntered_errorMessage() {
onView(withId(R.id.btn_event_details_send)).perform(click())
...
}
@Test
fun eventDetailsFragment_saveEvent_showsSuccess() {
onView(withId(R.id.btn_event_details_send)).perform(click())
...
}
}
运行(connectedCheck)时,这些错误一次或多次随机出现以下错误:
1) mainScreenFragment_buttonsOpenCorrectScreen(be.hogent.faith.faith.cityScreen.CityScreenFragmentTest)
05:25:24 V/InstrumentationResultParser: java.lang.RuntimeException: No activities found. Did you forget to launch the activity by calling getActivity() or startActivitySync or similar?
我认为ActivityScenario除了规则外只需要什么,但是我应该做更多的事情吗?活动怎么突然消失了?