diff --git a/app/views/magic_test/_context_menu.html.erb b/app/views/magic_test/_context_menu.html.erb index 40370ca..2e000f6 100644 --- a/app/views/magic_test/_context_menu.html.erb +++ b/app/views/magic_test/_context_menu.html.erb @@ -7,6 +7,27 @@ } } + // Override the alert and confirm functions to register capybara actions. + (function() { + var _old_alert = window.alert; + window.alert = function() { + if(!window.generatingAssertion) { + window.registerAcceptAlert = true; + } + return _old_alert.apply(window, arguments); + }; + })(); + + (function() { + var _old_confirm = window.confirm; + window.confirm = function() { + if(!window.generatingAssertion) { + window.registerAcceptConfirm = true; + } + return _old_confirm.apply(window, arguments); + } + })(); + ready(function() { enableKeyboardShortcuts(); }); @@ -31,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)); @@ -38,6 +61,7 @@ else { console.log("Assertion was not generated.") } + window.generatingAssertion = false; } } 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) {