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設(shè)備寬" + width + "\n" + "設(shè)備高" + height + "\n" + "手機型號" + device.model + "\n安卓版本" + device.release)
setScreenMetrics(width, height);
toast("設(shè)備高"+height);
3 無限循環(huán)事件 + 控件查找 + 點擊
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 根據(jù)字符串查找控件, 默認超時timeout=0
// 根據(jù)字符串查找控件, 默認超時timeout=0
function findOneByStr(str, timeout) {
timeout = timeout || 0;
let widget = null;
let isTimeout = false; // 是否超時
let endTime = (timeout > 0) ? (new Date().getTime() + timeout) : -1; // 結(jié)束時間
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);
}
代碼塊之: 連續(xù)滾動20秒
auto.waitFor();
var height = device.height;
var width = device.width;
toast("\n設(shè)備寬" + width + "\n" + "設(shè)備高" + height + "\n" + "手機型號" + device.model + "\n安卓版本" + device.release)
setScreenMetrics(width, height);
toast("設(shè)備高"+height);
autoplay();
function swipe20() {
sleep(1000);
swipe(width / 2, height - 500, width / 2, 0, 500);
}
function autoplay(){
toast("開始做滾動20秒任務(wù),做完后自動返回上一頁.");
sleep(1000);
for (let index = 0; index < 20; index++) {
swipe20();
}
back();
sleep(1000);
toast("結(jié)束");
}
將某個控件轉(zhuǎn)為點 點擊 注意: 如果不是控件則會崩潰
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);
}