【问题标题】:Is there a way to test Chrome Custom Tabs with Espresso?有没有办法用 Espresso 测试 Chrome 自定义标签?
【发布时间】:2017-06-03 11:02:50
【问题描述】:

这是代码的存根。

点击ListView 上的数据项。按设计工作并打开 Chrome 自定义标签

onData(anything()).inAdapterView(withId(R.id.listView))
                                       .atPosition(0).perform(click());

Pause(5000);
Espresso.pressBack();

似乎无法评估选项卡中的任何内容,甚至无法点击设备后退按钮。 收到此错误

Error : android.support.test.espresso.NoActivityResumedException: No 
activities  in stage RESUMED.

您是否忘记启动活动。 (test.getActivity() 或类似的)?

【问题讨论】:

  • 请编辑您的帖子,以便无需猜测即可理解。并考虑发布minimal reproducible example - 这有助于了解您的问题!
  • 我也遇到了同样的问题
  • 这里也是同样的问题。

标签: android listview android-espresso chrome-custom-tabs


【解决方案1】:

您可以使用 UIAutomator (https://developer.android.com/training/testing/ui-automator.html)。您实际上可以同时使用 Espresso 和 UIAutomator。有关更多信息,请参阅以下帖子中接受的答案: How to access elements on external website using Espresso

【讨论】:

    【解决方案2】:

    您可以阻止打开自定义选项卡,然后断言您启动的意图是否正确:

    fun stubWebView(uri: String) {
        Intents.intending(allOf(IntentMatchers.hasAction(Intent.ACTION_VIEW), IntentMatchers.hasData(uri)))
            .respondWith(Instrumentation.ActivityResult(Activity.RESULT_OK, null))
    }
    
    fun isNavigatedToWebView(uri: String) {
        Intents.intended(allOf(IntentMatchers.hasAction(Intent.ACTION_VIEW), IntentMatchers.hasData(uri)))
    }
    

    这样你就可以在你的测试中避免Espresso.pressBack()

    请注意,由于这些使用 Espresso Intent,您需要使用 IntentsTestRule 或使用 Intents.init 包装它们并像这样发布

    fun intents(func: () -> Unit) {
        Intents.init()
    
        try {
            func()
        } finally {
            Intents.release()
        }
    }
    
    intents {
        stubWebView(uri = "https://www.example.com")
        doSomethingSuchAsClickingAButton()
        isNavigatedToWebView(uri = "https://www.example.com")
    }
    

    【讨论】:

      【解决方案3】:

      在 Mustafa answer 之后提高可读性的建议:

      intents{
          Intents.intended(
              CoreMatchers.allOf(
                  IntentMatchers.hasAction(Intent.ACTION_VIEW),
                  IntentMatchers.hasPackage("com.android.chrome")
              )
      
          )
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-09-06
        • 1970-01-01
        • 1970-01-01
        • 2017-03-26
        • 1970-01-01
        • 1970-01-01
        • 2016-04-18
        • 1970-01-01
        相关资源
        最近更新 更多