berserkjs实例
满昊然
2023-12-01
(function() {
// 检测时间 25000 MS
var oldmem = App.memory();
var DETECT_TIME_OUT = 2500000;
var FLAG = false;
var ACTION_TIME = 2000;
var ACTION_TIME_CELL = 1000;
var ACTION_SEARCH_CELL = 1000;
var repaintInterval;
var w = App.webview;
//序顺执行变量
var ACTION_INDEX_EXPECT = -1;
var ACTION_INDEX = 0;
var action_orders = [];
// 报告类型
var typeList = {
cpu: 'val',
mem: 'val'
};
function _print(obj){
var description = "";
for(var i in obj){
var property=obj[i];
description+=i+" = "+property+"\n";
}
alert(description);
};
function reg_action(func){//注册动作
var tmp = {};
tmp["func"]=func;
var args=Array.prototype.slice.call(arguments,1,arguments.length);
tmp["args"]=args;
tmp["index"]=ACTION_INDEX;
ACTION_INDEX += 1;
action_orders.push(tmp);
};
var ii = 0;
function run_action(){//消费动作
var index = -9999;
var ps = action_orders.shift();
if(ps){
console.log('1111111');
console.log(ps.args);
ps.func.apply(null,ps.args);
index = ps.index;
}
else{
console.log('9999');
}
return index;
};
function run_action_loop(){//循环有序消费
var index = -9999;
//index = run_action();
var lock = 0;
var timeId = setInterval(function() {
console.log('59---'+index+','+ACTION_INDEX_EXPECT);
if(lock==0){
lock=1;//lock code
if(index ==-9999 || index==ACTION_INDEX_EXPECT){
console.log('63---'+index+','+ACTION_INDEX_EXPECT);
index = run_action();
console.log('65---'+index+','+ACTION_INDEX_EXPECT);
}
lock=0;// unlock code
}
}, ACTION_TIME_CELL);
};
function __click(cssString,num){
var flag = 0;
var lock = 0;
var timeId = setInterval(function() {
rect = App.webview.elementRects(cssString);
if (rect != undefined){
if(lock==0){
lock = 1;
clearInterval(timeId);
App.webview.sendMouseEvent({x:rect[0].x,y:rect[0].y});
ACTION_INDEX_EXPECT += 1;
console.log('85---'+ACTION_INDEX_EXPECT);
lock=0;
}
}
else{
console.log('couldn\'t find the '+cssString+' '+flag);
}
flag += 1;
if (flag > num) {
clearInterval(timeId);
// ACTION_INDEX_EXPECT += 1;//let go
}
}, ACTION_TIME_CELL);
};
function _click(cssString){
setTimeout(__click(cssString,60), ACTION_TIME);
};
function click(cssString){
reg_action(_click,cssString);
};
function __inputtext(cssString,stringtext){
var flag = 0;
var lock = 0;
var timeId = setInterval(function() {
rect = App.webview.elementRects(cssString);
if (rect != undefined){
if(lock==0){
lock == 1;
clearInterval(timeId);
App.webview.sendMouseEvent({x:rect[0].x,y:rect[0].y});
App.webview.execScript(function(args) {
document.querySelectorAll(args[0])[0].value = args[1];
}, [cssString,stringtext]);
ACTION_INDEX_EXPECT += 1;
console.log('114---'+ACTION_INDEX_EXPECT);
lock=0;
}
}
else{
console.log('_inputtext couldn\'t find the '+cssString+' '+flag);
}
flag += 1;
if (flag > 60) {
clearInterval(timeId);
// ACTION_INDEX_EXPECT += 1;//let go
}
}, ACTION_TIME_CELL);
};
function _inputtext(cssString,stringtext){
setTimeout(__inputtext(cssString,stringtext), ACTION_TIME);
};
function inputtext(cssString,stringtext){
reg_action(_inputtext,cssString,stringtext);
};
function __move(cssString,num){
var flag = 0;
var lock = 0;
var timeId = setInterval(function() {
rect = App.webview.elementRects(cssString);
if (rect != undefined){
if(lock==0){
lock = 1;
clearInterval(timeId);
App.webview.sendMouseEvent({x:rect[0].x,y:rect[0].y},'mousemove');
ACTION_INDEX_EXPECT += 1;
lock=0;
}
}
else{
console.log('couldn\'t find the '+cssString+' '+flag);
}
flag += 1;
if (flag > num) {
clearInterval(timeId);
// ACTION_INDEX_EXPECT += 1;//let go
}
}, ACTION_TIME_CELL);
};
function _move(cssString){
setTimeout(__move(cssString,60), ACTION_TIME);
};
function move(cssString,stringtext){
reg_action(_move,cssString,stringtext);
};
// 初始化报告对象
var cpu = {},mem = {};
cpu['config'] = {'name':'CPU(%)','interval':1000};
cpu['type'] = 'area';
cpu[typeList.cpu] = [];
mem['config'] = {'name':'Memory(M)','interval':1000};
mem['type'] = 'line';
mem[typeList.mem] = [];
// report['url'] = '';
var eventHandle = {
// 取数据
getNetworkData: function() {
// 收集 cpu 占用每 200 ms 收集一次
dataCollection.cpuNmem();
// 收集25秒用户首页数据
setTimeout(function() {
// 关闭网络监听
FLAG = true;
w.netListener(false);
console.log("report[typeList.cpu]");
// 发送报告
action.sendReport();
App.close();
}, DETECT_TIME_OUT);
},
load:function(){
;
}
};
var action = {
sendReport: function() {
// 将数据POST到远程页面
// console.log(JSON.stringify([cpu,mem]));
App.httpRequest('POST','http://10.1.5.9:4444/charts/postdata', JSON.stringify([cpu,mem]));
// alert(JSON.stringify(report, undefined, 2));
//App.writeFile(App.path + 'test.txt', JSON.stringify(report, undefined, 2));
}
};
var dataCollection = {
cpuNmem: function() {
var i = 0;
var timeId = setInterval(function() {
if (FLAG) {
clearInterval(timeId);
}
if (i > 10){
action.sendReport();
console.log('send......');
cpu[typeList.cpu]=[];
mem[typeList.mem]=[];
}
var t = new Date().getTime();
cpu[typeList.cpu].push([t,App.cpu()]);
mem[typeList.mem].push([t,(App.memory()-oldmem)/1000]);
i += 1;
}, 1000);
}
};
register = {//注册登记页面
map:{'lastname':'#registerApply_text_surname___pc',
'fisrtname':'#registerApply_text_name___pc',
'idcardNO':'#registerApply_text_id___pc',
'mobile':'#registerApply_text_phone___pc',
'save':'#registerApply_Icon_save___pc_label',
'cancel':'registerApply_Icon_cancel___pc_label'
},
run:function(name,idcardNO,mobile){
click(this.map.cancel);
inputtext(this.map.lastname,name.substr(0,1));
inputtext(this.map.fisrtname,name.substr(1,1));
inputtext(this.map.id,idcardNO);
inputtext(this.map.lastname,mobile);
click(this.map.save);
}
};
login = {
map:{'username':"input[name='username']",
'password':"input[name='password']",
'ok':'#dijit_form_Button_0'
},
run:function(username,password){
inputtext(this.map.username,username);
inputtext(this.map.password,password);
click(this.map.ok);
}
};
myTask = {
map:{},//js_table_my-task-row-0.40281187277287245
run:function(){
}
};
function myTaskGrid(cssString){
};
function openNavTask(cssString){
move('#navigation___index');//左侧导航
click(cssString);
};
function task(){
move('.header-container');//banner title
move('#navigation___index');
openNavTask('#dijit__TreeNode_2_label');//点击左侧第二个node
openNavTask('#dijit__TreeNode_3_label');
openNavTask('#dijit__TreeNode_4_label');
openNavTask('#dijit__TreeNode_5_label');
openNavTask('#dijit__TreeNode_7_label');
openNavTask('#dijit__TreeNode_8_label');
openNavTask('#dijit__TreeNode_9_label');
openNavTask('#dijit__TreeNode_11_label');
}
w.setViewport({width: 1024, height: 768});
w.netListener(true);
eventHandle.getNetworkData();
w.addEventListener('load', eventHandle.load);
w.open('http://xxx.com/newsmallLoan/');
run_action_loop();
login.run('002230','123456');
task();
return 0;
})();