【问题标题】:How to make 10 fast clicks [UIautomator]如何进行 10 次快速点击 [UIautomator]
【发布时间】:2016-05-13 17:50:10
【问题描述】:

我正在尝试使用此按钮快速点击 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 次 ritmic 点击,从 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");
            }
        }
    }

还是什么都没有。

【问题讨论】:

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


    【解决方案1】:

    抱歉,我没有足够的声誉来发表评论,所以我会尽量让这个答案成为正确的答案。

    由于此行为仅在第一次单击时可见,因此可能是因为在操作本身之前(或之后)进行了一些配置。例如:

    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 建议)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-08
      • 1970-01-01
      • 2016-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-27
      • 2016-02-01
      相关资源
      最近更新 更多