-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathuitest-mocha-shim.js
157 lines (140 loc) · 4.18 KB
/
uitest-mocha-shim.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
/* eslint-disable */
'use strict';
; (function () {
function getUUID() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
.replace(/[xy]/g, function (c) {
const r = Math.random() * 16 | 0;
const v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
if (!window.__execCommand) {
window.__execCommand = async () => { };
}
window._macaca_uitest = {
mouse: {
click(x, y, opt) {
return window.__execCommand('mouse', 'click', x, y, opt);
},
dblclick(x, y, opt) {
return window.__execCommand('mouse', 'dblclick', x, y, opt);
},
move(x, y, opt) {
return window.__execCommand('mouse', 'move', x, y, opt);
},
down(opt) {
return window.__execCommand('mouse', 'down', opt);
},
up(opt) {
return window.__execCommand('mouse', 'up', opt);
},
wheel(opt) {
return window.__execCommand('mouse', 'wheel', x, y, opt);
}
},
keyboard: {
type(str, opt) {
return window.__execCommand('keyboard', 'type', str, opt);
},
down(key) {
return window.__execCommand('keyboard', 'down', key);
},
up(key) {
return window.__execCommand('keyboard', 'up', key);
},
insertText(text) {
return window.__execCommand('keyboard', 'insertText', text);
},
press(key, opt) {
return window.__execCommand('keyboard', 'press', key, opt);
}
},
page: {
newPage(url) {
return window.__execCommand('newPage', url);
},
close(pageId) {
return window.__execCommand('closePage', pageId);
},
waitForSelector(pageId, selector) {
return window.__execCommand('waitForSelector', pageId, selector);
},
waitForEvent(pageId, eventName) {
return window.__execCommand('waitForEvent', pageId, eventName);
},
exec(pageId, func) {
return window.__execCommand('runInPage', pageId, `(${func.toString()})()`);
},
},
fileChooser(filePath) {
return window.__execCommand('fileChooser', filePath);
},
switchScene() {
const args = Array.prototype.slice.call(arguments);
return window.__execCommand('switchScene', args[0]);
},
switchAllScenes() {
const args = Array.prototype.slice.call(arguments);
return window.__execCommand('switchAllScenes', args[0]);
},
saveVideo(context) {
return new Promise((resolve, reject) => {
window.__execCommand('getVideoName').then(name => {
// 失败后直接返回
if (!name) return resolve(name);
const filePath = `./screenshots/${name}`;
this.appendToContext(context, filePath);
resolve(filePath);
}).catch(e => resolve(null));
});
},
saveScreenshot(context) {
return new Promise((resolve, reject) => {
const name = `${getUUID()}.png`;
const filePath = `./screenshots/${name}`;
this.appendToContext(context, filePath);
resolve(this.screenshot(name));
});
},
screenshot(name) {
const filePath = `./reports/screenshots/${name}`;
return window.__execCommand('screenshot', filePath);
},
appendToContext(mocha, content) {
try {
const test = mocha.currentTest || mocha.test;
if (!test.context) {
test.context = content;
} else if (Array.isArray(test.context)) {
test.context.push(content);
} else {
test.context = [test.context];
test.context.push(content);
}
} catch (e) {
console.log('error', e);
}
},
setup(options) {
let mochaOptions = options;
mochaOptions = Object.assign({}, options, {
reporter: 'spec',
useColors: true
});
return mocha.setup(mochaOptions);
},
run() {
return mocha.run(function (failedCount) {
const __coverage__ = window.__coverage__;
if (__coverage__) {
window.__execCommand('saveCoverage', __coverage__);
}
// delay to exit
setTimeout(() => {
window.__execCommand('exit', { failedCount });
}, 200);
});
}
};
})();