diff --git a/stere/browser_spy/xhr_script.py b/stere/browser_spy/xhr_script.py index 6f063502..2597f5a0 100644 --- a/stere/browser_spy/xhr_script.py +++ b/stere/browser_spy/xhr_script.py @@ -6,49 +6,34 @@ // Number of requests that have been completed. document.totalXHRrequests = 0; -// Patch a callback system onto XMLHttpRequest.send() -// Before a request is sent, a callback is run. -// Add a callback to the callback queue. -// If no patch exists yet, it will be created -function addXHRCallback(callback) { - if (XMLHttpRequest.callbacks) { - // Already overridden send() so just add the callback - XMLHttpRequest.callbacks.push(callback); - } else { - // Create a callback queue - XMLHttpRequest.callbacks = [callback]; - // Store the native send() - var oldSend = XMLHttpRequest.prototype.send; - // Override the native send() - XMLHttpRequest.prototype.send = function() { - // Process the callback queue - for (var i = 0; i < XMLHttpRequest.callbacks.length; i++) { - XMLHttpRequest.callbacks[i](this); - } - // Call the native send() - oldSend.apply(this, arguments); - } +// Prevent patch from stacking if applied multiple times. +if (!window.oldSend) { + window.oldSend = XMLHttpRequest.prototype.send; + + // Patch a request counter onto XMLHttpRequest.send() + XMLHttpRequest.prototype.send = function() { + countRequests(this); + // Call the native send() + window.oldSend.apply(this, arguments); } -} -// Increment a counter every time a request starts, -// and adds an event to decrement it when the request is complete. -addXHRCallback( - function(xhr) { - document.activeXHRrequests += 1; - document.totalXHRrequests += 1; + // Increment a counter every time a request starts, + // and add an event to decrement it when the request is complete. + var countRequests = function(xhr) { + document.activeXHRrequests += 1; + document.totalXHRrequests += 1; - xhr.addEventListener( - 'loadend', () => { - // Decrement counter - document.activeXHRrequests -= 1; + xhr.addEventListener( + 'loadend', () => { + // Decrement counter + document.activeXHRrequests -= 1; - // For live debugging - var remaining_msg = `remaining: ${document.activeXHRrequests}`; - var total_msg = `total: ${document.totalXHRrequests}`; - console.log('XHR loadend,', remaining_msg, total_msg); - } - ) - } -); + // For live debugging + var remaining_msg = `remaining: ${document.activeXHRrequests}`; + var total_msg = `total: ${document.totalXHRrequests}`; + console.log('XHR loadend,', remaining_msg, total_msg); + } + ) + } +} """ diff --git a/tests/splinter/test_spy.py b/tests/splinter/test_spy.py index bd0b4880..0d1e229b 100644 --- a/tests/splinter/test_spy.py +++ b/tests/splinter/test_spy.py @@ -89,7 +89,6 @@ def test_xhr_spy_not_added(xhr_test_page): xhr_test_page.xhr_spy.wait_for_no_activity() -@pytest.mark.xfail def test_xhr_spy_multiple_add(xhr_test_page): """ When I add the XHR spy to the page multiple times