Skip to content

功能建议:添加等待控件出现,消失 #7

Open

Description

@allrobot

找了一圈,没找到类似的功能,AutoX有类似的功能,等待某控件出现

textContains("哈哈哈").waitFor();
xxxx某控件.waitFor();

AutoX的waitFor()有些问题,没法实时捕获

我就自己写了

/**
 * 等待至某控件出现
 * @param widget:UiSelector对象,表示需要等待的控件
 * @param timeout:超时时间,单位毫秒,默认60秒
 * @param no_exit:是否不退出程序,默认false
 */
function waitForWidget(widget, timeout, no_exit) {
  if (timeout == null) {
    timeout = 60000;
  }
  if (no_exit == null) {
    no_exit = false;
  }
  if (widget == null) {
    console.log("目标控件不存在,无法等待出现");
    exit();
  }
  let now_time = new Date().getTime();
  while (widget.exists() == false) {
    checkIsReadBook();
    swipe(100, 150, 100, 200, 300);
    swipe(100, 200, 100, 150, 300);
    // 如果时间超过60秒,则退出
    if (new Date().getTime() - now_time > timeout) {
      console.log("等待" + widget + "超时");
      if (no_exit) {
        return false;
      } else {
        console.log("已退出");
        exit();
      }
    }
  }
  return true;
}

/**
 * 等待至某控件消失
 * @param widget:UiSelector对象,表示需要等待的控件
 * @param timeout:超时时间,单位毫秒,默认60秒
 * @param no_exit:是否不退出程序,默认false
 */
function waitForWidgetDisappear(widget, timeout, no_exit) {
  if (timeout == null) {
    timeout = 60000;
  }
  if (no_exit == null) {
    no_exit = false;
  }
  if (widget == null) {
    console.log("目标控件不存在,无法等待消失");
    exit();
  }
  let now_time = new Date().getTime();
  while (widget.exists() == true) {
    checkIsReadBook();
    swipe(100, 150, 100, 200, 300);
    swipe(100, 200, 100, 150, 300);
    // 如果时间超过60秒,则退出
    if (new Date().getTime() - now_time > timeout) {
      console.log("等待" + widget + "超时");
      if (no_exit) {
        return false;
      } else {
        console.log("已退出");
        exit();
      }
    }
  }
  return true;
}

使用swipe能实时刷新控件数据供exists()捕获,和AutoTask无关更具体的原理就不说了,希望作者能够提供类似的功能

Activity

  1. allrobot commented on Jun 6, 2024

    @allrobot
    Author

    前阵子Autox问题频出,打算切到这个Apk尝鲜

    刚体验了一回,一个任务只能有一个if xxx条件: 那么xxx ?长按那么后面添加同级元素,只能添加并且如果,看起来和else if xxx条件作用相当,咋添加多个如果和那么?

    看了下控件条件,只有当前窗口满足控件条件,貌似是不支持等待到某控件出现的那种

    比如我尝试把如果的条件设为进入应用,那么就添加当前页面有满足的控件自动点击某按钮,但是启动应用后并没有点击按钮,只有滑动切换快速切换为目标应用才会自动点击第一个按钮,之后等页面的第二个控件加载后就点击,但是它并没有点击第二个控件

    当前页面有满足的控件应该没做等待至某控件出现再点击的功能

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions