Android 意式浓缩咖啡-使用“意向开启”按钮检查打开的活动?

Android 意式浓缩咖啡-使用“意向开启”按钮检查打开的活动?,android,testing,android-espresso,Android,Testing,Android Espresso,是否可以跟踪在按下某个按钮后打开的活动?我有一个测试,在该测试中,单击/按下某个按钮时,会向服务器发送请求。在发送请求之前,将打开一个活动要验证测试的成功执行,我需要检查什么是打开的活动。我的测试示例: 检查在浓缩咖啡中打开的目的--- 是否可以跟踪在之后打开的活动 按下按钮 检查浓缩咖啡的意图library: 配置 添加到你的应用程序/build.gradle以下行: androidTestCompile 'com.android.support.test:runner:0.5' androi

是否可以跟踪在按下某个按钮后打开的活动?
我有一个测试,在该测试中,单击/按下某个按钮时,会向服务器发送请求。在发送请求之前,将打开一个活动要验证测试的成功执行,我需要检查什么是打开的活动。

我的测试示例:

检查在浓缩咖啡中打开的目的---

是否可以跟踪在之后打开的活动 按下按钮

检查浓缩咖啡的意图library:

配置 添加到你的
应用程序/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
规则
libs,将无法运行

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

意图测试规则 使用时请使用IntentsTestRule而不是ActivityTestRule 意式浓缩咖啡。IntentsTestRule使其易于使用 Espresso在功能UI测试中使用API。这门课是一门课程 ActivityTestRule的扩展,用于初始化意式浓缩咖啡 每次测试前,用@test标注并释放浓缩咖啡意向 每次测试运行后。每次测试后,活动将终止 此规则的使用方式与ActivityTestRule相同

发件人:

示例代码(单击按钮启动新活动) 对于类似的问题,这里有一个使用浓缩咖啡的解决方案:

使用意向存根的示例测试:

@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));
 }
发件人:

额外资源:

  • -上面链接的源代码


要将已启动的活动与意式浓缩咖啡真正匹配,您需要检查新意式的组成部分:

预期(hasComponent(NewActivity.class.getName());

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

这实际上是检查活动是否存在于记录的意图列表中。如果之前打开了相同的活动,这也会引发错误。所以要谨慎使用它。
@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));
 }