Skip to content

Commit

Permalink
Merge pull request #4 from gazayas/clean-up-javascript
Browse files Browse the repository at this point in the history
Unify JavaScript syntax and spacing
  • Loading branch information
gazayas authored Nov 10, 2023
2 parents 51c8ec0 + 30e05b3 commit d25b609
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 45 deletions.
3 changes: 1 addition & 2 deletions app/views/magic_test/_context_menu.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
enableKeyboardShortcuts();
});


function enableKeyboardShortcuts() {
// Ctrl+A to generate an assertion
// Ctrl+A to generate an assertion.
function keydown(event) {
if (event.ctrlKey && event.shiftKey && event.key === 'A') {
event.preventDefault();
Expand Down
15 changes: 6 additions & 9 deletions app/views/magic_test/_finders.html
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
<script>

function getPathTo(element) {
if (element.tagName === 'HTML') {
return '/HTML[1]';
}
if (element===document.body) {
if (element === document.body) {
return '/HTML[1]/BODY[1]';
}
var ix = 0;
var siblings = element.parentNode.childNodes;
for (var i= 0; i<siblings.length; i++) {
var sibling= siblings[i];
if (sibling===element) {
return getPathTo(element.parentNode)+'/'+element.tagName+'['+(ix+1)+']';
for (var i = 0; i < siblings.length; i++) {
var sibling = siblings[i];
if (sibling === element) {
return getPathTo(element.parentNode) + '/' + element.tagName + '[' + (ix + 1) + ']';
}
if (sibling.nodeType===1 && sibling.tagName===element.tagName) {
if (sibling.nodeType === 1 && sibling.tagName === element.tagName) {
ix++;
}
}
Expand Down Expand Up @@ -49,6 +48,4 @@
// use the xpath to the element
return `find(:xpath, '${getPathTo(element)}')`;
}


</script>
34 changes: 16 additions & 18 deletions app/views/magic_test/_javascript_helpers.html
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
<script text="text/javascript">

ready(function() {
console.log("🪄 Magic Test activated in the browser!")
console.log("🪄 Magic Test activated in the browser!");
initializeStorage();
initializeMutationObserver();
});

function clickFunction(event) {
var element = event.target
var tagName = element.tagName
var action = ""
var target = ""
var options = ""
var element = event.target;
var tagName = element.tagName;
var action = "";
var target = "";
var options = "";
if (tagName == "BUTTON" || tagName == "A" || (tagName == "INPUT" && element.type == 'submit')) {
action = "click_on"
action = "click_on";
target = element.value || element.text
if (!target) {
return;
}
target = "\'" + target.trim().replace("'", "\\\'") + "\'"
target = "\'" + target.trim().replace("'", "\\\'") + "\'";
} else if (tagName == "INPUT") {
let ignoreType = ['text', 'password', 'date', 'email', 'month', 'number', 'search']
let ignoreType = ['text', 'password', 'date', 'email', 'month', 'number', 'search'];
if (ignoreType.includes(element.type)) {
return;
}
var path = getPathTo(element)
action = `find(:xpath, '${path}').click`
var path = getPathTo(element);
action = `find(:xpath, '${path}').click`;
} else {
return;
}
Expand Down Expand Up @@ -65,8 +64,8 @@
var charStr = capybaraFromCharCode(charCode);
var letter = evt.key == "'" ? "\\\'" : evt.key;
var tagName = evt.target.tagName.toLowerCase();
var action = finderForElement(evt.target) + "." // `find('${tagName}').`;
var target = ""
var action = finderForElement(evt.target) + "."; // `find('${tagName}').`;
var target = "";
if (charStr) {
target = `send_keys(${charStr})`;
} else {
Expand Down Expand Up @@ -109,7 +108,7 @@
var letter = String.fromCharCode(charCode);
var tagName = evt.target.tagName.toLowerCase();
var action = `find('${tagName}').`;
var target = ""
var target = "";
if (charStr) {
target = `send_keys(${charStr})`;
} else {
Expand All @@ -120,14 +119,13 @@
var lastAction = testingOutput[testingOutput.length - 1];
if (lastAction && lastAction.action == action && lastAction.target.substr(0,9) == 'send_keys') {
if (charStr) {
lastAction.target = lastAction.target.substr(0, lastAction.target.length - 1) + ', ' + charStr + '' + ')'
lastAction.target = lastAction.target.substr(0, lastAction.target.length - 1) + ', ' + charStr + '' + ')';
} else {
lastAction.target = lastAction.target.substr(0, lastAction.target.length - 1) + ', \'' + letter + '\'' + ')'
lastAction.target = lastAction.target.substr(0, lastAction.target.length - 1) + ', \'' + letter + '\'' + ')';
}
} else {
testingOutput.push({action: action, target: target, options: options});
}
sessionStorage.setItem("testingOutput", JSON.stringify(testingOutput));
}

</script>
3 changes: 1 addition & 2 deletions app/views/magic_test/_key_codes.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,4 @@
}
return codes[charCode];
}

</script>
</script>
16 changes: 5 additions & 11 deletions app/views/magic_test/_mutation_observer.html
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
<script>

function initializeMutationObserver(){
window.mutationObserver = new MutationObserver(function(mutations) {
console.log("Mutation observed")
console.log("Mutation observed");
if (!window.target) {
console.log("There is no window.target element. Quitting the mutation callback function");
console.log("There is no window.target element. Quitting the mutation callback function.");
return;
}
var options = "";
var targetClass = window.target.classList[0] ? `.${window.target.classList[0]}` : ""
var text = window.target.innerText ? `', text: '${window.target.innerText}` : ""
var targetClass = window.target.classList[0] ? `.${window.target.classList[0]}` : "";
var text = window.target.innerText ? `', text: '${window.target.innerText}` : "";
var action = `${finderForElement(window.target)}.hover`;
// var action = `find('${window.target.localName}${targetClass}${text}').hover`;
var target = "";
var testingOutput = JSON.parse(sessionStorage.getItem("testingOutput"));
testingOutput.push({action: action, target: target, options: options});
sessionStorage.setItem("testingOutput", JSON.stringify(testingOutput));
});



}

function mutationStart(evt) {
Expand All @@ -31,6 +27,4 @@
function mutationEnd () {
window.mutationObserver.disconnect();
}


</script>
</script>
4 changes: 1 addition & 3 deletions app/views/magic_test/_storage.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,4 @@
sessionStorage.setItem("testingOutput", JSON.stringify([]));
}
}


</script>
</script>

0 comments on commit d25b609

Please sign in to comment.