From 06313ae34d548bab080c74dc7d999fa367dc5b96 Mon Sep 17 00:00:00 2001 From: Gabriel Zayas Date: Mon, 10 Jul 2023 20:42:46 +0900 Subject: [PATCH 1/2] Register Capypara actions for alert and confirm methods --- app/views/magic_test/_context_menu.html.erb | 17 +++++++++++++++++ app/views/magic_test/_javascript_helpers.html | 13 +++++++++++++ 2 files changed, 30 insertions(+) diff --git a/app/views/magic_test/_context_menu.html.erb b/app/views/magic_test/_context_menu.html.erb index 40370ca..96a72a9 100644 --- a/app/views/magic_test/_context_menu.html.erb +++ b/app/views/magic_test/_context_menu.html.erb @@ -7,6 +7,23 @@ } } + // Override the alert and confirm functions to register capybara actions. + (function() { + var _old_alert = window.alert; + window.alert = function() { + window.registerAcceptAlert = true; + _old_alert.apply(window, arguments); + }; + })(); + + (function() { + var _old_confirm = window.confirm; + window.confirm = function() { + window.registerAcceptConfirm = true; + _old_confirm.apply(window, arguments); + } + })(); + ready(function() { enableKeyboardShortcuts(); }); diff --git a/app/views/magic_test/_javascript_helpers.html b/app/views/magic_test/_javascript_helpers.html index d9ff750..e3cfa7e 100644 --- a/app/views/magic_test/_javascript_helpers.html +++ b/app/views/magic_test/_javascript_helpers.html @@ -32,6 +32,19 @@ var testingOutput = JSON.parse(sessionStorage.getItem("testingOutput")); testingOutput.push({action: action, target: target, options: options }); sessionStorage.setItem("testingOutput", JSON.stringify(testingOutput)); + + // Register Capybara actions for `alert` and `confirm` methods. + if (window.registerAcceptAlert) { + var testingOutput = JSON.parse(sessionStorage.getItem("testingOutput")); + testingOutput.push({action: "accept_alert", target: "", options: ""}) + sessionStorage.setItem("testingOutput", JSON.stringify(testingOutput)); + window.registerAcceptAlert = false; + } else if (window.registerAcceptConfirm) { + var testingOutput = JSON.parse(sessionStorage.getItem("testingOutput")); + testingOutput.push({action: "accept_confirm", target: "", options: ""}) + sessionStorage.setItem("testingOutput", JSON.stringify(testingOutput)); + window.registerAcceptConfirm = false; + } }; function keypressFunction(evt) { From 7aae8347f9b60bcbfe0a754e9aac26e6fd062f86 Mon Sep 17 00:00:00 2001 From: Gabriel Zayas Date: Tue, 11 Jul 2023 18:47:05 +0900 Subject: [PATCH 2/2] Allow assertion generation with keyboard shortcuts --- app/views/magic_test/_context_menu.html.erb | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/app/views/magic_test/_context_menu.html.erb b/app/views/magic_test/_context_menu.html.erb index 96a72a9..2e000f6 100644 --- a/app/views/magic_test/_context_menu.html.erb +++ b/app/views/magic_test/_context_menu.html.erb @@ -11,16 +11,20 @@ (function() { var _old_alert = window.alert; window.alert = function() { - window.registerAcceptAlert = true; - _old_alert.apply(window, arguments); + if(!window.generatingAssertion) { + window.registerAcceptAlert = true; + } + return _old_alert.apply(window, arguments); }; })(); (function() { var _old_confirm = window.confirm; window.confirm = function() { - window.registerAcceptConfirm = true; - _old_confirm.apply(window, arguments); + if(!window.generatingAssertion) { + window.registerAcceptConfirm = true; + } + return _old_confirm.apply(window, arguments); } })(); @@ -48,6 +52,8 @@ var target = ""; var options = ""; var accept = "You selected:\n\r" + text + "\n\rOk: Type `flush` into debugger console to add to test.\nCancel: To select new text." + + window.generatingAssertion = true; if (window.confirm(accept)) { testingOutput.push({action: action, target: target, options: options}); sessionStorage.setItem("testingOutput", JSON.stringify(testingOutput)); @@ -55,6 +61,7 @@ else { console.log("Assertion was not generated.") } + window.generatingAssertion = false; } }