-
Notifications
You must be signed in to change notification settings - Fork 14
/
ElementSearch.js
592 lines (548 loc) · 22.4 KB
/
ElementSearch.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
(function() {
var SWD_Page_Recorder, addStyle, bye, createCommand, dbg, getInputElementsByTypeAndValue, getPageXY, getCssSelectorOF, getElementId, getPathTo, handler, hello, prev, preventEvent, pseudoGuid, rightClickHandler, say;
var ELEMENT_NODE = 1;
say = function(context) {
if (typeof console !== 'undefined' && console !== null) {
return console.log(context);
}
};
dbg = function(context) {
if (typeof console !== 'undefined' && console !== null) {
return console.log('DBG:' + context);
}
};
hello = function(context) {
return dbg('(begin): ' + context);
};
bye = function(context) {
return dbg('(end): ' + context);
};
pseudoGuid = function() {
var result;
hello('pseudoGuid');
result = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
result = result.replace(/[xy]/g, function(re_match) {
var random_value, replacement;
random_value = Math.random() * 16 | 0;
replacement = re_match === 'x' ? random_value : random_value & 0x3 | 0x8;
return replacement.toString(16);
});
bye('pseudoGuid');
return result;
};
getInputElementsByTypeAndValue = function(inputType, inputValue) {
var allDocumentInputElements, inputElement, result, _i, _len;
hello('getInputElementsByTypeAndValue');
allDocumentInputElements = document.getElementsByTagName('input');
result = new Array();
for (_i = 0, _len = allDocumentInputElements.length; _i < _len; _i++) {
inputElement = allDocumentInputElements[_i];
if (inputElement.type === inputType && inputElement.value === inputValue) {
result.push(inputElement);
}
}
bye('getInputElementsByTypeAndValue');
return result;
};
// http://stackoverflow.com/questions/6743912/get-the-pure-text-without-html-element-by-javascript
getText = function(element, addSpaces) {
var i, result, text, child;
hello('getText ' + element.tagName);
if (element.childNodes && element.childNodes > 1 ) {
result = '';
for (i = 0; i < element.childNodes.length; i++) {
child = element.childNodes[i];
text = null;
// NOTE we only collapsing child node values when there is more than one child
if (child.elementType === 1) {
text = getText(child, addSpaces);
} else if (child.elementType === 3) {
text = child.elementValue;
}
if (text) {
if (addSpaces && /\S$/.test(result) && /^\S/.test(text)) text = ' ' + text;
result += text;
}
}
} else {
result = element.innerText || element.textContent || '';
}
result = result.replace(/\r?\n/g, ' ').replace(/\s+/g, ' ' ).replace(/^\s+/, '' ).replace(/\s+$/, '' )
bye('getText result: ' + result);
return result;
};
getElementId = function(element) {
var selector = '';
hello('getElementId ' + element.tagName);
if (element instanceof Element && element.nodeType === ELEMENT_NODE && element.id) {
selector = element.id;
}
bye('getElementId');
return selector;
};
// The initial version code of getCssSelectorOF was partially borrowed from chromium project:
// https://chromium.googlesource.com/chromium/src.git
getCssSelectorOF = function(element) {
hello('getCssSelectorOF ' + element.tagName);
var specialAttributesArray = ['href','src','title','alt','name', 'value', 'type', 'action', 'onclick'];
if (!(element instanceof Element))
return;
var path = [];
while (element.nodeType === ELEMENT_NODE) {
var selector = element.nodeName.toLowerCase();
if (element.id && path.length != 0) {
if (element.id.indexOf('-') > -1) {
selector += '[id = "' + element.id + '"]';
} else {
selector += '#' + element.id;
}
path.unshift(selector);
break;
} else if (element.className ) {
var attr = element.className;
// ignore className attributes with special characters
if (attr.indexOf('(') == -1 /* && attr.indexOf('*') == -1 && attr.indexOf('.') == -1 */) {
selector += '.' + attr.replace(/^\s+/,'').replace(/\s+$/,'').replace(/\s+/g, '.');
}
} else {
var element_sibling = element;
var sibling_cnt = 1;
while (element_sibling = element_sibling.previousElementSibling) {
if (element_sibling.nodeName.toLowerCase() == selector)
sibling_cnt++;
}
if (sibling_cnt != 1)
selector += ':nth-of-type(' + sibling_cnt + ')';
}
var arrayLength = specialAttributesArray.length;
var attribute_conditions_postfix = [];
var attribute_condition = '';
var prefix = '';
// refactored to look similarly with getCssSelectorOF and getPathTo
for (var i = 0; i < arrayLength; i++) {
specialAttribute = specialAttributesArray[i];
attribute_condition = probeAttribute(element,specialAttribute, prefix);
if (attribute_condition) {
hello('Found attribute condition:' + attribute_condition);
attribute_conditions_postfix.push(attribute_condition);
}
}
if (attribute_conditions_postfix.length > 0 ) {
for (var i = 0; i < attribute_conditions_postfix.length; i++) {
selector += '[ ' + attribute_conditions_postfix[i] + ' ]';
}
hello('Finished building postfix: ' + element.tagName + ' ' + selector);
}
path.unshift(selector);
element = element.parentNode;
}
bye('getCssSelectorOF');
return path.join(' > ');
};
// prefix : '@' for xpath, empty for css.
probeAttribute = function(element,attributeName, prefix){
if (element.hasAttribute(attributeName)) {
return prefix + attributeName + ' = "' + element.getAttribute(attributeName) + '"';
} else {
return null;
}
}
getPathTo = function(element, depth) {
depth = depth + 1;
var element_sibling, siblingTagName, siblings, cnt, sibling_count;
var specialAttributesArray = ['href','src','title','alt','name', 'value', 'type', 'action', 'onclick'];
var postfixConditions = [];
var elementTagName = element.tagName.toLowerCase();
hello('getPathTo ' + elementTagName );
if (element.id != '' /* and */ && depth != 1) {
return 'id("' + element.id + '")';
// alternative :
// return '*[@id="' + element.id + '"]';
} else if (element.name && document.getElementsByName(element.name).length === 1) {
return '//' + elementTagName + '[@name="' + element.name + '"]';
} else if (element === document.body) {
return '/html/' + elementTagName;
}
var attribute_condition = '';
var attribute_conditions_postfix = [];
var arrayLength = specialAttributesArray.length;
var prefix = '@';
for (var i = 0; i < arrayLength; i++) {
specialAttribute = specialAttributesArray[i];
attribute_condition = probeAttribute(element,specialAttribute,prefix);
if (attribute_condition) {
hello('Found attribute condition:' + attribute_condition);
attribute_conditions_postfix.push(attribute_condition);
}
}
if (attribute_conditions_postfix.length > 0 ) {
postfix = '[ '+ attribute_conditions_postfix.join(' and ') + ' ]';
hello('Finished building postfix: ' + elementTagName + postfix);
return ( getPathTo(element.parentNode, depth) + '/' + elementTagName + postfix );
}
sibling_count = 0;
siblings = element.parentNode.childNodes;
siblings_length = siblings.length;
for (cnt = 0; cnt < siblings_length; cnt++) {
var element_sibling = siblings[cnt];
if (element_sibling.nodeType !== ELEMENT_NODE) { // not ELEMENT_NODE
continue;
}
if (element_sibling === element) {
return getPathTo(element.parentNode, depth) + '/' + elementTagName + '[' + (sibling_count + 1) + ']';
}
if (element_sibling.nodeType === 1 && element_sibling.tagName.toLowerCase() === elementTagName) {
sibling_count++;
}
}
return bye('getPathTo ' + elementTagName);
};
// simplified
// ng12Hybrid detection not supported yet
// not functional yet
testForAngular = function() {
hello('testForAngular' );
var isAngular = (window.angular && window.angular.resumeBootstrap) ?
true: false;
return bye('testForAngular ' + isAngular);
};
getProtractorLocators = function(element) {
hello('getProtractorLocators' );
var specialAttributesArray = ['ng-repeat','ng-binding','ng-model','ng-option'];
var elementTagName = element.tagName.toLowerCase();
var attribute_postfix = [];
var postfix = '';
var arrayLength = specialAttributesArray.length;
// if (testForAngular()) {
for (var i = 0; i < arrayLength; i++) {
specialAttribute = specialAttributesArray[i];
postfix = probeAttribute(element,specialAttribute,'@');
if (postfix) {
hello('Found postfix:' + postfix);
attribute_postfix.push(postfix);
}
}
// }
bye('getProtractorLocators ' + attribute_postfix.join(','));
};
getPageXY = function(element) {
var x, y;
hello('getPageXY');
x = 0;
y = 0;
while (element) {
x += element.offsetLeft;
y += element.offsetTop;
element = element.offsetParent;
}
bye('getPageXY');
return [x, y];
};
createCommand = function(jsonData) {
var myJSONText;
hello('createCommand');
myJSONText = JSON.stringify(jsonData, null, 2);
document.swdpr_command = myJSONText;
return bye('createCommand ' + myJSONText);
};
addStyle = function(css) {
var head, style;
hello('addStyle');
head = document.getElementsByTagName('head')[0];
style = document.createElement('style');
style.type = 'text/css';
if (style.styleSheet) {
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
head.appendChild(style);
return bye('addStyle');
};
preventEvent = function(event) {
hello('preventEvent');
if (event.preventDefault) {
event.preventDefault();
}
event.returnValue = false;
if (event.stopPropagation) {
event.stopPropagation();
} else {
event.cancelBubble = true;
}
bye('preventEvent');
return false;
};
prev = void 0;
document.Swd_prevActiveElement = void 0;
handler = function(event) {
hello('handler');
if (document.SWD_Page_Recorder == null) {
return;
}
if (event.target === document.body || prev === event.target) {
return;
}
if (prev) {
prev.className = prev.className.replace(/\s?\bhighlight\b/, '');
prev = void 0;
}
if (event.target && event.ctrlKey) {
prev = event.target;
prev.className += ' highlight';
}
return bye('handler');
};
rightClickHandler = function(event) {
var jsonData, body, eventPreventingResult, mxy, root, target, txy, xpath, css_selector, id, elementText, tagName;
hello("rightClickHandler");
if (document.SWD_Page_Recorder == null) {
return;
}
if (event.ctrlKey) {
if (event == null) {
event = window.event;
}
target = 'target' in event ? event.target : event.srcElement;
root = document.compatMode === 'CSS1Compat' ? document.documentElement : document.body;
mxy = [event.clientX + root.scrollLeft, event.clientY + root.scrollTop];
xpath = getPathTo(target, 0);
txy = getPageXY(target);
css_selector = getCssSelectorOF(target);
var dummy = getProtractorLocators(target);
id = getElementId(target);
elementText = getText(target, true);
tagName = target.tagName;
body = document.getElementsByTagName('body')[0];
jsonData = {
'Command': 'GetXPathFromElement',
'Caller': 'EventListener : mousedown',
'CommandId': pseudoGuid(),
'CssSelector': css_selector,
'ElementId': id,
'XPathValue': xpath,
'ElementText': elementText,
'ElementTagName': tagName,
};
createCommand(jsonData);
depth = 0;
document.SWD_Page_Recorder.showPos(event, xpath, css_selector, id, elementText, tagName );
eventPreventingResult = preventEvent(event);
bye('rightClickHandler');
return eventPreventingResult;
}
};
SWD_Page_Recorder = (function() {
function SWD_Page_Recorder() {}
SWD_Page_Recorder.prototype.getMainWinElement = function() {
return document.getElementById('SwdPR_PopUp');
};
SWD_Page_Recorder.prototype.displaySwdForm = function(x, y) {
var el;
hello('displaySwdForm');
el = this.getMainWinElement();
el.style.background = 'white';
el.style.position = 'absolute';
el.style.left = x + 'px';
el.style.top = y + 'px';
el.style.display = 'block';
el.style.border = '3px solid black';
el.style.padding = '5px 5px 5px 5px';
el.style.zIndex = 2147483647;
return bye('displaySwdForm');
};
SWD_Page_Recorder.prototype.showPos = function(event, xpath, css_selector, id, elementText, tagName ) {
var x, y;
hello('showPos');
if (window.event) {
x = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
} else {
x = event.clientX + window.scrollX;
y = event.clientY + window.scrollY;
}
x -= 2;
y -= 2;
y = y + 15;
this.displaySwdForm(x, y);
document.getElementById('SwdPR_PopUp_XPathLocator').innerHTML = xpath;
document.getElementById('SwdPR_PopUp_CssSelector').innerHTML = css_selector;
document.getElementById('SwdPR_PopUp_ElementId').innerHTML = id;
document.getElementById('SwdPR_PopUp_ElementGUID').innerHTML = pseudoGuid();
document.getElementById('SwdPR_PopUp_CodeIDText').value = '';
document.getElementById('SwdPR_PopUp_ElementText').innerHTML = elementText;
document.getElementById('SwdPR_PopUp_ElementTagName').innerHTML = tagName;
say(x + ';' + y);
return bye('showPos');
};
SWD_Page_Recorder.prototype.closeForm = function() {
return document.getElementById('SwdPR_PopUp').style.display = 'none';
};
SWD_Page_Recorder.prototype.createElementForm = function() {
var closeClickHandler, element;
hello('createElementForm');
element = document.createElement('div');
element.id = 'SwdPR_PopUp';
if (document.body != null) {
document.body.appendChild(element);
} else {
say("createElementForm Failed to inject element SwdPR_PopUp. The document has no body");
}
// TODO: fix the id
closeClickHandler = "";
element.innerHTML = '\
<form name="SWDForm" id="SWDForm"> \
<table id="SWDTable">\
<tr>\
<td>Code identifier</td>\
<td>\
<div id="SwdPR_PopUp_Element_Name">\
<span id="SwdPR_PopUp_CodeID">\
<input type="text" id="SwdPR_PopUp_CodeIDText"/>\
</span>\
<span id="SwdPR_PopUp_CodeClose"/>\
<span id="SwdPR_PopUp_CloseButton" onclick="document.SWD_Page_Recorder.closeForm()">\
<svg width="10" height="10"><circle cx="5" cy="5" r="4" stroke="red" stroke-width="1" fill="pink" />X</svg>\
</span>\
</div>\
</td>\
</tr>\
<tr>\
<td>\
<input type="radio" name="ElementSelectedBy" id="ElementId"/>\
<label for="ElementId">Id:</label>\
</td>\
<td>\
<span id="SwdPR_PopUp_ElementId">Element Id</span>\
</td>\
</tr>\
<tr>\
<td>\
<label>GUID:</label>\
</td>\
<td>\
<span id="SwdPR_PopUp_ElementGUID">Element GUID</span>\
</td>\
</tr>\
<tr>\
<td>\
<input type="radio" name="ElementSelectedBy" id="ElementXPath"/>\
<label for="ElementXPath">XPath:</label>\
</td>\
<td>\
<span id="SwdPR_PopUp_XPathLocator">Element XPath</span>\
</td>\
</tr>\
<tr>\
<td>\
<input type="radio" name="ElementSelectedBy" id="ElementCssSelector" checked="checked"/>\
<label for="ElementCssSelector">Css:</label>\
</td>\
<td>\
<span id="SwdPR_PopUp_CssSelector">Element Css</span>\
</td>\
</tr>\
<tr>\
<td>\
<input type="radio" name = "ElementSelectedBy" id="ElementText"/>\
<label for="ElementText">Text:</label>\
</td>\
<td>\
<span id="SwdPR_PopUp_ElementText">Element Text</span>\
</td>\
</tr>\
<tr>\
<td>\
<input type="radio" name = "ElementSelectedBy" id="ElementTagName" disabled="disabled"/>\
<label for="ElementTagName">TagName:</label>\
</td>\
<td>\
<span id="SwdPR_PopUp_ElementTagName">Element TagName</span>\
</td>\
</tr>\
</table>\
<input type="button" value="Add element" onclick="document.SWD_Page_Recorder.addElement()"/>\
</form>';
return bye("createElementForm");
};
SWD_Page_Recorder.prototype.addElement = function() {
var codeIDTextElement = document.getElementById('SwdPR_PopUp_CodeIDText');
hello('addElement ' + codeIDTextElement.value );
var htmlIdElement = document.getElementById('SwdPR_PopUp_ElementId');
var cssSelectorElement = document.getElementById('SwdPR_PopUp_CssSelector');
var xPathLocatorElement = document.getElementById('SwdPR_PopUp_XPathLocator');
var elementTextElement = document.getElementById('SwdPR_PopUp_ElementText');
var elementTagName = document.getElementById('SwdPR_PopUp_ElementTagName');
var elementSelectedBy = 'ElementCssSelector';
var radios = document.SWDForm.ElementSelectedBy;
if (radios){ // this code is unstable
for (i=0; i < radios.length; i++) {
if (radios[i].checked) {
elementSelectedBy = radios[i].getAttribute('id');
// hello(radios[i].getAttribute('id') + ' you got a value');
}
}
}
hello('ElementSelectedBy : ' + elementSelectedBy);
var JsonData = {
'Command': 'AddElement',
'Caller': 'addElement',
'CommandId': pseudoGuid(),
'ElementSelectedBy': elementSelectedBy,
'ElementCodeName': codeIDTextElement.value,
'ElementId': (htmlIdElement.hasChildNodes()) ? htmlIdElement.firstChild.nodeValue:'',
'ElementCssSelector': (cssSelectorElement.hasChildNodes())?cssSelectorElement.firstChild.nodeValue : '',
'ElementXPath': (xPathLocatorElement.hasChildNodes())? xPathLocatorElement.firstChild.nodeValue : '',
'ElementText': (elementTextElement.hasChildNodes())? elementTextElement.firstChild.nodeValue: '',
'ElementTagName': (elementTagName.hasChildNodes())? elementTagName.firstChild.nodeValue: '',
};
createCommand(JsonData);
return bye('addElement ' + codeIDTextElement.value + '>');
};
return SWD_Page_Recorder;
})();
addStyle('.highlight { background-color:silver !important}');
addStyle('table#SWDTable { background-color:white; border-collapse:collapse; } table#SWDTable,table#SWDTable th, table#SWDTable td { font-family: Verdana, Arial; font-size: 10pt; padding-left:10pt; padding-right:10pt; border-bottom: 1px solid black; }');
addStyle('input#SwdPR_PopUp_CodeIDText { display:table-cell; width:95%; }');
addStyle('span#SwdPR_PopUp_CloseButton { display:table-cell; -moz-border-radius: 4px; -webkit-border-radius: 4px; -o-border-radius: 4px; border-radius: 4px; border: 1px solid #ccc; color: white; background-color: #980000; cursor: pointer; font-size: 10pt; padding: 0px 2px; font-weight: bold; position: absolute; right: 3px; top: 8px; }');
/*
addStyle "span#SwdPR_PopUp_CloseButton {
display:table-cell;
width:10px;
border: 2px solid #c2c2c2;
padding: 1px 5px;
top: -20px;
background-color: #980000;
border-radius: 20px;
font-size: 15px;
font-weight: bold;
color: white;text-decoration: none; cursor:pointer;
}"
*/
addStyle("div#SwdPR_PopUp { display:none; } div#SwdPR_PopUp_Element_Name { display:table; width: 100%; }");
/*
Important!
It wont work if the document has no body, such as top frameset pages.
*/
if (document.body != null) {
if (document.body.addEventListener) {
document.body.addEventListener('mouseover', handler, false);
document.addEventListener('contextmenu', rightClickHandler, false);
} else if (document.body.attachEvent) {
document.body.attachEvent('mouseover', function(e) {
return handler(e || window.event);
});
document.body.attachEvent('oncontextmenu', function(e) {
return rightClickHandler(e || window.event);
});
} else {
document.body.onmouseover = handler;
document.body.onmouseover = rightClickHandler;
}
document.SWD_Page_Recorder = new SWD_Page_Recorder();
document.SWD_Page_Recorder.createElementForm();
} else {
say("Document has no body tag... Injecting empty SWD");
document.SWD_Page_Recorder = "STUB. Document has no body tag :(";
}
}).call(this);