浓缩咖啡-通过按一下按钮意图检查打开了哪个活动? [英] Espresso - check which Activity is opened using intent on button press?

查看:73
本文介绍了浓缩咖啡-通过按一下按钮意图检查打开了哪个活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我的测试示例:

Is it possible to trace which Activity is opened after a certain button is pressed?
I have a test in which, when a button is clicked / pressed, it sends a request to the server. Till the time the request is sent, it opens an Activity. To verify the successful execution of the test, I need to check what is the open Activity.

Example of my test:

检查在Espresso中打开了哪个Intent ---

Check which Intent is opened in Espresso ---

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

}

推荐答案

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

Whether it is possible to trace which the Activity opened after pressing the button?

检查espresso-intents库:

将以下几行添加到您的app/build.gradle中:

Add to your app/build.gradle these lines:

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-corerunnerrules库,espresso-intents将无法运行.

NOTICE: espresso-intents won't run without espresso-core, runner or rules libs.

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

You may also need to change ActivityTestRule<> to IntentsTestRule as it is described here:

IntentsTestRule

使用时使用IntentsTestRule代替ActivityTestRule 意式浓缩咖啡. IntentsTestRule易于使用 功能性UI测试中的Espresso-Intents API.这堂课是 ActivityTestRule的扩展,它初始化Espresso-Intents 在每个测试使用@Test注释并释放Espresso-Intents之前 每次测试运行后.该活动将在每次测试后终止 该规则可以与ActivityTestRule相同的方式使用.

IntentsTestRule

Use IntentsTestRule instead of ActivityTestRule when using Espresso-Intents. IntentsTestRule makes it easy to use Espresso-Intents APIs in functional UI tests. This class is an extension of ActivityTestRule, which initializes Espresso-Intents before each test annotated with @Test and releases Espresso-Intents after each test run. The activity will be terminated after each test and this rule can be used in the same way as ActivityTestRule.

发件人: https://google.github.io/android-testing- support-library/docs/espresso/intents/

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

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

Example code (click on button to launch new activity)

Here's a solution using espresso-intents for similar problem:

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

An example test with 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));
 }

发件人: https://developer.android.com/reference/android/support/test/espresso/intent/Intents.html

其他资源:

[Github || Google样本]意式浓缩咖啡的基本样本

[Github] Android Espresso意向示例

测试使用Espresso的Android Intent

[要点]在Android测试中如何使用意式浓缩咖啡的示例-上面链接的源代码

这篇关于浓缩咖啡-通过按一下按钮意图检查打开了哪个活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆