Skip to content

Commit 4c2e362

Browse files
committed
Make multiple improvements to the Recorder
1 parent 9aba60f commit 4c2e362

File tree

1 file changed

+104
-93
lines changed

1 file changed

+104
-93
lines changed

seleniumbase/js_code/recorder_js.py

Lines changed: 104 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,21 @@
112112
function tagName(el) {
113113
return el.tagName.toLowerCase();
114114
};
115-
var getBestSelector = function(el) {
116-
if (!(el instanceof Element))
117-
return;
115+
function turnIntoParentAsNeeded(el) {
118116
if (tagName(el) == 'span') {
119-
if (tagName(el.parentElement) == 'button')
117+
if (tagName(el.parentElement) == 'button') {
120118
el = el.parentElement;
121-
else if (tagName(el.parentElement.parentElement) == 'button')
119+
}
120+
else if (tagName(el.parentElement.parentElement) == 'button') {
122121
el = el.parentElement.parentElement;
122+
}
123123
}
124+
return el;
125+
}
126+
var getBestSelector = function(el) {
127+
if (!(el instanceof Element))
128+
return;
129+
el = turnIntoParentAsNeeded(el);
124130
child_sep = ' > ';
125131
selector_by_id = cssPathById(el);
126132
if (!selector_by_id.includes(child_sep))
@@ -214,28 +220,29 @@
214220
contains_tags.push('code');
215221
contains_tags.push('mark');
216222
contains_tags.push('label');
223+
contains_tags.push('small');
217224
contains_tags.push('button');
218225
contains_tags.push('legend');
219226
contains_tags.push('strong');
220227
contains_tags.push('summary');
221228
all_by_tag = [];
222-
inner_text = '';
223-
if (el.innerText)
224-
inner_text = el.innerText.trim();
229+
text_content = '';
230+
if (el.textContent)
231+
text_content = el.textContent.trim();
225232
for (var i = 0; i < contains_tags.length; i++) {
226233
if (tag_name == contains_tags[i] &&
227-
inner_text.length >= 2 && inner_text.length <= 64)
234+
text_content.length >= 2 && text_content.length <= 64)
228235
{
229236
t_count = 0;
230237
all_by_tag[i] = document.querySelectorAll(contains_tags[i]);
231238
for (var j = 0; j < all_by_tag[i].length; j++) {
232-
if (all_by_tag[i][j].innerText.includes(inner_text))
239+
if (all_by_tag[i][j].textContent.includes(text_content))
233240
t_count += 1;
234241
}
235-
if (t_count === 1 && !inner_text.includes('\n')) {
236-
inner_text = inner_text.replaceAll("'", "\\'");
237-
inner_text = inner_text.replaceAll('"', '\\"');
238-
return tag_name += ':contains("'+inner_text+'")';
242+
if (t_count === 1 && !text_content.includes('\n')) {
243+
text_content = text_content.replaceAll("'", "\\'");
244+
text_content = text_content.replaceAll('"', '\\"');
245+
return tag_name += ':contains("'+text_content+'")';
239246
}
240247
}
241248
}
@@ -340,8 +347,8 @@
340347
document.body.addEventListener('mouseover', function (event) {
341348
reset_if_recorder_undefined();
342349
if (sessionStorage.getItem('pause_recorder') === 'yes') return;
343-
const element = event.target;
344-
const selector = getBestSelector(element);
350+
const el = event.target;
351+
const selector = getBestSelector(el);
345352
if (!selector.startsWith('body') && !selector.includes(' div')) {
346353
document.title = selector;
347354
}
@@ -392,16 +399,16 @@
392399
reset_if_recorder_undefined();
393400
if (sessionStorage.getItem('pause_recorder') === 'yes') return;
394401
const d_now = Date.now();
395-
const element = event.target;
396-
const selector = getBestSelector(element);
402+
const el = event.target;
403+
const selector = getBestSelector(el);
397404
ra_len = document.recorded_actions.length;
398405
if (ra_len > 0 &&
399406
document.recorded_actions[ra_len-1][0] === 'mo_dn' &&
400407
document.recorded_actions[ra_len-1][1] === selector)
401408
{
402409
document.recorded_actions.pop();
403410
}
404-
if (element.draggable === true) {
411+
if (el.draggable === true) {
405412
document.recorded_actions.push(['drags', selector, '', d_now]);
406413
}
407414
json_rec_act = JSON.stringify(document.recorded_actions);
@@ -422,8 +429,8 @@
422429
reset_if_recorder_undefined();
423430
if (sessionStorage.getItem('pause_recorder') === 'yes') return;
424431
const d_now = Date.now();
425-
const element = event.target;
426-
const selector = getBestSelector(element);
432+
const el = event.target;
433+
const selector = getBestSelector(el);
427434
ra_len = document.recorded_actions.length;
428435
if (ra_len > 0 && document.recorded_actions[ra_len-1][0] === 'drags')
429436
{
@@ -438,11 +445,11 @@
438445
reset_if_recorder_undefined();
439446
if (sessionStorage.getItem('pause_recorder') === 'yes') return;
440447
const d_now = Date.now();
441-
const element = event.target;
442-
const selector = getBestSelector(element);
448+
const el = event.target;
449+
const selector = getBestSelector(el);
443450
ra_len = document.recorded_actions.length;
444-
tag_name = tagName(element);
445-
e_type = element.type;
451+
tag_name = tagName(el);
452+
e_type = el.type;
446453
if (tag_name === 'select')
447454
{
448455
el_computed = document.querySelector(selector);
@@ -461,7 +468,7 @@
461468
document.recorded_actions.pop();
462469
ra_len = document.recorded_actions.length;
463470
}
464-
value = element.value;
471+
value = el.value;
465472
document.recorded_actions.push(['set_v', selector, value, d_now]);
466473
}
467474
else if (tag_name === 'input' && e_type === 'file') {
@@ -470,7 +477,7 @@
470477
document.recorded_actions.pop();
471478
ra_len = document.recorded_actions.length;
472479
}
473-
value = element.value;
480+
value = el.value;
474481
document.recorded_actions.push(['cho_f', selector, value, d_now]);
475482
}
476483
else if (ra_len > 0 &&
@@ -482,9 +489,9 @@
482489
if (ra_len > 0 && document.recorded_actions[ra_len-1][1] === selector)
483490
document.recorded_actions.pop();
484491
}
485-
if (tag_name === 'input' && e_type === 'checkbox' && element.checked)
492+
if (tag_name === 'input' && e_type === 'checkbox' && el.checked)
486493
document.recorded_actions.push(['c_box', selector, 'yes', d_now]);
487-
else if (tag_name === 'input' && e_type === 'checkbox' && !element.checked)
494+
else if (tag_name === 'input' && e_type === 'checkbox' && !el.checked)
488495
document.recorded_actions.push(['c_box', selector, 'no', d_now]);
489496
json_rec_act = JSON.stringify(document.recorded_actions);
490497
sessionStorage.setItem('recorded_actions', json_rec_act);
@@ -493,76 +500,80 @@
493500
reset_if_recorder_undefined();
494501
if (sessionStorage.getItem('pause_recorder') === 'yes') return;
495502
const d_now = Date.now();
496-
const element = event.target;
497-
const selector = getBestSelector(element);
503+
el = event.target;
504+
const selector = getBestSelector(el);
498505
ra_len = document.recorded_actions.length;
499506
rec_mode = sessionStorage.getItem('recorder_mode');
500-
tag_name = tagName(element);
507+
tag_name = tagName(el);
501508
text = '';
502-
if (rec_mode === '3')
503-
text = element.innerText;
509+
if (rec_mode === '2' || rec_mode === '3')
510+
el = turnIntoParentAsNeeded(el);
511+
texts = [el.innerText, el.textContent];
504512
if (ra_len > 0 && document.recorded_actions[ra_len-1][0] === 'mo_dn')
505513
document.recorded_actions.pop();
506514
if (tag_name === 'select') {
507515
// Do Nothing. ('change' action.)
508516
}
509517
else
510-
document.recorded_actions.push(['mo_dn', selector, text, d_now]);
518+
document.recorded_actions.push(['mo_dn', selector, texts, d_now]);
511519
json_rec_act = JSON.stringify(document.recorded_actions);
512520
sessionStorage.setItem('recorded_actions', json_rec_act);
513521
});
514522
document.body.addEventListener('mouseup', function (event) {
515523
reset_if_recorder_undefined();
516524
if (sessionStorage.getItem('pause_recorder') === 'yes') return;
517525
const d_now = Date.now();
518-
const element = event.target;
519-
selector = getBestSelector(element);
526+
const el = event.target;
527+
selector = getBestSelector(el);
520528
ra_len = document.recorded_actions.length;
521-
tag_name = tagName(element);
522-
parent_element = element.parentElement;
523-
parent_tag_name = tagName(parent_element);
524-
grand_element = "";
529+
tag_name = tagName(el);
530+
parent_el = el.parentElement;
531+
parent_tag_name = tagName(parent_el);
532+
grand_el = "";
525533
grand_tag_name = "";
526534
origin = "";
527535
rec_mode = sessionStorage.getItem('recorder_mode');
528-
if (ra_len > 0 && document.recorded_actions[ra_len-1][0] === 'mo_dn') {
529-
selector = document.recorded_actions[ra_len-1][1];
530-
sel_has_contains = selector.includes(':contains(');
531-
if (rec_mode === '2' || (rec_mode === '3' && sel_has_contains)) {
532-
origin = window.location.origin;
533-
document.recorded_actions.push(['as_el', selector, origin, d_now]);
536+
if (ra_len > 0 && document.recorded_actions[ra_len-1][0] === 'mo_dn' &&
537+
(rec_mode === '2' || rec_mode === '3'))
538+
{
539+
pre_sel = document.recorded_actions[ra_len-1][1];
540+
sel_has_contains = pre_sel.includes(':contains(');
541+
texts = document.recorded_actions[ra_len-1][2];
542+
text = texts[0];
543+
t_con = texts[1];
544+
origin = window.location.origin;
545+
if (!text) { text = ''; }
546+
if (rec_mode === '2' || (
547+
rec_mode === '3' && sel_has_contains && text === t_con))
548+
{
549+
document.recorded_actions.push(['as_el', pre_sel, origin, d_now]);
534550
json_rec_act = JSON.stringify(document.recorded_actions);
535551
sessionStorage.setItem('recorded_actions', json_rec_act);
536552
return;
537553
}
538554
else if (rec_mode === '3') {
539-
origin = window.location.origin;
540-
text = document.recorded_actions[ra_len-1][2];
541555
action = 'as_et';
542-
if (!text) { text = ''; }
543-
else {
544-
text = text.trim();
545-
var match = /\r|\n/.exec(text);
546-
if (match) {
547-
lines = text.split(/\r\n|\r|\n/g);
548-
text = '';
549-
for (var i = 0; i < lines.length; i++) {
550-
if (lines[i].length > 0) {
551-
action = 'as_te'; text = lines[i]; break;
552-
}
556+
text = text.trim();
557+
var match = /\r|\n/.exec(text);
558+
if (match) {
559+
lines = text.split(/\r\n|\r|\n/g);
560+
text = '';
561+
for (var i = 0; i < lines.length; i++) {
562+
if (lines[i].length > 0) {
563+
action = 'as_te'; text = lines[i]; break;
553564
}
554565
}
555566
}
556-
tex_sel = [text, selector];
567+
tex_sel = [text, pre_sel];
557568
document.recorded_actions.push([action, tex_sel, origin, d_now]);
558569
json_rec_act = JSON.stringify(document.recorded_actions);
559570
sessionStorage.setItem('recorded_actions', json_rec_act);
560571
return;
561572
}
562573
}
563-
if (parent_element.parentElement != null) {
564-
grand_element = parent_element.parentElement;
565-
grand_tag_name = tagName(grand_element);
574+
if (parent_el.parentElement != null) {
575+
grand_el = parent_el.parentElement;
576+
grand_tag_name = tagName(grand_el);
566577
}
567578
if (ra_len > 0 &&
568579
document.recorded_actions[ra_len-1][1] === selector &&
@@ -572,38 +583,38 @@
572583
{
573584
href = '';
574585
if (tag_name === 'a' &&
575-
element.hasAttribute('href') &&
576-
element.getAttribute('href').length > 0 &&
577-
element.origin != 'null')
586+
el.hasAttribute('href') &&
587+
el.getAttribute('href').length > 0 &&
588+
el.origin != 'null')
578589
{
579-
href = element.href;
580-
origin = element.origin;
590+
href = el.href;
591+
origin = el.origin;
581592
}
582593
else if (parent_tag_name === 'a' &&
583-
parent_element.hasAttribute('href') &&
584-
parent_element.getAttribute('href').length > 0 &&
585-
parent_element.origin != 'null')
594+
parent_el.hasAttribute('href') &&
595+
parent_el.getAttribute('href').length > 0 &&
596+
parent_el.origin != 'null')
586597
{
587-
href = parent_element.href;
588-
origin = parent_element.origin;
598+
href = parent_el.href;
599+
origin = parent_el.origin;
589600
}
590601
else if (grand_tag_name === 'a' &&
591-
grand_element.hasAttribute('href') &&
592-
grand_element.getAttribute('href').length > 0 &&
593-
grand_element.origin != 'null')
602+
grand_el.hasAttribute('href') &&
603+
grand_el.getAttribute('href').length > 0 &&
604+
grand_el.origin != 'null')
594605
{
595-
href = grand_element.href;
596-
origin = grand_element.origin;
606+
href = grand_el.href;
607+
origin = grand_el.origin;
597608
}
598609
document.recorded_actions.pop();
599610
child_sep = ' > ';
600611
child_count = ssOccurrences(selector, child_sep);
601-
if ((tag_name === "a" && !element.hasAttribute('onclick') &&
612+
if ((tag_name === "a" && !el.hasAttribute('onclick') &&
602613
child_count > 0 && href.length > 0) ||
603614
(parent_tag_name === "a" && href.length > 0 &&
604-
child_count > 1 && !parent_element.hasAttribute('onclick')) ||
615+
child_count > 1 && !parent_el.hasAttribute('onclick')) ||
605616
(grand_tag_name === "a" && href.length > 0 &&
606-
child_count > 2 && !grand_element.hasAttribute('onclick')))
617+
child_count > 2 && !grand_el.hasAttribute('onclick')))
607618
{
608619
w_orig = window.location.origin;
609620
if (origin === w_orig)
@@ -614,11 +625,11 @@
614625
else
615626
document.recorded_actions.push(['click', selector, href, d_now]);
616627
// hover_click() if dropdown.
617-
if (element.parentElement.classList.contains('dropdown-content') &&
618-
element.parentElement.parentElement.classList.contains('dropdown'))
628+
if (el.parentElement.classList.contains('dropdown-content') &&
629+
el.parentElement.parentElement.classList.contains('dropdown'))
619630
{
620631
ch_s = selector;
621-
pa_el = element.parentElement.parentElement;
632+
pa_el = el.parentElement.parentElement;
622633
pa_s = getBestSelector(pa_el);
623634
if (pa_el.childElementCount >= 2 &&
624635
!pa_el.firstElementChild.classList.contains('dropdown-content'))
@@ -649,8 +660,8 @@
649660
document.body.addEventListener('contextmenu', function (event) {
650661
reset_if_recorder_undefined();
651662
if (sessionStorage.getItem('pause_recorder') === 'yes') return;
652-
const element = event.target;
653-
const selector = getBestSelector(element);
663+
const el = event.target;
664+
const selector = getBestSelector(el);
654665
ra_len = document.recorded_actions.length;
655666
if (ra_len > 0 &&
656667
document.recorded_actions[ra_len-1][0] === 'mo_dn' &&
@@ -714,12 +725,12 @@
714725
// After controls for switching modes.
715726
if (sessionStorage.getItem('pause_recorder') === 'yes') return;
716727
const d_now = Date.now();
717-
const element = event.target;
718-
const selector = getBestSelector(element);
728+
const el = event.target;
729+
const selector = getBestSelector(el);
719730
skip_input = false;
720-
if ((tagName(element) === 'input' &&
721-
element.type !== 'checkbox' && element.type !== 'range') ||
722-
tagName(element) === 'textarea')
731+
if ((tagName(el) === 'input' &&
732+
el.type !== 'checkbox' && el.type !== 'range') ||
733+
tagName(el) === 'textarea')
723734
{
724735
ra_len = document.recorded_actions.length;
725736
if (ra_len > 0 && l_key === 'enter' &&
@@ -755,7 +766,7 @@
755766
}
756767
if (!skip_input) {
757768
document.recorded_actions.push(
758-
['input', selector, element.value, d_now]);
769+
['input', selector, el.value, d_now]);
759770
}
760771
}
761772
json_rec_act = JSON.stringify(document.recorded_actions);

0 commit comments

Comments
 (0)