如何进行10次快速点击[UIautomator]

时间:2016-02-04 08:03:53

标签: android unit-testing testing ui-automation uiautomator

我试图使用此

快速点击按钮10次
public static void fastClicks(String text, int index) throws Exception {
        Thread.sleep(1000);
        UiObject settingsButton = new UiObject(new UiSelector().resourceId(text).index(index));
        Configurator cc = Configurator.getInstance();
        cc.setActionAcknowledgmentTimeout(10);
          for (int i = 1; i < 11; ++i){
        settingsButton.click();
        System.out.println("clicked "+ i + " ");
    }
    }

是的,它点击了10次,但是第一次点击有一点延迟或类似的东西,所以它没有正常工作。我需要的只是10次点击点击,从1次点击到10次延迟相同。如何改进此代码?谢谢:))

否则我试过这段代码

 public static void fastClicks(String text, int index, int clicksCount) throws Exception {
        UiObject settingsButton = new UiObject(new UiSelector().resourceId(text).index(index));
        for(int currentClickIndex = 0; currentClickIndex < clicksCount; currentClickIndex++) {
            if(settingsButton.exists()) {
                settingsButton.click();
                Thread.sleep(40);
                System.out.println("clicked " + currentClickIndex + " times");
            }
        }
    }

仍然没有。

1 个答案:

答案 0 :(得分:3)

抱歉,我没有足够的声誉发表评论,所以我会尝试将其作为正确答案。

因为只有第一次单击才能看到此行为,所以可能会发生这种行为,因为某些配置是在操作本身之前(或之后)生成的。例如:

https://android.googlesource.com/platform/frameworks/testing/+/master/uiautomator/library/core-src/com/android/uiautomator/core/UiObject.java

public boolean click() throws UiObjectNotFoundException {
    [...]
    AccessibilityNodeInfo node = findAccessibilityNodeInfo(mConfig.getWaitForSelectorTimeout());
    [...]
}

protected AccessibilityNodeInfo findAccessibilityNodeInfo(long timeout) {
    [...]
    while (currentMills <= timeout) {
        node = getQueryController().findAccessibilityNodeInfo(getSelector());
        if (node != null) {
            break;
        } else {
            // does nothing if we're reentering another runWatchers()
            UiDevice.getInstance().runWatchers();
        }
       [...]
    }
    return node;
}

为避免这种情况,您可以先尝试获取对象的边界,然后直接调用getUiDevice().click(...)

    UiObject settingsButton = new UiObject(new UiSelector().resourceId(text).index(index));
    Rect bounds = settingsButton.getBounds();
    for (int i = 1; i < 11; ++i){
         getUiDevice().click(bounds.centerX(), bounds.centerY());
         System.out.println("clicked "+ i + " ");
    }

(由@Rami Kuret https://stackoverflow.com/a/17497559/2723645建议)