【问题标题】:Android Expresso/UI Automator How do i automate clicking on Android screens outside my app (app picker)Android Espresso/UI Automator 如何自动点击我的应用程序外部的 Android 屏幕(应用程序选择器)
【发布时间】:2018-01-31 19:42:14
【问题描述】:

我的 Android 应用程序有一个按钮,用于下载文件,然后将其发送到设备上的应用程序。 Android 会弹出一个屏幕,列出设备上的应用程序供用户选择要使用的应用程序。

我想自动执行此流程,但我看不到如何自动单击 Android 提供的应用程序选择器。我想这是因为它在我的应用程序之外。

我尝试使用 Android Studio 的“Record Expresso Test”,我执行了以下测试步骤

  1. 单击将我的图像发送到设备上的应用程序的操作 (action1)
  2. 看到 Android 应用程序选择器出现并选择了照片
  3. 点击返回关闭照片应用并返回我的应用
  4. 点击我的应用中的不同操作 (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 个检查,它应该只识别应用程序选择器。

【问题讨论】:

标签: android android-uiautomator expresso


【解决方案1】:

这个问题的答案应该归功于 John O'Reilly,他让我使用 UI Automator。

我解决了检查当我的测试点击一个动作时调用哪个 Android 屏幕的问题,只需检查屏幕上是否有一个带有我期望的标题的 TextView。它并不完美,因为如果屏幕上有任何带有文本的 TextView,这将通过,因此不会精确检查其应用程序选择器。

但是,对于我的测试,这应该足以进行检查,因为我的应用程序(将在应用程序选择器后面)不应该有带有预期标题的 TextView,所以如果找到标题,它很可能是应用程序选择器。

public static boolean verifyAndroidScreenTitlePresent(String title) {
    UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

    UiObject titleTextUI = new UiObject(new UiSelector()
            .className("android.widget.TextView")
            .text(title)
    );
    boolean titleExists = titleTextUI.exists();

    // close the app selector to go back to our app so we can carry on with Expresso
    mDevice.pressBack();

    return titleExists;
}

【讨论】:

    猜你喜欢
    • 2013-09-15
    • 1970-01-01
    • 2020-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多