意式浓缩咖啡 – 如何在执行特定操作后检查活动是否启动?

以下是我的Espressotesting用例之一。

public void testLoginAttempt() { Espresso.onView(ViewMatchers.withId(R.id.username)).perform(ViewActions.clearText()).perform(ViewActions.typeText("nonexistinguser@krossover.com")); Espresso.onView(ViewMatchers.withId(R.id.username)).perform(ViewActions.clearText()).perform(ViewActions.typeText("invalidpassword")); Espresso.onView(ViewMatchers.withId(R.id.login_button)).perform(ViewActions.click()); // AFTER CLICKING THE BUTTON, A NEW ACTIVITY WILL POP UP. // Clicking launches a new activity that shows the text entered above. You don't need to do // anything special to handle the activity transitions. Espresso takes care of waiting for the // new activity to be resumed and its view hierarchy to be laid out. Espresso.onView(ViewMatchers.withId(R.id.action_logout)) .check(ViewAssertions.matches(not(ViewMatchers.isDisplayed()))); } 

目前我所做的是检查新活动(R.id.action_logout)中的视图是否可见。 如果可见,我将假定活动已成功打开。 但它似乎没有像我预期的那样工作。 有没有更好的方法来检查一个新的活动是否成功启动,而不是检查该活动中的视图是否可见? 谢谢

Solutions Collecting From Web of "意式浓缩咖啡 – 如何在执行特定操作后检查活动是否启动?"

您可以使用:

 intended(hasComponent(YourExpectedActivity.class.getName())); 

需要这个gradle条目:

 androidTestCompile("com.android.support.test.espresso:espresso-intents:$espressoVersion") 

inteded和hasComponent的导入

 import android.support.test.espresso.intent.Intents.intended import android.support.test.espresso.intent.matcher.IntentMatchers.hasComponent 

尝试

 intended(hasComponent(new ComponentName(getTargetContext(), ExpectedActivity.class))); 

看看@riwnodennyk的回应

问题是您的应用程序在您点击loginbutton后执行networking操作。 Espresso不处理(等待)networking电话默认完成。 您必须实现您的自定义IdlingResource,它将阻止Espresso继续进行testing,直到IdlingResource返回到空闲状态,这意味着networking请求已完成。 查看Espresso示例页面 – https://google.github.io/android-testing-support-library/samples/index.html

尝试:

intended(hasComponent(YourActivity.class.getName()));

另外,请记住

如果Intents.init()intended()之前未被调用,则抛出java.lang.NullPointerException

你可以这样做:

  @Test public void testLoginAttempt() { Espresso.onView(ViewMatchers.withId(R.id.username)).perform(ViewActions.clearText()).perform(ViewActions.typeText("nonexistinguser@krossover.com")); Espresso.onView(ViewMatchers.withId(R.id.username)).perform(ViewActions.clearText()).perform(ViewActions.typeText("invalidpassword")); Intents.init(); Espresso.onView(ViewMatchers.withId(R.id.login_button)).perform(ViewActions.click()); Intents.release(); } 

如果不调用Intents.init()则抛出java.lang.NullPointerExceptionexception。

确保Expresso意图库位于gradle依赖项中

 androidTestImplementation "com.android.support.test.espresso:espresso-intents:3.0.1" 

然后在你的testing文件中导入这两个

 import android.support.test.espresso.intent.Intents.intended import android.support.test.espresso.intent.matcher.IntentMatchers.hasComponent 

然后在你的testing类中添加IntentsTestRule

 @Rule @JvmField val mainActivityRule = IntentsTestRule(MainActivity::class.java) 

最后检查活动已经启动的意图

 @Test fun launchActivityTest() { onView(ViewMatchers.withId(R.id.nav_wonderful_activity)) .perform(click()) intended(hasComponent(WonderfulActivity::class.java!!.getName())) } 
 @RunWith(RobolectricTestRunner.class) public class WelcomeActivityTest { @Test public void clickingLogin_shouldStartLoginActivity() { WelcomeActivity activity = Robolectric.setupActivity(WelcomeActivity.class); activity.findViewById(R.id.login).performClick(); Intent expectedIntent = new Intent(activity, LoginActivity.class); assertThat(shadowOf(activity).getNextStartedActivity()).isEqualTo(expectedIntent); } }