android - UiAutomator -- 将小部件添加到主屏幕

标签 android automation android-uiautomator

我对 Google 的 uiautomator 有相当多的经验;但是,在向手机的主屏幕添加小部件时,我似乎感到很困惑。现在让我们保持简单,并假设要添加小部件的屏幕是空的。思考过程是打开应用程序抽屉 > 单击小部件选项卡 > 找到要添加的小部件 > 长按并将小部件拖到主屏幕。看起来小部件不是“可长时间点击”的。任何想法/建议/解决方案将不胜感激。我实现的代码如下。

@Override
protected void setUp() throws UiObjectNotFoundException {
    getUiDevice().pressHome();

    new UiObject(new UiSelector().className(TEXT_VIEW).description("Apps")).clickAndWaitForNewWindow();
    new UiObject(new UiSelector().className(TEXT_VIEW).text("Widgets")).click();

    UiScrollable widgets = new UiScrollable(new UiSelector().scrollable(true));
    widgets.setAsHorizontalList();
    widgets.flingToEnd(MAX_SWIPES);

    UiObject widget = widgets.getChildByText(
            new UiSelector().className(TEXT_VIEW).resourceId("com.android.launcher:id/widget_name"),
            WIDGET_NAME
    );

    // Returns true
    System.out.println("exists(): " + widget.exists());
    // Returns false...
    System.out.println("longClickable(): " + widget.isLongClickable());

    widget.longClick();

    // Also tried...
    int startX = sonosWidget.getVisibleBounds().centerX();
    int startY = sonosWidget.getVisibleBounds().centerY();
    getUiDevice().drag(startX, startY, 0, 0, 3);
}

最佳答案

使用 Anders 的想法,我设法长按小部件并将其拖到家里的某个地方,但在返回小部件列表之前我短暂地看到了我的配置 Activity :((Kolin 代码)

@Before fun setWidgetOnHome() {
    mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())
    val screenSize = Point(mDevice.displayWidth, mDevice.displayHeight)
    val screenCenter = Point(screenSize.x / 2, screenSize.y / 2)

    mDevice.pressHome()

    val launcherPackage = mDevice.launcherPackageName!!
    mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT)

    // attempt long press
    mDevice.swipe(arrayOf(screenCenter, screenCenter), 150)
    pauseTest(2000)

    mDevice.findObject(By.text("Widgets")).click()
    mDevice.waitForIdle()

    val y = screenSize.y / 2

    var widget = mDevice.findObject(By.text("Efficio"))
    var additionalSwipe = 1
    while (widget == null || additionalSwipe > 0) {
        mDevice.swipe(screenCenter.x, y, screenCenter.x, 0, 150)
        mDevice.waitForIdle()
        if (widget == null) {
            widget = mDevice.findObject(By.text("Efficio"))
        } else {
            additionalSwipe--
        }
    }
    val b = widget.visibleBounds
    val c = Point(b.left + 150, b.bottom + 150)
    val dest = Point(c.x + 250, c.y + 250)
    mDevice.swipe(arrayOf(c, c, dest), 150)
}

我相信有事情正在发生,但是什么? :-/就像点击了一个后退

关于android - UiAutomator -- 将小部件添加到主屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18191745/

相关文章:

android - UIAutomator 2 在运行 API 级别低于 21 的设备上运行 Shell 命令

android - x86_64 的 Libgmp NDK 构建问题

java - Runtime.exec 不工作

java.lang.IllegalArgumentException : No view found for id 0x7f090047 ("project name":id/content) for fragment FmMenu 异常

shell - 如何在重新启动后自动设置一次 "AT"计划作业

android - 从 ApplicationTest 启动时,DataBindingUtil 不得返回 null

android - 消息上下文不显示在 android 的 webview 中

testing - 我如何生成一个包含功能配置和所有测试设置的脚本,以避免在每个类中编写设置方法

本地化语言的 API 自动化测试

android-uiautomator - 为什么打开uiautomatorviewer时cmd会出错?