5

我对 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);
}
4

4 回答 4

1

使用安德斯的想法,我设法长按小部件并将其拖到家中的某个地方,但在返回小部件列表之前,我简要地看到了我的配置活动:((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)
}

我相信有些事情正在发生,但是什么?:-/ 就像点击了背部一样

于 2016-10-07T11:57:18.380 回答
0

在 uiautomator Android 中长按也可以使用:

   public boolean drag (int startX, int startY, int endX, int endY, int steps)

执行从一个坐标到另一个坐标的滑动。您可以通过指定步数来控制滑动的平滑度和速度。每一步执行被限制为每步 5 毫秒,因此对于 100 步,滑动大约需要 0.5 秒才能完成。

参数:

startX :: 起始坐标的X轴值

startY :: 起始坐标的 Y 轴值

endX :: 结束坐标的 X 轴值

endY :: 结束坐标的 Y 轴值

Steps :: 是滑动操作的步数。

返回:

如果执行滑动,则为 true,如果操作失败或坐标无效,则为 false。

自从 :

Android API 级别 18

所以给 (startX,startY) = (endX,endY) 。这相当于长按。

于 2013-08-13T11:15:10.563 回答
0

感谢@geob-o-matic发布的代码。

我必须添加一些修改才能工作:

  • 带有水平和垂直滚动小部件选择器(由 const 变量更改)
  • 带有配置活动

代码中内嵌的额外解释,您可能需要更改某些值,以便查看注释。

const val TIMEOUT: Long = 5000

// WIDGET_SELECTION_AT_X: USE here true or depending 
//  if you have to scroll at the X or Y axis when 
//  navigating through widget selection screen.
const val WIDGET_SELECTION_AT_X: Boolean = true
const val WIDGET_NAME: String = "MyAppWigdetName"

@RunWith(AndroidJUnit4::class)
class WidgetAutomatorTest {

    private val mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation())

    @Before
    fun setWidgetOnHome() {
        val screenSize = Point(mDevice.displayWidth, mDevice.displayHeight)
        val screenCenter = Point(screenSize.x / 2, screenSize.y / 2)
        // showWidgets: This a point on screen between the bottom icons 
        // and the widgets, its a point that has no objects on a Galaxy S5 
        // device with stock Launcher. Most probably you have to modify it in
        // your device or use an empty homescreen and just long press at 
        // the center of it.
        val showWidgets = Point(825, 1500)

        mDevice.pressHome()

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

        // attempt long press
        // Here you can use the screenCenter if you don't have any widget standing/living there!
        // ie. mDevice.swipe(arrayOf(showWidgets, screenCenter), 150)
        mDevice.swipe(arrayOf(showWidgets, showWidgets), 150)

        // Navigate to the system's widget selector, localaize if needed.
        mDevice.findObject(By.text("Widgets")).click()

        var diment = screenSize.y / 2
        if (WIDGET_SELECTION_AT_X) {
            diment = screenSize.x / 2
        }

        var widget = findMyWidget(WIDGET_NAME)
        while (widget == null) {
            if (WIDGET_SELECTION_AT_X) {
                // Swipe left to right
                mDevice.swipe(diment, screenCenter.y, 0, screenCenter.y, 150)
            } else {
                // Swipe top to bottom
                mDevice.swipe(screenCenter.x, diment, screenCenter.x, 0, 150)
            }

            widget = findMyWidget(WIDGET_NAME)
        }
        // Throw the selected widget on screen
        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)
    }

    private fun findMyWidget(withName: String): UiObject2? {
        return mDevice.findObject(By.text(withName))
    }

    @Test
    fun addWidget() {
        // Press the button on the Widget Configuration Activity
        val okButton = mDevice.findObject(UiSelector()
                .text("Add widget") 
                .className("android.widget.Button"))
        okButton.click()

        // Find the just added widget
        val widget = mDevice.findObject(By.descContains(WIDGET_NAME))
        // Click outside the widget in order to added in the screen
        mDevice.click(widget.visibleBounds.left - 150, widget.visibleBounds.top - 150)

    }
}

笔记。提供的示例在 Galaxy S5、Android 6.0.1、1080x1920 中完美运行。

于 2018-04-29T10:48:17.543 回答
-2

您可以使用 dragTo (相同的 UI 对象,步骤)

*步数应大于 100 以确保点击时间足够长

于 2014-02-25T14:20:43.817 回答