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

时间:2013-08-12 16:10:14

标签: android automation uiautomator

我对Google的uiautomator有相当多的经验;然而,在手机的主屏幕上添加小部件时,我似乎感到难过。现在让我们保持简单,并假设添加小部件的屏幕是空的。思考过程是打开app抽屉>点击小工具标签>找到要添加的小部件>长按并将小部件拖动到主屏幕。虽然小部件似乎不是“长时间可点击”。任何想法/建议/解决方案将不胜感激。我实施的代码如下。

@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 个答案:

答案 0 :(得分:1)

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

我相信事情正在发生,但又是什么? : - /它就像一个背面被点击了

答案 1 :(得分: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 ::结束坐标的

endY ::结束坐标的

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

返回:

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

自:

Android API等级18

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

答案 2 :(得分:0)

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

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

  • 使用水平和垂直滚动窗口小部件选择器(由const变量更改)
  • with Configuration Activity

代码内联的额外说明,您可能需要更改案例中的某些值,以便通过评论。

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中完美运行。

答案 3 :(得分:-2)

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

*步骤应该是> 100以确保点击足够长

相关问题