-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevicefarm.js
312 lines (258 loc) · 9.27 KB
/
devicefarm.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// atomic counter for pending ajax calls, used to block periodic timer for extension racing with existing uncomplete processing
var deviceFarmBusy = 0;
// string set for already checked session, used to avoid injecting html elements twice for a test case
var alreadyCheckedSessions = {
__lastLocation: ''
};
function isDeviceFarmTab() {
if (deviceFarmBusy) {
return;
}
try {
var metaTags = document.getElementsByTagName("meta");
var deviceFarmPathFound = window.location.pathname === "/devicefarm/home";
var userLoggedIn = false;
var userIdVisible = false;
for (var i = 0; i < metaTags.length; i++) {
var tag = metaTags[i];
if (
tag.getAttribute('name') === 'adf-user-fullName' &&
tag.getAttribute('content').length > 0
) {
userLoggedIn = true;
}
if (
tag.getAttribute('name') === 'adf-user-accountId' &&
tag.getAttribute('content').length > 0 &&
!isNaN(parseInt(tag.getAttribute('content'))) &&
parseInt(tag.getAttribute('content')) > 0
) {
userIdVisible = true;
}
}
return deviceFarmPathFound && userLoggedIn && userIdVisible;
} catch (error) {
console.error(error);
return false;
}
}
function addTestFairyDeviceFarmIFrame() {
if (deviceFarmBusy) {
// there is an ongoing processing being done, skip this call and give more time for the previous attempt
return;
}
var logsHeader = document.querySelector("#logs-header");
if (!logsHeader) {
// if device farm didn't record logcat, we can't do anything
return;
}
if (document.location.href !== alreadyCheckedSessions.__lastLocation) {
alreadyCheckedSessions = {
__lastLocation: document.location.href
};
}
var modal = injectModal();
var sectionsParent = document.querySelectorAll("#logs-header")[0].parentElement.parentElement.parentElement.parentElement;
var sections = sectionsParent.querySelectorAll(".results-report-section");
var foundSections = {
logsSection: logsHeader.parentElement
};
sections.forEach(function (section) {
switch (section.getAttribute('ui-view')) {
case 'report-files':
foundSections.filesSection = section;
break;
}
});
var testCases = extractTestCases(foundSections.filesSection);
deviceFarmBusy = testCases.length;
testCases.forEach(function (testCase) {
/*
Structure of `testCase`:
{
testSuiteName: string
testName: string,
testFilesSection: DOMElement of <ul></ul> of <a></a> elements
}
*/
extractSessionUrl(testCase.testFilesSection, function (sessionUrl, exceptionFound, minimal) {
var testCanonicalName = testCase.testSuiteName + "." + testCase.testName;
if (sessionUrl && !alreadyCheckedSessions[testCanonicalName]) {
console.error("Session url for " + testCanonicalName + ": " + sessionUrl);
addSessionLinkToSection(modal, testCase, sessionUrl, exceptionFound, minimal);
}
alreadyCheckedSessions[testCanonicalName] = true;
deviceFarmBusy--;
});
});
return;
}
function extractTestCases(filesSection) {
if (!filesSection) {
return [];
}
var testCases = [];
var testCaseSections = filesSection.querySelectorAll('.adf-card > div');
testCaseSections.forEach(function (section) {
// Device Farm has 3 types of details pages:
//
// 1. Instrumentation page (has links to test suites)
// 2. Single test suite page (has links to test cases)
// 3. Single test case page (has a single link to test output files)
//
// This section parser can handle all three cases by checking the selectors below
var testSuiteNameHeader = section.querySelector("h5:first-child"); // if this is null, we are in single test suite details page
var testNameHeader = section.querySelectorAll(".results-report-files h6:first-child"); // if this is null, we are in single test suite's single case results page
var testFiles = section.querySelectorAll(".results-report-files > ul"); // this is always non-empty otherwise we die
if (testFiles.length === 0) {
return;
}
for (var i = 0; i < testNameHeader.length; i++) {
var name = testNameHeader[i];
var files = testFiles[i];
// console.error("Name:");
// console.error(name);
/*
Structure of `testCase`:
{
testSuiteName: string
testName: string,
testFilesSection: DOMElement of <ul></ul> of <a></a> elements
}
*/
testCases.push({
testSuiteName: testSuiteNameHeader ? testSuiteNameHeader.innerText : '',
testName: name ? name.innerText : '',
testFilesSection: files
});
}
if (!testSuiteNameHeader && testNameHeader.length === 0 && testFiles.length === 1) {
// we are in single test suite's single case results page
/*
Structure of `testCase`:
{
testSuiteName: string
testName: string,
testFilesSection: DOMElement of <ul></ul> of <a></a> elements
}
*/
testCases.push({
testSuiteName: '',
testName: '',
testFilesSection: testFiles[0]
});
}
});
return testCases;
}
function extractSessionUrl(testFilesSection, callback) {
if (!testFilesSection) {
callback();
return;
}
var logcat = Array.prototype.slice.call(testFilesSection.querySelectorAll('li > a'))
.filter(function (a) {
return a.textContent === 'Logcat';
})[0];
// console.error("Logcat: ");
// console.error(logcat);
if (!logcat) {
// if device farm didn't record logcat, we can't do anything
callback();
return;
}
var testFairySession = Array.prototype.slice.call(testFilesSection.querySelectorAll('li > a'))
.filter(function (a) {
return a.textContent === 'TestFairy Session';
})[0];
if (testFairySession) {
// if we already injected a <a>TestFairy Session</a>, we can skip
callback();
return;
}
// Fetch and parse logcat to detect session urls
httpGetAsync(logcat.href, function (logs) {
if (!logs) {
// if logcat file is empty or unavailable, there is no hope
callback();
return;
}
// console.error("Logs:");
// console.error(logs.slice(0, 25) + "...");
var lines = logs.split('\n');
var sessionUrl = null;
var exceptionFound = false;
var minimal = true;
for (var i = 0; i < lines.length; i++) {
var line = lines[i];
// This line must match with these:
// https://github.com/testfairy-blog/TestFairyInstrumentationExamples/blob/master/app/src/androidTest/java/com/testfairy/instrumentation/utils/TestFairyInstrumentationUtil.java#L139
// https://github.com/testfairy-blog/TestFairyInstrumentationExamples/blob/master/app/src/androidTest/java/com/testfairy/instrumentation/utils/TestFairyInstrumentationUtil.java#L151
var instrumentationUrlLine = 'Instrumentation session url:';
var exceptionLine = 'TestFairy detected an exception:';
var instrumentationUrlLineIndex = line.indexOf(instrumentationUrlLine);
if (instrumentationUrlLineIndex !== -1) {
sessionUrl = line.slice(instrumentationUrlLineIndex + instrumentationUrlLine.length).trim();
} else {
var testFairySessionReponseIndex = line.search("TESTFAIRYSDK: Received: ");
if (testFairySessionReponseIndex !== -1) {
try {
var sessionResponse = JSON.parse(line.slice(testFairySessionReponseIndex + 23));
if (sessionResponse) {
sessionUrl = sessionResponse.sessionUrl;
minimal = false;
break;
}
} catch(_) {}
}
}
var exceptionLineIndex = line.indexOf(exceptionLine);
if (exceptionLineIndex !== -1) {
exceptionFound = true;
}
}
callback(sessionUrl, exceptionFound, minimal);
});
}
function addSessionLinkToSection(modal, testCase, sessionUrl, exceptionFound, minimal) {
/*
Structure of `testCase`:
{
testSuiteName: string
testName: string,
testFilesSection: DOMElement of <ul></ul> of <a></a> elements
}
*/
var testCanonicalName = testCase.testSuiteName + "." + testCase.testName;
// console.error("Injecting TestFairy Session url into files section for " + testCanonicalName);
var sessionLink = createA("TestFairy Session", sessionUrl);
testCase.testFilesSection.appendChild(createLi(sessionLink));
// // The code below showcases how to convert a link into a modal popup with iFrame
// sessionLink.onclick = function(e) {
// e.preventDefault();
// modal.show();
// modal.clear();
//
// var iFrame = createIFrame(sessionUrl + "?iframe", 'testfairy-iframe', 'TestFairy Session', '100%', 'auto', 'scroll !important');
// modal.addContent(iFrame);
//
// modal.element.onclick = function(e) {
// e.preventDefault();
// modal.hide();
// modal.element.onclick = undefined;
// };
// }
var throwablesUrl = sessionUrl + "/minimal?source=devicefarm&widget=throwables&ui=true";
if (!minimal) {
throwablesUrl = convertSessionUrlToIFrameUrl(sessionUrl, "source=devicefarm");
}
if (exceptionFound || !minimal) {
testCase.testFilesSection.appendChild(createLiWithText('-'));
// testCase.testFilesSection.appendChild(createLi(createA('Stacktrace', throwablesUrl)));
testCase.testFilesSection.appendChild(createLi(createIFrame(throwablesUrl, !minimal ? getTestFairyCommonIFrameId() : null, null, '100%', '700', 'scroll !important')));
var sectionContainer = testCase.testFilesSection.parentElement.parentElement;
sectionContainer.setAttribute('class', sectionContainer.getAttribute('class').replace('large-4', 'large-12'));
sectionContainer.setAttribute('class', sectionContainer.getAttribute('class').replace('large-3', 'large-12'));
sectionContainer.parentElement.setAttribute('class', sectionContainer.parentElement.getAttribute('class').replace('large-4', 'large-12'));
}
}