是否可以在按下某个按钮后跟踪哪个活动被打开?
我有一个测试,当点击/按下按钮时,向服务器发送请求。在发送请求的时间内,会打开一个活动。 要验证测试的成功执行,我需要检查哪些是打开的活动。
我的测试示例:
检查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);
}
答案 0 :(得分:20)
要将已启动的活动与espresso意图实际匹配,您需要检查新意图的组成部分:
intended(hasComponent(NewActivity.class.getName()));
请务必在设置中调用Intents.init()
,然后在拆解时Intents.release()
调用能够使用espresso记录意图。
答案 1 :(得分:9)
是否可以追踪活动之后打开的内容 按下按钮?
检查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-intents
不会在没有espresso-core
,runner
或rules
库的情况下运行。
您可能还需要将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
其他资源: