android - 如何使用 Robolectric 在另一个应用程序中测试 Activity 启动

标签 android automated-tests robolectric android-testing

我有一个场景要测试:

  1. 在我的应用中,点击一个按钮 - 例如,btn1
  2. btn1 点击使用 intent action=VIEW, uri=http://www.m10v.com, category= DEFAULT 启动另一个 Activity 。此 Activity 属于另一个包(例如:browser)
  3. 我如何:
    • 确保安装了具有此 Intent 过滤器 Activity 的应用程序?
    • 如何测试 Activity 是否真正开始?

最佳答案

Robolectric 应该用于对您的代码进行单元测试。它消除了 android 运行时,允许快速测试/重构周期。测试在 JVM 上运行,而不是在模拟器或设备上运行。因此,您不能使用它与系统中的其他组件进行交互。

对于您的场景,有两种选择:

编写单元测试并仅验证交互,即检查 Intent 属性。您可以为此使用 Robolectric 或 ActivityUnitTestCase。

使用 UiAutomator Framework (Api 16) 或使用随 Api 18 引入的新 UIAutomation Apis 编写功能性 Ui 测试。

关于android - 如何使用 Robolectric 在另一个应用程序中测试 Activity 启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17864163/

相关文章:

android - Chris Banes 的 Android-PullToRefresh java.lang.NoSuchFieldError : com. handmark.pulltorefresh.library.R$id.pull_to_refresh_sub_text

android - 尝试使用 Robolectric 读取 Assets 时出现 FileNotFoundException

android - 使用 dagger2 进行 Robolectric 测试 Activity

c# - Selenium C# 中的显式等待不起作用。怎么了?

testing - 当我们在现有测试用例中间添加一个新测试用例时,测试框架如何表现?

cucumber "OR"条款?

java - 当我已经赋值时如何修复空指针异常

android - 下载管理器 android 出错

android - 在 flutter 中将屏幕分成两个相等的部分

android - 在不一致的位置观察到软件包ID 'system-images;android-28;default;x86_64'