Espresso - 如何在执行某项操作后检查某项活动是否已启动?

use*_*024 56 android android-activity android-espresso

以下是我的Espresso测试用例之一.

    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())));

    }
Run Code Online (Sandbox Code Playgroud)

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

小智 65

您可以使用:

intended(hasComponent(YourExpectedActivity.class.getName()));
Run Code Online (Sandbox Code Playgroud)

需要此gradle条目:

androidTestCompile ("com.android.support.test.espresso:espresso-intents:$espressoVersion")
Run Code Online (Sandbox Code Playgroud)

进口intended()hasComponent()

import static android.support.test.espresso.intent.Intents.intended;
import static android.support.test.espresso.intent.matcher.IntentMatchers.hasComponent;
Run Code Online (Sandbox Code Playgroud)

如Shubam Gupta所述,请记得在打电话Intents.init()前打电话intended().你最终可以在@Before方法中调用它.


小智 16

尝试:

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

另外,请记住

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

  • 谢谢你提到init()的事情! (3认同)

s-h*_*ter 8

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

androidTestImplementation "com.android.support.test.espresso:espresso-intents:3.0.1"
Run Code Online (Sandbox Code Playgroud)

然后在测试文件中导入这两个

import static android.support.test.espresso.intent.Intents.intended
import static android.support.test.espresso.intent.matcher.IntentMatchers.hasComponent
Run Code Online (Sandbox Code Playgroud)

然后在测试类中添加IntentsTestRule

@Rule
@JvmField
val mainActivityRule = IntentsTestRule(MainActivity::class.java)
Run Code Online (Sandbox Code Playgroud)

最后检查活动是否已启动意图

@Test
fun launchActivityTest() {
    onView(ViewMatchers.withId(R.id.nav_wonderful_activity))
            .perform(click())

    intended(hasComponent(WonderfulActivity::class.java!!.getName()))
}
Run Code Online (Sandbox Code Playgroud)


den*_*nys 7

问题是您的应用程序在单击登录按钮后执行网络操作.Espresso默认不处理(等待)网络呼叫.您必须实现自定义IdlingResource,它将阻止Espresso继续进行测试,直到IdlingResource在空闲状态下返回,这意味着网络请求已完成.看看Espresso样本页面 - https://google.github.io/android-testing-support-library/samples/index.html


luj*_*jop 7

试试吧

intended(hasComponent(new ComponentName(getTargetContext(), ExpectedActivity.class)));
Run Code Online (Sandbox Code Playgroud)

看看@riwnodennyk的回复

  • 为什么需要`new ComponentName`? (2认同)

Abd*_*ood 6

你可以这样做:

    @Test
public void testLoginAttempt() {
    Espresso.onView(ViewMatchers.withId(R.id.username)).perform(ViewActions.clearText()).perform(ViewActions.typeText("nonexistinguser@example.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();
}
Run Code Online (Sandbox Code Playgroud)

java.lang.NullPointerException如果Intents.init()没有被调用则被抛出.