-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplanga-appium.js
206 lines (172 loc) · 5.24 KB
/
applanga-appium.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
var parseStringPromise = require('xml2js').parseStringPromise;
var fs = require('fs');
var axios = require('axios');
var sizeOf = require('image-size');
const apiUrl = 'https://api.applanga.com/v1/api/screenshots?app=';
async function captureScreenshot(driver, tagName, applangaJsonPath) {
apiToken = getAPIToken(applangaJsonPath);
appId = getAppID(applangaJsonPath);
let screenshotLocation = __dirname + '/tmpScreen.png';
try {
screenshot = await driver.takeScreenshot();
await fs.promises.writeFile(screenshotLocation, screenshot, 'base64');
} catch (error) {
console.log('Error ocurred while saving screenshot: ' + error);
}
const screenshotDimensions = sizeOf(screenshotLocation);
let xml = await driver.getPageSource();
let parsedXml = await parseStringPromise(xml);
let ratio = await getRatio(driver, screenshotDimensions);
let positions = []
await getAllTextOnScreen(driver, parsedXml, ratio, positions);
await doUpload(
driver,
tagName,
apiToken,
appId,
positions,
screenshotLocation
);
}
function getDeviceLanguageLong(client) {
let country = client.isAndroid
? client.capabilities.desired.locale
: client.capabilities.locale;
let language = client.isAndroid
? client.capabilities.desired.language
: client.capabilities.language;
if (!country) return language;
else return language + '-' + country;
}
async function getAllTextOnScreen(driver, object, ratio, positions) {
for (var key in object) {
if (object.hasOwnProperty(key)) {
let value = object[key];
if (typeof value == 'object') {
await getAllTextOnScreen(driver, value, ratio, positions);
}
var text;
for (let i = 0; i < value.length; i++) {
if (value[i]['$'] === undefined) continue;
if (driver.isAndroid) {
text = value[i]['$']['text'];
} else {
text = value[i]['$']['label'];
}
if (text !== undefined && text != null && text !== '') {
await getTextPositions(value[i], text, ratio, positions);
}
}
}
}
}
async function getRatio(driver, screenshotDimensions) {
var ratio = 1;
if (!driver.isAndroid) {
// get the ratio for iOS devices
// get screen size
const screen = await driver.$(`//XCUIElementTypeApplication`);
let screenPosition = await screen.getLocation();
let screenSize = await screen.getSize();
if (screenPosition.x != 0 || screenPosition.y != 0) {
console.error(
'Screen position is not 0,0. This is not supported by Applanga. Please make sure your app is fullscreen.'
);
}
ratio = screenshotDimensions.width / screenSize.width;
}
return ratio;
}
async function getTextPositions(object, textValue, ratio, positions) {
let values = object['$'];
let x = values['x'];
let y = values['y'];
let width = values['width'];
let height = values['height'];
let regex = /\[([0-9]+),([0-9]+)\]\[([0-9]+),([0-9]+)\]/g;
let matches = regex.exec(values.bounds);
if(matches !== null && matches !== undefined && matches.length == 5){
x = matches[1];
y = matches[2];
width = matches[3] - x ;
height = matches[4] - y;
}
positions.push({
text: textValue,
x: x * ratio,
y: y * ratio,
width: width * ratio,
height: height * ratio,
});
}
async function doUpload(
client,
tagName,
apiToken,
appId,
stringPositions,
imageLocation
) {
let screenSize = await client.getWindowRect();
let opts = client.capabilities;
var data = {
screenTag: tagName,
width: screenSize.width,
height: screenSize.height,
deviceModel: opts.deviceModel || 'appium',
platform: opts.platformName,
operatingSystem: opts.platformVersion || opts.platform,
bundleVersion: 1,
deviceLanguageLong: getDeviceLanguageLong(client),
isBlank: false,
useOCR: false,
stringPositions: stringPositions,
useFuzzyMatching: false
};
const form = {
data: JSON.stringify(data),
file: fs.createReadStream(imageLocation),
};
const headers = {
Authorization: apiToken,
'Content-Type': 'multipart/form-data',
'X-Integration': 12
};
const fullUrl = apiUrl + appId;
try {
let res = await axios.post(fullUrl, form, { headers: headers });
if (res.status != 200) {
console.error(
'Applanga Screenshot upload failed with status code: ',
res.status + ', msg: ' + res.data.message
);
} else {
console.log('Applanga Screenshot Upload successful!');
}
} catch (err) {
console.error('Applanga Screenshot upload failed:', err);
}
}
// returns the api token in the format "Bearer <token>"
function getAPIToken(path) {
if (!path) path = '';
let applangaJson = JSON.parse(fs.readFileSync(path + '.applanga.json'));
let apiKey = applangaJson.app.access_token;
if (apiKey.length != 57) {
console.log('Invalid Applanga API token.');
return;
}
return 'Bearer ' + apiKey;
}
function getAppID(path) {
let token = getAPIToken(path);
// remove Bearer from token
let str = token.substring('Bearer '.length);
if (str.charAt(24) != '!') {
console.log('Invalid Applanga AppID, missing exclamation mark.');
return;
}
var appId = str.substring(0, 24);
return appId;
}
module.exports = { captureScreenshot };