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);
}
网友评论