tfr*_*ger 5 automated-tests android-espresso android-junit android-espresso-recorder
我使用 Android Studio 4.2.2 中的记录器记录了一个 Espresso 测试,其中包括一个断言,即我的 MainActivity UI 上的文本字段显示了正确的文本字符串。然后我将其保存到 SplashActivityTest.java:
公共类 SplashActivityTest {
@Rule
public ActivityTestRule<SplashActivity> mActivityTestRule = new ActivityTestRule<>(SplashActivity.class);
@Before
public void registerIdlingResource() {
IdlingRegistry.getInstance().register(CountingIdlingResourceSingleton.espressoIdlingResource);
}
@After
public void unregisterIdlingResource() {
IdlingRegistry.getInstance().unregister(CountingIdlingResourceSingleton.espressoIdlingResource);
}
@Test
public void splashActivityTest() {
ViewInteraction textView = onView(
allOf(withId(R.id.playlistText), withText("My Playlists"),
withParent(withParent(withId(R.id.nav_host_fragment))),
isDisplayed()));
textView.check(matches(isDisplayed()));
ViewInteraction textView2 = onView(
allOf(withId(R.id.playlistText), withText("My Playlists"),
withParent(withParent(withId(R.id.nav_host_fragment))),
isDisplayed()));
textView2.check(matches(withText("My Playlists")));
}
Run Code Online (Sandbox Code Playgroud)
}
我向此类添加了 Idling 注册表的使用,因为在我的应用程序中,实际发生的启动画面是启动器活动,然后启动加载我想要测试的 UI 的活动。
我有这个代码:
// Necessary for automated tests, decrement handled in MainActivity.onResume()
CountingIdlingResourceSingleton.increment();
Run Code Online (Sandbox Code Playgroud)
在 SplashActivity 的 onCreate() 方法和这段代码中:
// Necessary for automated tests - increment is done in SplashActivity.onCreate()
CountingIdlingResourceSingleton.decrement();
Run Code Online (Sandbox Code Playgroud)
在 MainActivity 中 onResume() 的末尾。
上面的代码运行完美,测试成功。耶。
然而,我在使用 ActivityTestRule 时收到了弃用警告,赞成使用 ActivityScenarioRule 而不是 ActivityTestRule (有点有趣,因为使用该 API 是由最新 4.2.2 Android Studio 中的 Espresso 记录器生成的,但那就是不同帖子的主题!)。
所以我改变它:
公共类 SplashActivityTest {
@Rule
public ActivityScenarioRule<SplashActivity> mActivityTestRule = new ActivityScenarioRule<>(SplashActivity.class);
@Before
public void registerIdlingResource() {
IdlingRegistry.getInstance().register(CountingIdlingResourceSingleton.espressoIdlingResource);
}
@After
public void unregisterIdlingResource() {
IdlingRegistry.getInstance().unregister(CountingIdlingResourceSingleton.espressoIdlingResource);
}
@Test
public void splashActivityTest() {
ViewInteraction textView = onView(
allOf(withId(R.id.playlistText), withText("My Playlists"),
withParent(withParent(withId(R.id.nav_host_fragment))),
isDisplayed()));
textView.check(matches(isDisplayed()));
ViewInteraction textView2 = onView(
allOf(withId(R.id.playlistText), withText("My Playlists"),
withParent(withParent(withId(R.id.nav_host_fragment))),
isDisplayed()));
textView2.check(matches(withText("My Playlists")));
}
Run Code Online (Sandbox Code Playgroud)
}
现在它不再完美运行。我的应用程序启动,应用程序类运行,但启动器类从未被调用。相反,我得到:
java.lang.AssertionError: Activity never becomes requested state "[DESTROYED, CREATED, STARTED, RESUMED]" (last lifecycle transition = "PRE_ON_CREATE")
Run Code Online (Sandbox Code Playgroud)
为什么?我需要做哪些不同的事情才能确保调用我的正常启动器活动?
| 归档时间: |
|
| 查看次数: |
2837 次 |
| 最近记录: |