-
Notifications
You must be signed in to change notification settings - Fork 10
/
common.js
176 lines (168 loc) · 5.19 KB
/
common.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/**
* 通用自动操作功能
*/
var common = {};
var debug = false;
var find_text_time = 3000;
common.debug = debug;
//消息提示
common.toast_console = function (msg) {
console.log(msg);
if (this.debug)
toast(msg);
}
//按照转换后的坐标点点击
//myx:按照自己手机屏幕获取的点击点的x坐标
//myy:按照自己手机屏幕获取的点击点的y坐标
common.my_click = function (myx, myy) {
let x = parseInt(myx / 1080 * device.width);
let y = parseInt(myy / 2160 * device.height);
click(x, y);
}
//点击控件所在坐标
common.btn_position_click = function (x) {
if (x)
click(x.bounds().centerX(), x.bounds().centerY());
}
// 按文本查找按钮,并点击
common.click_by_text = function (txt) {
let btn = text(txt).findOne(find_text_time);
if (!btn) {
this.toast_console('没找到‘' + txt + '’按钮')
return false;
}
this.toast_console('找到了‘' + txt + '’按钮');
this.btn_position_click(btn);
return true;
}
// 按desc文本查找按钮,并点击
common.click_by_desc = function (txt) {
let btn = desc(txt).findOne(find_text_time);
if (!btn) {
this.toast_console('没找到‘' + txt + '’按钮');
return false;
}
this.toast_console('找到了‘' + txt + '’按钮');
this.btn_position_click(btn);
return true;
}
// 按id文本查找按钮,并点击
common.click_by_id = function (id_str) {
let btn = id(id_str).findOne(find_text_time);
if (!btn) {
this.toast_console('没找到‘' + id_str + '’按钮');
return false;
}
this.toast_console('找到了‘' + id_str + '’按钮');
this.btn_position_click(btn);
return true;
}
// 按文本包含内容查找按钮,并点击
common.click_by_textcontains = function (txt) {
let btn = textContains(txt).findOne(find_text_time);
if (!btn) {
this.toast_console('没找到‘' + txt + '’按钮');
return false;
}
this.toast_console('找到了‘' + txt + '’按钮');
this.btn_position_click(btn);
return true;
}
// 点击区域
common.click_bounds = function (x1, y1, x2, y2) {
x = x1 + (x2 - x1) / 2;
y = y1 + (y2 - y1) / 2;
click(x, y);
}
// 截屏查找图片颜色并单击对应的点
/**
* @param num 重试次数
* @param rgb 颜色值 ‘#112233’
*@param xr,yr,wr,hr, 区域坐标(起点x,起点y,宽,高),小数时,是按照屏幕长宽按照给定的值按照比例动态算出的坐标。
* @param flipup 反转屏幕
*/
common.cs_click = function (num, rgb, xr, yr, wr, hr, flipup) {
while (num--) {
let img = captureScreen();
if (flipup != undefined)
img = images.rotate(img, 180);
if (xr < 1)
var region = [
parseInt(img.getWidth() * xr),
parseInt(img.getHeight() * yr),
parseInt(img.getWidth() * wr),
parseInt(img.getHeight() * hr)
];
else
var region = [
xr, yr, wr, hr
];
let point = findColor(img, rgb,
{
region: region,
threshold: 8
});
if (point) {
if (flipup != undefined) {
point.x = img.getWidth() - point.x;
point.y = img.getHeight() - point.y;
}
return click(point.x, point.y);
}
if (num) sleep(1000);
}
return false;
}
//在屏幕上匹配图片,匹配到就点击
common.find_images = function (num, img_file, flipup, color, region, threshold) {
let templ = images.read(img_file);
//toast_console('color:' + color + ';threshold:' + threshold)
if (color == undefined)
templ = images.grayscale(templ);
while (num--) {
let img = captureScreen();
if (flipup != undefined)
img = images.rotate(img, 180);
if (color == undefined)
img = images.grayscale(img);
if (threshold != undefined)
threshold = 0.9;
else
threshold = threshold;
if (region != undefined)
var point = findImage(img, templ, {
threshold: threshold, //图片相似度
region: region
});
else
var point = findImage(img, templ, {
threshold: threshold //图片相似度
});
if (point) {
if (flipup != undefined) {
point.x = img.getWidth() - point.x; point.y = img.getHeight() - point.y;
}
this.toast_console('找到了‘' + img_file + '’图片');
return click(point.x, point.y);
}
if (num)
sleep(1000);
}
this.toast_console('没找到‘' + img_file + '’图片');
return false;
}
//生成从minNum到maxNum的随机数
common.randomNum = function (minNum, maxNum) {
switch (arguments.length) {
case 1:
return parseInt(Math.random() * minNum + 1, 10);
break;
case 2:
return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
break;
default:
return 0;
break;
}
}
module.exports = common;