【问题标题】:Espresso - check which Activity is opened using intent on button press?浓缩咖啡 - 检查使用按钮按下意图打开了哪个活动?
【发布时间】:2016-08-31 13:18:32
【问题描述】:

是否可以跟踪按下某个按钮后打开了哪个 Activity?
我有一个测试,当一个按钮被点击/按下时 ,它向服务器发送请求。在发送请求之前,它会打开一个 Activity。 要验证测试是否成功执行,我需要检查什么是打开的 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);

}

【问题讨论】:

    标签: android testing android-espresso


    【解决方案1】:

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

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

    确保在设置中调用Intents.init(),在拆卸中调用Intents.release(),以便能够使用 Espresso 记录意图。

    【讨论】:

    • 这实际上检查活动是否存在于记录的意图列表中。如果之前打开了相同的活动,这也会引发错误。所以请谨慎使用。
    【解决方案2】:

    是否可以追踪哪个Activity打开之后 按下按钮?

    查看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'
    

    注意:如果没有 espresso-core、runner 或 rules 库,espresso-intents 将无法运行。

    您可能还需要将ActivityTestRule<> 更改为IntentsTestRule,如下所述:

    IntentsTestRule

    使用时使用 IntentsTestRule 而不是 ActivityTestRule 意式浓缩咖啡。 IntentsTestRule 使其易于使用 功能 UI 测试中的 Espresso-Intents API。这门课是一个 ActivityTestRule 的扩展,它初始化 Espresso-Intents 在使用 @Test 注释并发布 Espresso-Intents 的每个测试之前 每次试运行后。活动将在每次测试后终止 并且这条规则的使用方式和ActivityTestRule一样。

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

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

    这是使用espresso-intents 解决类似问题的解决方案:

    带有意图存根的示例测试:

    @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));
     }
    

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

    其他资源:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-15
      • 1970-01-01
      相关资源
      最近更新 更多