forked from SideeX/sideex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayback-api.js
More file actions
executable file
·716 lines (645 loc) · 22.8 KB
/
playback-api.js
File metadata and controls
executable file
·716 lines (645 loc) · 22.8 KB
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
/*
* Copyright 2017 SideeX committers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
var playingTabIds = {};
var playingTabNames = {};
var playingTabCount = 1;
var currentPlayingFrameLocation = "root";
var currentPlayingCommandIndex = -1;
var currentTestCaseId = "";
var isPause = false;
var pauseValue = null;
var isPlayingSuite = false;
var isPlayingAll = false;
var selectTabId = null;
var isSelecting = false;
var commandType = "";
var pageCount = 0;
var pageTime = "";
var ajaxCount = 0;
var ajaxTime = "";
var domCount = 0;
var domTime = "";
var implicitCount = 0;
var implicitTime = "";
var caseFailed = false;
var extCommand = new ExtCommand();
window.onload = function() {
var recordButton = document.getElementById("record");
var playButton = document.getElementById("playback");
var stopButton = document.getElementById("stop");
var pauseButton = document.getElementById("pause");
var resumeButton = document.getElementById("resume");
var playSuiteButton = document.getElementById("playSuite");
var playSuitesButton = document.getElementById("playSuites");
var showElementButton = document.getElementById("showElementButton")
var selectElementButton = document.getElementById("selectElementButton");
/*var recordButton = document.getElementById("record");*/
//element.addEventListener("click",play);
recordButton.addEventListener("click", function(){
isRecording = !isRecording;
if (isRecording) {
notificationCount = 0;
recordButton.childNodes[1].textContent = "Stop";
}
else {
recordButton.childNodes[1].textContent = "Record";
}
})
playButton.addEventListener("click", function() {
document.getElementById("result-runs").innerHTML = "0";
document.getElementById("result-failures").innerHTML = "0";
initAllSuite();
setCaseScrollTop(getSelectedCase());
play();
});
stopButton.addEventListener("click", function() {
stop();
});
pauseButton.addEventListener("click", pause);
pauseButton.disabled = true;
resumeButton.addEventListener("click", resume);
playSuiteButton.addEventListener("click", function() {
document.getElementById("result-runs").innerHTML = "0";
document.getElementById("result-failures").innerHTML = "0";
initAllSuite();
playSuite(0);
});
playSuitesButton.addEventListener("click", function() {
document.getElementById("result-runs").innerHTML = "0";
document.getElementById("result-failures").innerHTML = "0";
initAllSuite();
playSuites(0);
});
selectElementButton.addEventListener("click",function(){
var button = document.getElementById("selectElementButton");
if (isSelecting) {
isSelecting = false;
button.textContent = "Select";
browser.tabs.query({
active: true,
windowId: contentWindowId
}).then(function(tabs) {
browser.tabs.sendMessage(tabs[0].id, {selectMode: true, selecting: false});
}).catch(function(reason) {
console.log(reason);
})
return;
}
isSelecting = true;
if (isRecording)
/* TODO: disable record button */
isRecording = false;
button.textContent = "Cancel";
browser.tabs.query({
active: true,
windowId: contentWindowId
}).then(function(tabs) {
if (tabs.length === 0) {
console.log("No match tabs");
isSelecting = false;
button.textContent = "Select";
} else
browser.tabs.sendMessage(tabs[0].id, {selectMode: true, selecting: true});
})
});
showElementButton.addEventListener("click", function(){
try{
var targetValue = document.getElementById("command-target").value;
browser.tabs.query({
active: true,
windowId: contentWindowId
}).then(function(tabs) {
if (tabs.length === 0) {
console.log("No match tabs");
} else {
browser.tabs.sendMessage(tabs[0].id, {
showElement: true,
targetValue: targetValue
}).then(function(response) {
if (response){
console.log(response.result);
}
});
}
});
} catch (e) {
console.error(e);
}
});
/*recordButton.addEventListener("click", startRecord);*/
//console.error(recordButton);
};
function disableClick() {
document.getElementById("pause").disabled = false;
document.getElementById('testCase-grid').style.pointerEvents = 'none';
document.getElementById('command-container').style.pointerEvents = 'none';
}
function enableClick() {
document.getElementById("pause").disabled = true;
document.getElementById('testCase-grid').style.pointerEvents = 'auto';
document.getElementById('command-container').style.pointerEvents = 'auto';
}
function play() {
initializePlayingProgress()
.then(executionLoop)
.then(finalizePlayingProgress)
.catch(catchPlayingError);
}
function stop() {
if (isPause){
resume();
}
isPlaying = false;
isPlayingSuite = false;
isPlayingAll = false;
switchPS();
sideex_log.info("Stop executing");
initAllSuite();
document.getElementById("result-runs").innerHTML = "0";
document.getElementById("result-failures").innerHTML = "0";
finalizePlayingProgress();
}
function playAfterConnectionFailed() {
initializeAfterConnectionFailed()
.then(executionLoop)
.then(finalizePlayingProgress)
.catch(catchPlayingError);
}
function initializeAfterConnectionFailed() {
disableClick();
isRecording = false;
isPlaying = true;
commandType = "preparation";
pageCount = ajaxCount = domCount = implicitCount = 0;
pageTime = ajaxTime = domTime = implicitTime = "";
caseFailed = false;
currentTestCaseId = getSelectedCase().id;
var commands = getRecordsArray();
return Promise.resolve(true);
}
function pause() {
if (isPlaying) {
sideex_log.info("Pausing");
isPause = true;
switchPR();
}
}
function resume() {
if(currentTestCaseId!=getSelectedCase().id)
setSelectedCase(currentTestCaseId);
if (isPause) {
sideex_log.info("Resuming");
isPlaying = true;
isPause = false;
switchPR();
disableClick();
executionLoop()
.then(finalizePlayingProgress)
.catch(catchPlayingError);
}
}
function initAllSuite() {
var suites = document.getElementById("testCase-grid").getElementsByClassName("message");
var length = suites.length;
for (var k = 0; k < suites.length; ++k) {
var cases = suites[k].getElementsByTagName("p");
for (var u = 0; u < cases.length; ++u) {
$("#" + cases[u].id).removeClass('fail success');
}
}
}
function playSuite(i) {
isPlayingSuite = true;
var cases = getSelectedSuite().getElementsByTagName("p");
var length = cases.length;
if (i == 0) {
sideex_log.info("Playing test suite " + sideex_testSuite[getSelectedSuite().id].title);
}
if (i < length) {
setSelectedCase(cases[i].id);
setCaseScrollTop(getSelectedCase());
sideex_log.info("Playing test case " + sideex_testCase[cases[i].id].title);
play();
nextCase(i);
} else {
isPlayingSuite = false;
switchPS();
}
}
function nextCase(i) {
if (isPlaying || isPause) setTimeout(function() {
nextCase(i);
}, 500);
else if(isPlayingSuite) playSuite(i + 1);
}
function playSuites(i) {
isPlayingAll = true;
var suites = document.getElementById("testCase-grid").getElementsByClassName("message");
var length = suites.length;
if (i < length) {
if (suites[i].id.includes("suite")) {
setSelectedSuite(suites[i].id);
playSuite(0);
}
nextSuite(i);
} else {
isPlayingAll = false;
switchPS();
}
}
function nextSuite(i) {
if (isPlayingSuite) setTimeout(function() {
nextSuite(i);
}, 2000);
else if(isPlayingAll) playSuites(i + 1);
}
function executeCommand(index) {
var id = parseInt(index) - 1;
var commands = getRecordsArray();
var commandName = getCommandName(commands[id]);
var commandTarget = getCommandTarget(commands[id]);
var commandValue = getCommandValue(commands[id]);
sideex_log.info("Executing: | " + commandName + " | " + commandTarget + " | " + commandValue + " |");
initializePlayingProgress(true);
setColor(id + 1, "executing");
browser.tabs.query({
windowId: extCommand.getContentWindowId(),
active: true
})
.then(function(tabs) {
return browser.tabs.sendMessage(tabs[0].id, {
commands: commandName,
target: commandTarget,
value: commandValue
}, {
frameId: extCommand.getFrame(tabs[0].id)
})
})
.then(function(result) {
if (result.result != "success") {
sideex_log.error(result.result);
setColor(id + 1, "fail");
if (!result.result.includes("did not match")) {
return true;
}
} else {
setColor(id + 1, "success");
}
})
finalizePlayingProgress();
}
function onError(error) {
console.log(error);
}
function cleanStatus() {
var commands = getRecordsArray();
for (var i = 0; i < commands.length; ++i) {
commands[i].setAttribute("class", "");
}
classifyRecords(1, commands.length);
}
function initializePlayingProgress(isDbclick) {
disableClick();
isRecording = false;
isPlaying = true;
switchPS();
currentPlayingCommandIndex = -1;
// xian wait
pageCount = ajaxCount = domCount = implicitCount = 0;
pageTime = ajaxTime = domTime = implicitTime = "";
caseFailed = false;
currentTestCaseId = getSelectedCase().id;
if (!isDbclick) {
$("#" + currentTestCaseId).removeClass('fail success');
}
var commands = getRecordsArray();
cleanStatus();
return extCommand.init();
}
function executionLoop() {
if (!isPlaying) {
cleanStatus();
return false;
}
if (isPause) {
return true;
}
currentPlayingCommandIndex++;
let commands = getRecordsArray();
if (currentPlayingCommandIndex >= commands.length) {
if (!caseFailed) {
setColor(currentTestCaseId, "success");
document.getElementById("result-runs").innerHTML = parseInt(document.getElementById("result-runs").innerHTML) + 1;
declaredVars = {};
sideex_log.info("Test case passed");
} else {
caseFailed = false;
}
return true;
}
let commandName = getCommandName(commands[currentPlayingCommandIndex]);
let commandTarget = getCommandTarget(commands[currentPlayingCommandIndex]);
let commandValue = getCommandValue(commands[currentPlayingCommandIndex]);
setColor(currentPlayingCommandIndex + 1, "executing");
if (isExtCommand(commandName)) {
sideex_log.info("Executing: | " + commandName + " | " + commandTarget + " | " + commandValue + " |");
let upperCase = commandName.charAt(0).toUpperCase() + commandName.slice(1);
return (extCommand["do" + upperCase](commandTarget, commandValue))
.then(function() {
setColor(currentPlayingCommandIndex + 1, "success");
}).then(executionLoop);
} else {
return doPreparation()
.then(doPrePageWait)
.then(doPageWait)
.then(doAjaxWait)
.then(doDomWait)
.then(doCommand)
.then(executionLoop)
}
}
function finalizePlayingProgress() {
enableClick();
playingTabIds = {};
playingTabNames = {};
playingTabCount = 1;
//console.log("success");
setTimeout(function() {
isPlaying = false;
//isRecording = true;
switchPS();
}, 500);
}
document.addEventListener("dblclick", function(event) {
var temp = event.target;
while (temp.tagName.toLowerCase() != "body") {
if (/records-(\d)+/.test(temp.id)) {
var index = temp.id.split("-")[1];
executeCommand(index);
}
if (temp.id == "command-grid") {
break;
} else temp = temp.parentElement;
}
});
function playDisable(setting) {
if (setting)
document.getElementById("record").childNodes[1].textContent = "Record";
document.getElementById("record").disabled = setting;
document.getElementById("playback").disabled = setting;
document.getElementById("playSuite").disabled = setting;
document.getElementById("playSuites").disabled = setting;
}
function switchPS() {
if ((isPlaying||isPause)||isPlayingSuite||isPlayingAll) {
playDisable(true);
document.getElementById("playback").style.display = "none";
document.getElementById("stop").style.display = "";
} else {
playDisable(false);
document.getElementById("playback").style.display = "";
document.getElementById("stop").style.display = "none";
}
}
function switchPR() {
if (isPause) {
// playDisable(true);
document.getElementById("pause").style.display = "none";
document.getElementById("resume").style.display = "";
} else {
// playDisable(false);
document.getElementById("pause").style.display = "";
document.getElementById("resume").style.display = "none";
}
}
function catchPlayingError(reason) {
// doCommands is depend on test website, so if make a new page,
// doCommands funciton will fail, so keep retrying to get connection
if (isReceivingEndError(reason)) {
commandType = "preparation";
setTimeout(function() {
currentPlayingCommandIndex--;
playAfterConnectionFailed();
}, 100);
} else {
enableClick();
sideex_log.error(reason);
if (currentPlayingCommandIndex == -1) {
currentPlayingCommandIndex++;
}
setColor(currentPlayingCommandIndex + 1, "fail");
setColor(currentTestCaseId, "fail");
document.getElementById("result-failures").innerHTML = parseInt(document.getElementById("result-failures").innerHTML) + 1;
sideex_log.info("Test case failed");
playingTabIds = {};
playingTabNames = {};
playingTabCount = 1;
/* Clear the flag, reset to recording phase */
/* A small delay for preventing recording events triggered in playing phase*/
setTimeout(function() {
isPlaying = false;
//isRecording = true;
switchPS();
}, 500);
}
}
function doPreparation() {
//console.log("in preparation");
return extCommand.sendMessage("waitPreparation", "", "")
.then(function() {
return true;
})
}
function doPrePageWait() {
//console.log("in prePageWait");
return extCommand.sendMessage("prePageWait", "", "")
.then(function(response) {
if (response && response.new_page) {
//console.log("prePageWaiting");
return doPrePageWait();
} else {
return true;
}
})
}
function doPageWait() {
//console.log("in pageWait");
return extCommand.sendMessage("pageWait", "", "")
.then(function(response) {
if (pageTime && (Date.now() - pageTime) > 30000) {
sideex_log.error("Page Wait timed out after 30000ms");
pageCount = 0;
pageTime = "";
return true;
} else if (response && response.page_done) {
pageCount = 0;
pageTime = "";
return true;
} else {
pageCount++;
if (pageCount == 1) {
pageTime = Date.now();
sideex_log.info("Wait for the new page to be fully loaded");
}
return doPageWait();
}
})
}
function doAjaxWait() {
//console.log("in ajaxWait");
return extCommand.sendMessage("ajaxWait", "", "")
.then(function(response) {
if (ajaxTime && (Date.now() - ajaxTime) > 30000) {
sideex_log.error("Ajax Wait timed out after 30000ms");
ajaxCount = 0;
ajaxTime = "";
return true;
} else if (response && response.ajax_done) {
ajaxCount = 0;
ajaxTime = "";
return true;
} else {
ajaxCount++;
if (ajaxCount == 1) {
ajaxTime = Date.now();
sideex_log.info("Wait for all ajax requests to be done");
}
return doAjaxWait();
}
})
}
function doDomWait() {
//console.log("in domWait");
return extCommand.sendMessage("domWait", "", "")
.then(function(response) {
if (domTime && (Date.now() - domTime) > 30000) {
sideex_log.error("DOM Wait timed out after 30000ms");
domCount = 0;
domTime = "";
return true;
} else if (response && (Date.now() - response.dom_time) < 400) {
domCount++;
if (domCount == 1) {
domTime = Date.now();
sideex_log.info("Wait for the DOM tree modification");
}
return doDomWait();
} else {
domCount = 0;
domTime = "";
return true;
}
})
}
function doCommand() {
let commands = getRecordsArray();
let commandName = getCommandName(commands[currentPlayingCommandIndex]);
let commandTarget = getCommandTarget(commands[currentPlayingCommandIndex]);
let commandValue = getCommandValue(commands[currentPlayingCommandIndex]);
//console.log("in common");
if (implicitCount == 0) {
sideex_log.info("Executing: | " + commandName + " | " + commandTarget + " | " + commandValue + " |");
}
let p = new Promise(function(resolve, reject) {
let count = 0;
let interval = setInterval(function() {
if (count > 60) {
sideex_log.error("Timed out after 30000ms");
reject("Window not Found");
clearInterval(interval);
}
if (!extCommand.getPageStatus()) {
if (count == 0) {
sideex_log.info("Wait for the new page to be fully loaded");
}
count++;
} else {
resolve();
clearInterval(interval);
}
}, 500);
});
return p.then(function() {
if(commandValue.substr(0,2) === "${" && commandValue.substr(commandValue.length-1) === "}"){
commandValue = xlateArgument(commandValue);
}
if(commandTarget.substr(0,2) === "${" && commandTarget.substr(commandTarget.length-1) === "}"){
commandTarget = xlateArgument(commandTarget);
}
if (isWindowMethodCommand(commandName))
{
return extCommand.sendMessage(commandName, commandTarget, commandValue, true);
}
return extCommand.sendMessage(commandName, commandTarget, commandValue);
})
.then(function(result) {
if (result.result != "success") {
// implicit
if (result.result.match(/Element[\s\S]*?not found/)) {
if (implicitTime && (Date.now() - implicitTime > 30000)) {
sideex_log.error("Implicit Wait timed out after 30000ms");
implicitCount = 0;
implicitTime = "";
} else {
implicitCount++;
if (implicitCount == 1) {
sideex_log.info("Wait until the element is found");
implicitTime = Date.now();
}
return doCommand();
}
}
implicitCount = 0;
implicitTime = "";
sideex_log.error(result.result);
setColor(currentPlayingCommandIndex + 1, "fail");
setColor(currentTestCaseId, "fail");
document.getElementById("result-failures").innerHTML = parseInt(document.getElementById("result-failures").innerHTML) + 1;
if (commandName.includes("verify") && result.result.includes("did not match")) {
setColor(currentPlayingCommandIndex + 1, "fail");
} else {
sideex_log.info("Test case failed");
caseFailed = true;
currentPlayingCommandIndex = commands.length;
}
} else {
setColor(currentPlayingCommandIndex + 1, "success");
}
})
}
function isReceivingEndError(reason) {
if (reason == "TypeError: response is undefined" ||
reason == "Error: Could not establish connection. Receiving end does not exist." ||
// Below message is for Google Chrome
reason.message == "Could not establish connection. Receiving end does not exist." ||
// Google Chrome misspells "response"
reason.message == "The message port closed before a reponse was received." ||
reason.message == "The message port closed before a response was received." )
return true;
return false;
}
function isWindowMethodCommand(command) {
if (command == "answerOnNextPrompt"
|| command == "chooseCancelOnNextPrompt"
|| command == "assertPrompt"
|| command == "chooseOkOnNextConfirmation"
|| command == "chooseCancelOnNextConfirmation"
|| command == "assertConfirmation"
|| command == "assertAlert")
return true;
return false;
}