-
Notifications
You must be signed in to change notification settings - Fork 78
/
ios.js
104 lines (84 loc) · 2.43 KB
/
ios.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
'use strict';
const fs = require('fs');
const path = require('path');
const logger = require('./common/logger');
const XCTest = require('xctest-client');
const Simulator = require('ios-simulator');
const pkg = require('../package.json');
const chalk = require('chalk');
const _ = require('./common/helper');
function reportNodesError(source) {
return chalk.red(
`The source may be wrong, please report with below message at:
${chalk.blue(pkg.bugs)}
****** xctest source start *******
${JSON.stringify(JSON.parse(source))}
'****** xctest source end *******`
);
}
const adaptor = function(node) {
node.class = node.type;
const rect = node.rect;
node.bounds = [
rect.origin.x,
rect.origin.y,
rect.size.width,
rect.size.height,
];
if (node.children) {
const children = node.children.length ? node.children : [node.children];
var nodes = [];
children.forEach(child => {
if (parseInt(child.isVisible, 10) || child.type !== 'Window') {
nodes.push(adaptor(child));
}
});
node.nodes = nodes;
delete node.children;
}
return node;
};
var xctest;
exports.dumpXMLAndScreenShot = function *() {
const source = yield _.request(`http://${xctest.proxyHost}:${xctest.proxyPort}/source`, 'get', {});
const tree = JSON.parse(source).value.tree;
const tempDir = path.join(__dirname, '..', '.temp');
_.mkdir(tempDir);
const xmlFilePath = path.join(tempDir, 'ios.json');
let compatibleTree;
try {
compatibleTree = adaptor(tree);
} catch(e) {
console.error(reportNodesError(source));
throw e;
}
fs.writeFileSync(xmlFilePath, JSON.stringify(compatibleTree), 'utf8');
logger.debug(`Dump iOS XML success, save to ${xmlFilePath}`);
const screenshot = yield _.request(`http://${xctest.proxyHost}:${xctest.proxyPort}/screenshot`, 'get', {});
const base64Data = JSON.parse(screenshot).value;
const imgFilePath = path.join(tempDir, 'ios-screenshot.png');
fs.writeFileSync(imgFilePath, base64Data, 'base64');
};
exports.initDevice = function *(udid) {
const isRealIOS = _.getDeviceInfo(udid).isRealIOS;
var device;
if (isRealIOS) {
device = {
deviceId: udid
};
} else {
device = new Simulator({
deviceId: udid
});
}
xctest = new XCTest({
device: device
});
yield xctest.start({
desiredCapabilities: {}
});
if (isRealIOS) {
yield _.sleep(15 * 1000);
}
logger.info(`iOS device started: ${udid}`);
};