-
Notifications
You must be signed in to change notification settings - Fork 104
/
recorder.js
1107 lines (938 loc) · 36.2 KB
/
recorder.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
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
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//----------------------------------------------------------------------------
//Copyright (c) 2005 Zope Foundation and Contributors.
//This software is subject to the provisions of the Zope Public License,
//Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
//THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
//WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
//WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
//FOR A PARTICULAR PURPOSE.
//TestRecorder - a javascript library to support browser test recording. It
//is designed to be as cross-browser compatible as possible and to promote
//loose coupling with the user interface, making it easier to evolve the UI
//and experiment with alternative interfaces.
//caveats: popup windows undefined, cant handle framesets
//todo:
//- capture submit (w/lookback for doctest)
//- cleanup strings
//Contact Brian Lloyd (brian@zope.com) with questions or comments.
//---------------------------------------------------------------------------
if (typeof(TestRecorder) == "undefined") {
TestRecorder = {};
}
//---------------------------------------------------------------------------
//Browser -- a singleton that provides a cross-browser API for managing event
//handlers and miscellaneous browser functions.
//Methods:
//captureEvent(window, name, handler) -- capture the named event occurring
//in the given window, setting the function handler as the event handler.
//The event name should be of the form "click", "blur", "change", etc.
//releaseEvent(window, name, handler) -- release the named event occurring
//in the given window. The event name should be of the form "click", "blur",
//"change", etc.
//getSelection(window) -- return the text currently selected, or the empty
//string if no text is currently selected in the browser.
//---------------------------------------------------------------------------
if (typeof(TestRecorder.Browser) == "undefined") {
TestRecorder.Browser = {};
}
TestRecorder.Browser.captureEvent = function(wnd, name, func) {
var lname = name.toLowerCase();
var doc = wnd.document;
wnd.captureEvents(Event[name.toUpperCase()]);
wnd["on" + lname] = func;
}
TestRecorder.Browser.releaseEvent = function(wnd, name, func) {
var lname = name.toLowerCase();
var doc = wnd.document;
wnd.releaseEvents(Event[name.toUpperCase()]);
wnd["on" + lname] = null;
}
TestRecorder.Browser.getSelection = function(wnd) {
var doc = wnd.document;
if (wnd.getSelection) {
return wnd.getSelection() + "";
}
else if (doc.getSelection) {
return doc.getSelection() + "";
}
else if (doc.selection && doc.selection.createRange) {
return doc.selection.createRange().text + "";
}
return "";
}
TestRecorder.Browser.windowHeight = function(wnd) {
var doc = wnd.document;
if (wnd.innerHeight) {
return wnd.innerHeight;
}
else if (doc.documentElement && doc.documentElement.clientHeight) {
return doc.documentElement.clientHeight;
}
else if (document.body) {
return document.body.clientHeight;
}
return -1;
}
TestRecorder.Browser.windowWidth = function(wnd) {
var doc = wnd.document;
if (wnd.innerWidth) {
return wnd.innerWidth;
}
else if (doc.documentElement && doc.documentElement.clientWidth) {
return doc.documentElement.clientWidth;
}
else if (document.body) {
return document.body.clientWidth;
}
return -1;
}
//---------------------------------------------------------------------------
//Event -- a class that provides a cross-browser API dealing with most of the
//interesting information about events.
//Methods:
//type() -- returns the string type of the event (e.g. "click")
//target() -- returns the target of the event
//button() -- returns the mouse button pressed during the event. Because
//it is not possible to reliably detect a middle button press, this method
//only recognized the left and right mouse buttons. Returns one of the
//constants Event.LeftButton, Event.RightButton or Event.UnknownButton for
//a left click, right click, or indeterminate (or no mouse click).
//keycode() -- returns the index code of the key pressed. Note that this
//value may differ across browsers because of character set differences.
//Whenever possible, it is suggested to use keychar() instead.
//keychar() -- returns the char version of the key pressed rather than a
//raw numeric code. The resulting value is subject to all of the vagaries
//of browsers, character encodings in use, etc.
//shiftkey() -- returns true if the shift key was pressed.
//posX() -- return the X coordinate of the mouse relative to the document.
//posY() -- return the y coordinate of the mouse relative to the document.
//stopPropagation() -- stop event propagation (if supported)
//preventDefault() -- prevent the default action (if supported)
//---------------------------------------------------------------------------
TestRecorder.Event = function(e) {
this.event = (e) ? e : window.event;
}
TestRecorder.Event.LeftButton = 0;
TestRecorder.Event.MiddleButton = 1;
TestRecorder.Event.RightButton = 2;
TestRecorder.Event.UnknownButton = 3;
TestRecorder.Event.prototype.stopPropagation = function() {
if (this.event.stopPropagation)
this.event.stopPropagation();
}
TestRecorder.Event.prototype.preventDefault = function() {
if (this.event.preventDefault)
this.event.preventDefault();
}
TestRecorder.Event.prototype.type = function() {
return this.event.type;
}
TestRecorder.Event.prototype.button = function() {
if (this.event.button) {
if (this.event.button == 2) {
return TestRecorder.Event.RightButton;
}
return TestRecorder.Event.LeftButton;
}
else if (this.event.which) {
if (this.event.which > 1) {
return TestRecorder.Event.RightButton;
}
return TestRecorder.Event.LeftButton;
}
return TestRecorder.Event.UnknownButton;
}
TestRecorder.Event.prototype.target = function() {
var t = (this.event.target) ? this.event.target : this.event.srcElement;
if (t && t.nodeType == 3) // safari bug
return t.parentNode;
return t;
}
TestRecorder.Event.prototype.keycode = function() {
return (this.event.keyCode) ? this.event.keyCode : this.event.which;
}
TestRecorder.Event.prototype.keychar = function() {
return String.fromCharCode(this.keycode());
}
TestRecorder.Event.prototype.shiftkey = function() {
if (this.event.shiftKey)
return true;
return false;
}
TestRecorder.Event.prototype.posX = function() {
if (this.event.pageX)
return this.event.pageX;
else if (this.event.clientX) {
return this.event.clientX + document.body.scrollLeft;
}
return 0;
}
TestRecorder.Event.prototype.posY = function() {
if (this.event.pageY)
return this.event.pageY;
else if (this.event.clientY) {
return this.event.clientY + document.body.scrollTop;
}
return 0;
}
//---------------------------------------------------------------------------
//TestCase -- this class contains the interesting events that happen in
//the course of a test recording and provides some testcase metadata.
//Attributes:
//title -- the title of the test case.
//items -- an array of objects representing test actions and checks
//---------------------------------------------------------------------------
TestRecorder.TestCase = function() {
this.title = "Test Case";
// maybe some items are already stored in the background
// but we do not need them here anyway
this.items = new Array();
}
TestRecorder.TestCase.prototype.append = function(o) {
this.items[this.items.length] = o;
chrome.runtime.sendMessage({action: "append", obj: o});
}
TestRecorder.TestCase.prototype.peek = function() {
return this.items[this.items.length - 1];
}
TestRecorder.TestCase.prototype.poke = function(o) {
this.items[this.items.length - 1] = o;
chrome.runtime.sendMessage({action: "poke", obj: o});
}
//---------------------------------------------------------------------------
//Event types -- whenever an interesting event happens (an action or a check)
//it is recorded as one of the object types defined below. All events have a
//'type' attribute that marks the type of the event (one of the values in the
//EventTypes enumeration) and different attributes to capture the pertinent
//information at the time of the event.
//---------------------------------------------------------------------------
if (typeof(TestRecorder.EventTypes) == "undefined") {
TestRecorder.EventTypes = {};
}
TestRecorder.EventTypes.OpenUrl = 0;
TestRecorder.EventTypes.Click = 1;
TestRecorder.EventTypes.Change = 2;
TestRecorder.EventTypes.Comment = 3;
TestRecorder.EventTypes.Submit = 4;
TestRecorder.EventTypes.CheckPageTitle = 5;
TestRecorder.EventTypes.CheckPageLocation = 6;
TestRecorder.EventTypes.CheckTextPresent = 7;
TestRecorder.EventTypes.CheckValue = 8;
TestRecorder.EventTypes.CheckValueContains = 9;
TestRecorder.EventTypes.CheckText = 10;
TestRecorder.EventTypes.CheckHref = 11;
TestRecorder.EventTypes.CheckEnabled = 12;
TestRecorder.EventTypes.CheckDisabled = 13;
TestRecorder.EventTypes.CheckSelectValue = 14;
TestRecorder.EventTypes.CheckSelectOptions = 15;
TestRecorder.EventTypes.CheckImageSrc = 16;
TestRecorder.EventTypes.PageLoad = 17;
TestRecorder.EventTypes.ScreenShot = 18;
TestRecorder.EventTypes.MouseDown = 19;
TestRecorder.EventTypes.MouseUp = 20;
TestRecorder.EventTypes.MouseDrag = 21;
TestRecorder.EventTypes.MouseDrop = 22;
TestRecorder.EventTypes.KeyPress = 23;
TestRecorder.ElementInfo = function(element) {
this.action = element.action;
this.method = element.method;
this.href = element.href;
this.tagName = element.tagName;
this.selector = this.getCleanCSSSelector(element);
this.value = element.value;
this.checked = element.checked;
this.name = element.name;
this.type = element.type;
if (this.type)
this.type = this.type.toLowerCase();
if (element.form)
this.form = {id: element.form.id, name: element.form.getAttribute('name')};
this.src = element.src;
this.id = element.id;
this.title = element.title;
this.options = [];
if (element.selectedIndex) {
for (var i=0; i < element.options.length; i++) {
var o = element.options[i];
this.options[i] = {text:o.text, value:o.value};
}
}
this.label = this.findLabelText(element);
}
TestRecorder.ElementInfo.prototype.findLabelText = function(element) {
var label = this.findContainingLabel(element)
var text;
if (!label) {
label = this.findReferencingLabel(element);
}
if (label) {
text = label.innerHTML;
// remove newlines
text = text.replace('\n', ' ');
// remove tags
text = text.replace(/<[^>]*>/g, ' ');
// remove non-alphanumeric prefixes or suffixes
text = text.replace(/^\W*/mg, '')
text = text.replace(/\W*$/mg, '')
// remove extra whitespace
text = text.replace(/^\s*/, '').replace(/\s*$/, '').replace(/\s+/g, ' ');
}
return text;
}
TestRecorder.ElementInfo.prototype.findReferencingLabel = function(element) {
var labels = window.document.getElementsByTagName('label')
for (var i = 0; i < labels.length; i++) {
if (labels[i].attributes['for'] &&
labels[i].attributes['for'].value == element.id)
return labels[i]
}
}
TestRecorder.ElementInfo.prototype.findContainingLabel = function(element) {
var parent = element.parentNode;
if (!parent)
return undefined;
if (parent.tagName && parent.tagName.toLowerCase() == 'label')
return parent;
else
return this.findContainingLabel(parent);
}
TestRecorder.ElementInfo.prototype.getCleanCSSSelector = function(element) {
if(!element) return;
var selector = element.tagName ? element.tagName.toLowerCase() : '';
if(selector == '' || selector == 'html') return '';
var tmp_selector = '';
var accuracy = document.querySelectorAll(selector).length;
if(element.id) {
selector = "#" + element.id.replace(/\./g, '\\.');
accuracy = document.querySelectorAll(selector).length
if(accuracy==1) return selector;
}
if(element.className) {
tmp_selector = '.' + element.className.trim().replace(/ /g,".");
if(document.querySelectorAll(tmp_selector).length < accuracy) {
selector = tmp_selector;
accuracy = document.querySelectorAll(selector).length
if(accuracy==1) return selector;
}
}
var parent = element.parentNode;
var parent_selector = this.getCleanCSSSelector(parent);
if(parent_selector) {
// resolve sibling ambiguity
var matching_sibling = 0;
var matching_nodes = document.querySelectorAll(parent_selector + ' > ' + selector);
for(var i=0; i<matching_nodes.length;i++) {
if(matching_nodes[i].parentNode == parent) matching_sibling++;
}
if(matching_sibling > 1) {
var index = 1;
for (var sibling = element.previousElementSibling; sibling; sibling = sibling.previousElementSibling) index++;
selector = selector + ':nth-child(' + index + ')';
}
// remove useless intermediary parent
selector_array = parent_selector.split(' ');
if(selector_array.length>1) {
for(var i=1;i<selector_array.length;i++) {
tmp_selector = selector_array.slice(0,i).join(' ') + ' ' + selector;
if(document.querySelectorAll(tmp_selector).length == 1) {
selector = tmp_selector;
break;
}
}
}
// improve accuracy if still not correct
accuracy = document.querySelectorAll(selector).length
if(accuracy>1) {
tmp_selector = parent_selector + " " + selector;
if(document.querySelectorAll(tmp_selector).length==1) {
selector = tmp_selector;
} else {
selector = parent_selector + " > " + selector;
}
}
}
return selector;
}
TestRecorder.DocumentEvent = function(type, target) {
this.type = type;
this.url = target.URL;
this.title = target.title;
}
TestRecorder.ElementEvent = function(type, target, text) {
this.type = type;
this.info = new TestRecorder.ElementInfo(target);
this.text = text ? text : recorder.strip(contextmenu.innertext(target));
}
TestRecorder.CommentEvent = function(text) {
this.type = TestRecorder.EventTypes.Comment;
this.text = text;
}
TestRecorder.KeyEvent = function(target, text) {
this.type = TestRecorder.EventTypes.KeyPress;
this.info = new TestRecorder.ElementInfo(target);
this.text = text;
}
TestRecorder.MouseEvent = function(type, target, x, y) {
this.type = type;
this.info = new TestRecorder.ElementInfo(target);
this.x = x;
this.y = y;
this.text = recorder.strip(contextmenu.innertext(target));
}
TestRecorder.ScreenShotEvent = function() {
this.type = TestRecorder.EventTypes.ScreenShot;
}
TestRecorder.OpenURLEvent = function(url) {
this.type = TestRecorder.EventTypes.OpenUrl;
this.url = url;
this.width = window.innerWidth;
this.height = window.innerHeight;
}
TestRecorder.PageLoadEvent = function(url) {
this.type = TestRecorder.EventTypes.OpenUrl;
this.url = url;
this.viaBack = back
}
//---------------------------------------------------------------------------
//ContextMenu -- this class is responsible for managing the right-click
//context menu that shows appropriate checks for targeted elements.
//All methods and attributes are private to this implementation.
//---------------------------------------------------------------------------
TestRecorder.ContextMenu = function() {
this.selected = null;
this.target = null;
this.window = null;
this.visible = false;
this.over = false;
this.menu = null;
}
contextmenu = new TestRecorder.ContextMenu();
TestRecorder.ContextMenu.prototype.build = function(t, x, y) {
var d = recorder.window.document;
var b = d.getElementsByTagName("body").item(0);
var menu = d.createElement("div");
// Needed to deal with various cross-browser insanities...
menu.setAttribute("style", "backgroundColor:#ffffff;color:#000000;border:1px solid #000000;padding:2px;position:absolute;display:none;top:" + y + "px;left:" + x + "px;border:1px;z-index:10000;");
menu.style.backgroundColor="#ffffff";
menu.style.color="#000000";
menu.style.border = "1px solid #000000";
menu.style.padding="2px";
menu.style.position = "absolute";
menu.style.display = "none";
menu.style.zIndex = "10000";
menu.style.top = y.toString();
menu.style.left = x.toString();
menu.onmouseover=contextmenu.onmouseover;
menu.onmouseout=contextmenu.onmouseout;
var selected = TestRecorder.Browser.getSelection(recorder.window).toString();
if (t.width && t.height) {
menu.appendChild(this.item("Check Image Src", this.checkImgSrc));
}
else if (t.type == "text" || t.type == "textarea") {
menu.appendChild(this.item("Check Text Value", this.checkValue));
menu.appendChild(this.item("Check Enabled", this.checkEnabled));
menu.appendChild(this.item("Check Disabled", this.checkDisabled));
}
else if (selected && (selected != "")) {
this.selected = recorder.strip(selected);
menu.appendChild(this.item("Check Text Appears On Page",
this.checkTextPresent));
}
else if (t.href) {
menu.appendChild(this.item("Check Link Text", this.checkText));
menu.appendChild(this.item("Check Link Href", this.checkHref));
}
else if (t.selectedIndex || t.type == "option") {
var name = "Check Selected Value";
if (t.type != "select-one") {
name = name + "s";
}
menu.appendChild(this.item(name, this.checkSelectValue));
menu.appendChild(this.item("Check Select Options",
this.checkSelectOptions));
menu.appendChild(this.item("Check Enabled", this.checkEnabled));
menu.appendChild(this.item("Check Disabled", this.checkDisabled));
}
else if (t.type == "button" || t.type == "submit") {
menu.appendChild(this.item("Check Button Text", this.checkText));
menu.appendChild(this.item("Check Button Value", this.checkValue));
menu.appendChild(this.item("Check Enabled", this.checkEnabled));
menu.appendChild(this.item("Check Disabled", this.checkDisabled));
}
else if (t.value) {
menu.appendChild(this.item("Check Value", this.checkValue));
menu.appendChild(this.item("Check Enabled", this.checkEnabled));
menu.appendChild(this.item("Check Disabled", this.checkDisabled));
}
else {
menu.appendChild(this.item("Check Page Location", this.checkPageLocation));
menu.appendChild(this.item("Check Page Title", this.checkPageTitle));
menu.appendChild(this.item("Screenshot", this.doScreenShot));
}
menu.appendChild(this.item("Cancel", this.cancel));
b.insertBefore(menu, b.firstChild);
return menu;
}
TestRecorder.ContextMenu.prototype.item = function(text, func) {
var doc = recorder.window.document;
var div = doc.createElement("div");
var txt = doc.createTextNode(text);
div.setAttribute("style", "padding:6px;border:1px solid #ffffff;");
div.style.border = "1px solid #ffffff";
div.style.padding = "6px";
div.appendChild(txt);
div.onmouseover = this.onitemmouseover;
div.onmouseout = this.onitemmouseout;
div.onclick = func;
return div;
}
TestRecorder.ContextMenu.prototype.show = function(e) {
if (this.menu) {
this.hide();
}
var wnd = recorder.window;
var doc = wnd.document;
this.target = e.target();
TestRecorder.Browser.captureEvent(wnd, "mousedown", this.onmousedown);
var wh = TestRecorder.Browser.windowHeight(wnd);
var ww = TestRecorder.Browser.windowWidth(wnd);
var x = e.posX();
var y = e.posY();
if ((ww >= 0) && ((ww - x) < 100)) {
x = x - 100;
}
if ((wh >= 0) && ((wh - y) < 100)) {
y = y - 100;
}
var menu = this.build(e.target(), x, y);
this.menu = menu;
menu.style.display = "";
this.visible = true;
return;
}
TestRecorder.ContextMenu.prototype.hide = function() {
var wnd = recorder.window;
TestRecorder.Browser.releaseEvent(wnd, "mousedown", this.onmousedown);
var d = wnd.document;
var b = d.getElementsByTagName("body").item(0);
this.menu.style.display = "none" ;
b.removeChild(this.menu);
this.target = null;
this.visible = false;
this.over = false;
this.menu = null;
}
TestRecorder.ContextMenu.prototype.onitemmouseover = function(e) {
this.style.backgroundColor = "#efefef";
this.style.border = "1px solid #c0c0c0";
return true;
}
TestRecorder.ContextMenu.prototype.onitemmouseout = function(e) {
this.style.backgroundColor = "#ffffff";
this.style.border = "1px solid #ffffff";
return true;
}
TestRecorder.ContextMenu.prototype.onmouseover = function(e) {
contextmenu.over = true;
}
TestRecorder.ContextMenu.prototype.onmouseout = function(e) {
contextmenu.over = false;
}
TestRecorder.ContextMenu.prototype.onmousedown = function(e) {
if(contextmenu.visible) {
if (contextmenu.over == false) {
contextmenu.hide();
return true;
}
return true ;
}
return false;
}
TestRecorder.ContextMenu.prototype.record = function(o) {
recorder.testcase.append(o);
recorder.log(o.type);
contextmenu.hide();
}
TestRecorder.ContextMenu.prototype.checkPageTitle = function() {
var doc = recorder.window.document;
var et = TestRecorder.EventTypes;
var e = new TestRecorder.DocumentEvent(et.CheckPageTitle, doc);
contextmenu.record(e);
}
TestRecorder.ContextMenu.prototype.doScreenShot = function() {
var e = new TestRecorder.ScreenShotEvent();
contextmenu.record(e);
}
TestRecorder.ContextMenu.prototype.checkPageLocation = function() {
var doc = recorder.window.document;
var et = TestRecorder.EventTypes;
var e = new TestRecorder.DocumentEvent(et.CheckPageLocation, doc);
contextmenu.record(e);
}
TestRecorder.ContextMenu.prototype.checkValue = function() {
var t = contextmenu.target;
var et = TestRecorder.EventTypes;
var e = new TestRecorder.ElementEvent(et.CheckValue, t);
contextmenu.record(e);
}
TestRecorder.ContextMenu.prototype.checkValueContains = function() {
var s = contextmenu.selected;
var t = contextmenu.target;
var et = TestRecorder.EventTypes;
var e = new TestRecorder.ElementEvent(et.CheckValueContains, t, s);
contextmenu.selected = null;
contextmenu.record(e);
}
TestRecorder.ContextMenu.prototype.innertext = function(e) {
var doc = recorder.window.document;
if (document.createRange) {
var r = recorder.window.document.createRange();
r.selectNodeContents(e);
return r.toString();
} else {
return e.innerText;
}
}
TestRecorder.ContextMenu.prototype.checkText = function() {
var t = contextmenu.target;
var s = "";
if (t.type == "button" || t.type == "submit") {
s = t.value;
}
else {
s = contextmenu.innertext(t);
}
s = recorder.strip(s);
var et = TestRecorder.EventTypes;
var e = new TestRecorder.ElementEvent(et.CheckText, t, s);
contextmenu.record(e);
}
TestRecorder.ContextMenu.prototype.checkTextPresent = function() {
var t = contextmenu.target;
var s = contextmenu.selected;
var et = TestRecorder.EventTypes;
var e = new TestRecorder.ElementEvent(et.CheckTextPresent, t, s);
contextmenu.selected = null;
contextmenu.record(e);
}
TestRecorder.ContextMenu.prototype.checkHref = function() {
var t = contextmenu.target;
var et = TestRecorder.EventTypes;
var e = new TestRecorder.ElementEvent(et.CheckHref, t);
contextmenu.record(e);
}
TestRecorder.ContextMenu.prototype.checkEnabled = function() {
var t = contextmenu.target;
var et = TestRecorder.EventTypes;
var e = new TestRecorder.ElementEvent(et.CheckEnabled, t);
contextmenu.record(e);
}
TestRecorder.ContextMenu.prototype.checkDisabled = function() {
var t = contextmenu.target;
var et = TestRecorder.EventTypes;
var e = new TestRecorder.ElementEvent(et.CheckDisabled, t);
contextmenu.record(e);
}
TestRecorder.ContextMenu.prototype.checkSelectValue = function() {
var t = contextmenu.target;
var et = TestRecorder.EventTypes;
var e = new TestRecorder.ElementEvent(et.CheckSelectValue, t);
contextmenu.record(e);
}
TestRecorder.ContextMenu.prototype.checkSelectOptions = function() {
var t = contextmenu.target;
var et = TestRecorder.EventTypes;
var e = new TestRecorder.ElementEvent(et.CheckSelectOptions, t);
contextmenu.record(e);
}
TestRecorder.ContextMenu.prototype.checkImgSrc = function() {
var t = contextmenu.target;
var et = TestRecorder.EventTypes;
var e = new TestRecorder.ElementEvent(et.CheckImageSrc, t);
contextmenu.record(e);
}
TestRecorder.ContextMenu.prototype.cancel = function() {
contextmenu.hide();
}
//---------------------------------------------------------------------------
//Recorder -- a controller class that manages the recording of web browser
//activities to produce a test case.
//Instance Methods:
//start() -- start recording browser events.
//stop() -- stop recording browser events.
//reset() -- reset the recorder and initialize a new test case.
//---------------------------------------------------------------------------
TestRecorder.Recorder = function() {
this.testcase = new TestRecorder.TestCase();
this.logfunc = null;
this.window = null;
this.active = false;
}
//The recorder is a singleton -- there is no real reason to have more than
//one instance, and many of its methods are event handlers which need a
//stable reference to the instance.
recorder = new TestRecorder.Recorder();
recorder.logfunc = function(msg) {console.log(msg);};
TestRecorder.Recorder.prototype.start = function() {
this.window = window;
this.captureEvents();
// OVERRIDE stopPropagation
var actualCode = '(' + function() {
var overloadStopPropagation = Event.prototype.stopPropagation;
Event.prototype.stopPropagation = function(){
console.log(this);
overloadStopPropagation.apply(this, arguments);
};
} + ')();';
var script = document.createElement('script');
script.textContent = actualCode;
(document.head||document.documentElement).appendChild(script);
script.parentNode.removeChild(script);
this.active = true;
this.log("recorder started");
}
TestRecorder.Recorder.prototype.stop = function() {
this.releaseEvents();
this.active = false;
this.log("recorder stopped");
return;
}
TestRecorder.Recorder.prototype.open = function(url) {
var e = new TestRecorder.OpenURLEvent(url);
this.testcase.append(e);
this.log("open url: " + url);
}
TestRecorder.Recorder.prototype.pageLoad = function() {
var doc = recorder.window.document;
var et = TestRecorder.EventTypes;
var e = new TestRecorder.DocumentEvent(et.PageLoad, doc);
this.testcase.append(e);
this.log("page loaded url: " + e.url);
}
TestRecorder.Recorder.prototype.captureEvents = function() {
var wnd = this.window;
TestRecorder.Browser.captureEvent(wnd, "contextmenu", this.oncontextmenu);
TestRecorder.Browser.captureEvent(wnd, "drag", this.ondrag);
TestRecorder.Browser.captureEvent(wnd, "mousedown", this.onmousedown);
TestRecorder.Browser.captureEvent(wnd, "mouseup", this.onmouseup);
TestRecorder.Browser.captureEvent(wnd, "click", this.onclick);
TestRecorder.Browser.captureEvent(wnd, "change", this.onchange);
TestRecorder.Browser.captureEvent(wnd, "keypress", this.onkeypress);
TestRecorder.Browser.captureEvent(wnd, "select", this.onselect);
TestRecorder.Browser.captureEvent(wnd, "submit", this.onsubmit);
}
TestRecorder.Recorder.prototype.releaseEvents = function() {
var wnd = this.window;
TestRecorder.Browser.releaseEvent(wnd, "contextmenu", this.oncontextmenu);
TestRecorder.Browser.releaseEvent(wnd, "drag", this.ondrag);
TestRecorder.Browser.releaseEvent(wnd, "mousedown", this.onmousedown);
TestRecorder.Browser.releaseEvent(wnd, "mouseup", this.onmouseup);
TestRecorder.Browser.releaseEvent(wnd, "click", this.onclick);
TestRecorder.Browser.releaseEvent(wnd, "change", this.onchange);
TestRecorder.Browser.releaseEvent(wnd, "keypress", this.onkeypress);
TestRecorder.Browser.releaseEvent(wnd, "select", this.onselect);
TestRecorder.Browser.releaseEvent(wnd, "submit", this.onsubmit);
}
TestRecorder.Recorder.prototype.clickaction = function(e) {
// This method is called by our low-level event handler when the mouse
// is clicked in normal mode. Its job is decide whether the click is
// something we care about. If so, we record the event in the test case.
//
// If the context menu is visible, then the click is either over the
// menu (selecting a check) or out of the menu (cancelling it) so we
// always discard clicks that happen when the menu is visible.
if (!contextmenu.visible) {
var et = TestRecorder.EventTypes;
var t = e.target();
if (t.href || (t.type && t.type == "submit") ||
(t.type && t.type == "submit")) {
this.testcase.append(new TestRecorder.ElementEvent(et.Click,e.target()));
} else {
recorder.testcase.append(
new TestRecorder.MouseEvent(
TestRecorder.EventTypes.Click, e.target(), e.posX(), e.posY()
));
}
}
}
TestRecorder.Recorder.prototype.addComment = function(text) {
this.testcase.append(new TestRecorder.CommentEvent(text));
}
TestRecorder.Recorder.prototype.check = function(e) {
// This method is called by our low-level event handler when the mouse
// is clicked in check mode. Its job is decide whether the click is
// something we care about. If so, we record the check in the test case.
contextmenu.show(e);
var target = e.target();
if (target.type) {
var type = target.type.toLowerCase();
if (type == "submit" || type == "button" || type == "image") {
recorder.log('check button == "' + target.value + '"');
}
}
else if (target.href) {
if (target.innerText) {
var text = recorder.strip(target.innerText);
recorder.log('check link == "' + target.text + '"');
}
}
}
TestRecorder.Recorder.prototype.onpageload = function() {
if (this.active) {
// This must be called each time a new document is fully loaded into the
// testing target frame to ensure that events are captured for the page.
recorder.captureEvents();
// if a new page has loaded, but there doesn't seem to be a reason why,
// then we need to record the fact or the information will be lost
if (this.testcase.peek()) {
var last_event_type = this.testcase.peek().type;
if (last_event_type != TestRecorder.EventTypes.OpenUrl &&
last_event_type != TestRecorder.EventTypes.Click &&
last_event_type != TestRecorder.EventTypes.Submit) {
this.open(this.window.location.toString());
}
}
// record the fact that a page load happened
if (this.window)
this.pageLoad();
}
}
TestRecorder.Recorder.prototype.onchange = function(e) {
var e = new TestRecorder.Event(e);
var et = TestRecorder.EventTypes;
var v = new TestRecorder.ElementEvent(et.Change, e.target());
recorder.testcase.append(v);
recorder.log("value changed: " + e.target().value);
}
TestRecorder.Recorder.prototype.onselect = function(e) {
var e = new TestRecorder.Event(e);
recorder.log("select: " + e.target());
}
TestRecorder.Recorder.prototype.onsubmit = function(e) {
var e = new TestRecorder.Event(e);
var et = TestRecorder.EventTypes;
// We want to save the form element as the event target
var t = e.target();
while (t.parentNode && t.tagName != "FORM") {
t = t.parentNode;
}
var v = new TestRecorder.ElementEvent(et.Submit, t);
recorder.testcase.append(v);
recorder.log("submit: " + e.target());
}
TestRecorder.Recorder.prototype.ondrag = function(e) {
var e = new TestRecorder.Event(e);
recorder.testcase.append(
new TestRecorder.MouseEvent(
TestRecorder.EventTypes.MouseDrag, e.target(), e.posX(), e.posY()
));
}
TestRecorder.Recorder.prototype.onmousedown = function(e) {
if(!contextmenu.visible) {
var e = new TestRecorder.Event(e);
if (e.button() == TestRecorder.Event.LeftButton) {
recorder.testcase.append(
new TestRecorder.MouseEvent(
TestRecorder.EventTypes.MouseDown, e.target(), e.posX(), e.posY()
));
}
}
}
TestRecorder.Recorder.prototype.onmouseup = function(e) {