【发布时间】:2018-01-31 19:42:14
【问题描述】:
我的 Android 应用程序有一个按钮,用于下载文件,然后将其发送到设备上的应用程序。 Android 会弹出一个屏幕,列出设备上的应用程序供用户选择要使用的应用程序。
我想自动执行此流程,但我看不到如何自动单击 Android 提供的应用程序选择器。我想这是因为它在我的应用程序之外。
我尝试使用 Android Studio 的“Record Expresso Test”,我执行了以下测试步骤
- 单击将我的图像发送到设备上的应用程序的操作 (action1)
- 看到 Android 应用程序选择器出现并选择了照片
- 点击返回关闭照片应用并返回我的应用
- 点击我的应用中的不同操作 (action2)
我在上面的步骤 1 和 4 记录的测试代码中看到,但 2 和 3 没有。因此这让我认为 Expresso 不能用于这个特定的测试流程。
有人知道我如何使用 Expresso 测试这个流程吗?
编辑:
感谢“John O'Reilly”推荐 UI Automator。我可以看到我可以在我的 Expresso 测试中成功使用 UI Automator 代码。但是,我在编写应用程序选择器的精确验证时遇到了麻烦。
选择器的标题为“打开方式”。使用 Android Device Monitor,我可以看到对象的层次结构,如下图所示。
有些类和 ID 是内部的,所以我无法搜索这些东西。我不想编写代码来查找特定应用程序,因为当测试在另一台机器上运行时它可能没有该应用程序。我只需要验证应用程序选择器是否已显示。
// the app selector has a FrameLayout as one of its parent views, and a child Text View which has the "Open With" title
UiObject labelOnly = new UiObject(new UiSelector()
.className("android.widget.FrameLayout")
.childSelector(new UiSelector()
.className("android.widget.TextView")
.text(openWithLabel)
)
);
boolean labelOnly_exists = labelOnly.exists();
// the app selector has a FrameLayout as one of its parent views, and a child ListView (containing the apps)
UiObject listOnly = new UiObject(new UiSelector()
.className("android.widget.FrameLayout")
.childSelector(new UiSelector()
.className("android.widget.ListView")
)
);
boolean listOnly_exists = listOnly.exists();
// I can use the listView to search for a specific app, but this makes the tests fragile if a different device does not have that app installed
UiObject listAndAppName = new UiObject(new UiSelector()
.className("android.widget.ListView")
.instance(0)
.childSelector(new UiSelector()
.text("Photos")));
boolean listAndAppName_exists = listAndAppName.exists();
我如何编写一个语句来验证屏幕上的内容是应用程序选择器?我希望可能有一个选择器来搜索 FrameLayout,它有一个包含“打开方式”的子 textView 并且还包含一个子 ListView。通过这 2 个检查,它应该只识别应用程序选择器。
【问题讨论】:
-
应该可以使用 UI Automator - developer.android.com/training/testing/ui-automator.html
-
谢谢你,这是一个出色的选择器,我可以从我的 Expresso 代码中调用 UI Automator 代码。现在来弄清楚如何精确识别显示的应用程序选择器。
标签: android android-uiautomator expresso