auto.js常用语法

1 模拟滚动

// 此滚动方式为向上滚动,很顺滑,1秒滚动一次。
function  swipe() {
        sleep(1000);
        swipe(width / 2, height - 500, width / 2, 0, 500);
}

2 启动

auto.waitFor();
var height = device.height;
var width = device.width;
toast("\n设备宽" + width + "\n" + "设备高" + height + "\n" + "手机型号" + device.model + "\n安卓版本" + device.release)
setScreenMetrics(width, height);
toast("设备高"+height);

3 无限循环事件 + 控件查找 + 点击

function swipe22s(act){
    while(textContains(act).exists()){        
        toast("存在" + act);
        textContains(act).findOne().click();
        sleep(1500);
        swipe(width / 2, height - 500, width / 2, 0, 500);
        sleep(2500);
        swipe(width / 2, height - 500, width / 2, 0, 500);
        sleep(10000);
        swipe(width / 2, height - 500, width / 2, 0, 500);
        sleep(8000);
        if(textContains("完成").exists()){
            back();
        sleep(1000);
if (textContains("忍痛离开").exists()) {
     textContains("忍痛离开").findOne().click();
 }
 if (textContains("晚点再来").exists()) {
     textContains("晚点再来").findOne().click();
 }
        } else {
        sleep(2200);
        back();
        sleep(1000);
if (textContains("忍痛离开").exists()) {
    textContains("忍痛离开").findOne().click();
}
if (textContains("晚点再来").exists()) {
    textContains("晚点再来").findOne().click();
}
        sleep(3000);
        }
        sleep(1600);
    }
    toast("完成[" + act + "]检测");
    sleep(2000);
}

4 根据字符串查找控件, 默认超时timeout=0

// 根据字符串查找控件, 默认超时timeout=0
function findOneByStr(str, timeout) {
    timeout = timeout || 0;
    let widget = null;
    let isTimeout = false;    // 是否超时
    let endTime = (timeout > 0) ? (new Date().getTime() + timeout) : -1;   // 结束时间
    do {
        widget = text(str).findOne(250) || desc(str).findOne(250);
        if (widget) {
            return widget;
        }
        isTimeout = (timeout <= 0) ? false : (new Date().getTime() - endTime > 0);
    } while (!isTimeout || widget != null);
}

代码块之: 连续滚动20秒

auto.waitFor();
var height = device.height;
var width = device.width;
toast("\n设备宽" + width + "\n" + "设备高" + height + "\n" + "手机型号" + device.model + "\n安卓版本" + device.release)
setScreenMetrics(width, height);
toast("设备高"+height);
autoplay();

function  swipe20() {
        sleep(1000);
        swipe(width / 2, height - 500, width / 2, 0, 500);
}
function autoplay(){
    toast("开始做滚动20秒任务,做完后自动返回上一页.");
    sleep(1000);
    for (let index = 0; index < 20; index++) {
         swipe20();
    }
    back();
    sleep(1000);
    toast("结束");
}

将某个控件转为点 点击 注意: 如果不是控件则会崩溃

function clickBoundsBtn(button) {
    let clickBounds = button.bounds();
    click(clickBounds.centerX(), clickBounds.centerY());
    console.log("点击了: ",button);
    sleep(3000);
}

打印要点击的位置,以及信息,然后沉睡多少秒

//打印要点击的位置,以及信息,然后沉睡多少秒
function clickLogWithSleep(clickX,clickY,clickLog,点击前延迟几秒,点击后延迟几秒) {
    log("X: %d , Y: %d 控件log: %s 点击前延迟: %s 秒,点击后延迟: %s 秒",clickX,clickY,clickLog,点击前延迟几秒,点击后延迟几秒);
    sleep(点击前延迟几秒 * 1000);
    click(clickX,clickY);
    sleep(点击后延迟几秒 * 1000);
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 首先附上jQuery官网 http://jquery.com/ 2.0版本开始放弃了对IE7/8及其之前版本的...
    风之帆阅读 715评论 4 2
  • 文档结构 需要使用HTML5文档结构 <!DocType html> 移动设备优先 需要在头部增加 标签 引...
    风之帆阅读 2,562评论 0 4
  • 为了提高程序的健壮性,避免页面还未加载出来时就点击某控件造成的失败,可以使用一下几种solo提供的方法。 1.sl...
    海的那一边阅读 494评论 1 1
  • 连接设备 使用USB连接: d=u2.connect_USB('148b4090')输入手机序列号 d是给当前连接...
    迷迭香_d0e9阅读 7,546评论 0 5
  • 由于appium是扩展了Webdriver协议,所以可以使用webdriver提供的方法,比如在处理webview...
    涛b阅读 1,097评论 0 0