Espresso - 使用按下按钮的意图检查打开哪个活动?

met*_*ink 7 testing android android-espresso

是否可以在按下某个按钮后跟踪哪个活动被打开?
我有一个测试,当点击/按下按钮时,它会向服务器发送一个请求.直到发送请求的时间,它会打开一个活动.为了验证测试的成功执行,我需要检查什么是打开的Activity.

我的测试示例:

检查Espresso中打开的Intent ---

 private void startTest() {
    recreateAuthData(InstrumentationRegistry.getTargetContext(), "d78269d9-9e00-4b8d-9242-815204b0a2f6", "3f32da21-914d-4adc-b6a1-891b842a2972");

    InstrumentationRegistry.getTargetContext().getSharedPreferences(ActivitySplashScreen.class.getSimpleName(),
            Context.MODE_PRIVATE).edit().putInt(ActivitySplashScreen.PROPERTY_APP_VERSION, ActivitySplashScreen.getAppVersion(InstrumentationRegistry.getTargetContext())).commit();
    InstrumentationRegistry.getTargetContext().getSharedPreferences(ActivitySplashScreen.class.getSimpleName(),
            Context.MODE_PRIVATE).edit().putString(ActivitySplashScreen.PROPERTY_REG_ID, "testKey").commit();

    mActivityRule.launchActivity(setIntent());
    // inputPinCode("2794");
}

@Test
public void testIdent() {
    startTest();
    onView(withText("???")).perform(click());
    putDelay(500);
    onView(withId(R.id.get_pro)).perform(click());
    onView(withText("??????????? ?? ?????????? ??????")).perform(click());
    putDelay(500);
    closeSoftKeyboard();
    onView(withId(R.id.btn_goto_passport)).perform(click());
    onView(withHint("????? ? ????? ????????")).perform(replaceText("9894657891"));
    onView(withHint("???? ?????? ????????")).perform(replaceText("17032014"));
    onView(withHint("???? ????????")).perform(replaceText("31091994"));
    onView(withHint("?????")).perform(replaceText("54665285919"));
    putDelay(500);
    Log.d("TestWidget", hasComponent(hasShortClassName("ActivityMain")).toString());
    onView(withId(R.id.btn_next)).perform(click());
    // some code which check which activity is display now
    putDelay(500);

}
Run Code Online (Sandbox Code Playgroud)

Be_*_*ive 19

要将已启动的活动与espresso意图实际匹配,您需要检查新意图的组件:

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

确保Intents.init()在设置和Intents.release()拆解时调用能够记录浓缩咖啡的意图.


pio*_*543 10

是否可以在按下按钮后跟踪哪个活动打开?

检查espresso-intents库:

组态

添加到您的app/build.gradle这些行:

androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.2'
Run Code Online (Sandbox Code Playgroud)

注意:espresso-intents不会在未运行espresso-core,runnerrules库.

你也可能需要改变ActivityTestRule<>,以IntentsTestRule作为其描述如下:

IntentsTestRule

使用Espresso-Intents时,请使用IntentsTestRule而不是ActivityTestRule.IntentsTestRule可以在功能UI测试中轻松使用Espresso-Intents API.此类是ActivityTestRule的扩展,它在每次使用@Test注释的测试之前初始化Espresso-Intents,并在每次测试运行后释放Espresso-Intents.每次测试后活动都将终止,此规则的使用方式与ActivityTestRule相同.

来自:https: //google.github.io/android-testing-support-library/docs/espresso/intents/

示例代码(单击按钮以启动新活动)

这是espresso-intents针对类似问题的解决方案:

使用intent stubbing的示例测试:

@Test
 public void testActivityResultIsHandledProperly() {
   // Build a result to return when a particular activity is launched.
   Intent resultData = new Intent();
   String phoneNumber = "123-345-6789";
   resultData.putExtra("phone", phoneNumber);
   ActivityResult result = new ActivityResult(Activity.RESULT_OK, resultData);

   // Set up result stubbing when an intent sent to "contacts" is seen.
   intending(toPackage("com.android.contacts")).respondWith(result));

   // User action that results in "contacts" activity being launched.
   // Launching activity expects phoneNumber to be returned and displays it on the screen.
   user.clickOnView(system.getView(R.id.pickButton));

   // Assert that data we set up above is shown.
   assertTrue(user.waitForText(phoneNumber));
 }
Run Code Online (Sandbox Code Playgroud)

来自:https: //developer.android.com/reference/android/support/test/espresso/intent/Intents.html

其他资源: