From 5546c85d5bbb3b88508630b05daefd53f315c386 Mon Sep 17 00:00:00 2001 From: Alfonso Presa Date: Wed, 27 Jul 2016 21:51:52 +0200 Subject: [PATCH] feat(XMLHttpRequest): Now this is the change for checking outstanding requests --- dist/protractor-testability-plugin.js | 32 +++++++++++------------ dist/protractor-testability-plugin.min.js | 5 ---- lib/protractor-testability-plugin.js | 32 +++++++++++------------ 3 files changed, 32 insertions(+), 37 deletions(-) delete mode 100644 dist/protractor-testability-plugin.min.js diff --git a/dist/protractor-testability-plugin.js b/dist/protractor-testability-plugin.js index 2f53e3a..0904434 100644 --- a/dist/protractor-testability-plugin.js +++ b/dist/protractor-testability-plugin.js @@ -46,23 +46,10 @@ return { } }; - window.getAllAngularTestabilities = function() { - return [ window.angular.getTestability() ]; + window.getAllAngularTestabilities = function () { + return [window.angular.getTestability()]; }; } - function onload() { - if (window.$) { - window.$.ajaxStart(window.testability.wait.oneMore); - window.$.ajaxStop(window.testability.wait.oneLess); - } - } - - if (window.addEventListener) { - window.addEventListener('onload', onload); - } - else { - window.attachEvent('load', onload); - } function patchFunction(set, clear, filterTime) { var setFn = window[set]; @@ -121,12 +108,25 @@ return { patchFunction('setInterval', 'clearInterval', true); patchFunction('setImmediate', 'clearImmediate', false); + var oldOpen = XMLHttpRequest.prototype.open; + XMLHttpRequest.prototype.open = function (method, url, async, user, pass) { + if(window.testability) { + testability.wait.oneMore(); + this.addEventListener('readystatechange', function () { + if (this.readyState === 4) { + testability.wait.oneLess(); + } + }, false); + } + oldOpen.call(this, method, url, async, user, pass); + }; + patchPromiseFunction('fetch'); }); }, waitForPromise: function () { return browser.executeAsyncScript( - 'return testability.when.ready.apply(null,arguments)') + 'return window.testability && window.testability.when.ready.apply(null,arguments)') .then(function (browserErr) { if (browserErr) { throw 'Error while waiting to sync with the page: ' + JSON.stringify(browserErr); diff --git a/dist/protractor-testability-plugin.min.js b/dist/protractor-testability-plugin.min.js deleted file mode 100644 index afd8bfe..0000000 --- a/dist/protractor-testability-plugin.min.js +++ /dev/null @@ -1,5 +0,0 @@ -/*! protractor-testability-plugin - v1.0.2 - * Release on: 2016-07-27 - * Copyright (c) 2016 Alfonso Presa - * Licensed MIT */ -!function(a,b){"function"==typeof define&&define.amd?define([],function(){return b()}):"object"==typeof exports?module.exports=b():b()}(this,function(){"use strict";var a=require("fs");return{name:"protractor-testability-plugin",onPageLoad:function(){var b=a.readFileSync(require.resolve("testability.js")).toString();browser.executeScript("if(!window.testability) {"+b+"}"),browser.executeScript(function(){function a(){window.$&&(window.$.ajaxStart(window.testability.wait.oneMore),window.$.ajaxStop(window.testability.wait.oneLess))}function b(a,b,c){var d=window[a],e=window[b],f={};window[a]=function(){var a,b=arguments[0],e=arguments[1];return arguments[0]=function(){f[a]=void 0,(!c||5e3>e)&&window.testability.wait.oneLess(),b.apply(window,arguments)},a=d.apply(window,arguments),(!c||5e3>e)&&(window.testability.wait.oneMore(),f[a]=!0),a},window[b]=function(){return f[arguments[0]]&&(window.testability.wait.oneLess(),f[arguments[0]]=void 0),e.apply(window,arguments)}}function c(a){var b=window[a];window[a]=function(){var a;return a=b.apply(window,arguments),a.then(function(a){return window.testability.wait.oneLess(),a}),window.testability.wait.oneMore(),a}}window.angular||(window.angular={resumeBootstrap:function(){},module:function(){return{config:function(){return this}}},getTestability:function(){return{whenStable:function(a){a()}}}},window.getAllAngularTestabilities=function(){return[window.angular.getTestability()]}),window.addEventListener?window.addEventListener("onload",a):window.attachEvent("load",a),b("setTimeout","clearTimeout",!0),b("setInterval","clearInterval",!0),b("setImmediate","clearImmediate",!1),c("fetch")})},waitForPromise:function(){return browser.executeAsyncScript("return testability.when.ready.apply(null,arguments)").then(function(a){if(a)throw"Error while waiting to sync with the page: "+JSON.stringify(a)})}}}); \ No newline at end of file diff --git a/lib/protractor-testability-plugin.js b/lib/protractor-testability-plugin.js index ec4e832..60a214c 100644 --- a/lib/protractor-testability-plugin.js +++ b/lib/protractor-testability-plugin.js @@ -26,23 +26,10 @@ return { } }; - window.getAllAngularTestabilities = function() { - return [ window.angular.getTestability() ]; + window.getAllAngularTestabilities = function () { + return [window.angular.getTestability()]; }; } - function onload() { - if (window.$) { - window.$.ajaxStart(window.testability.wait.oneMore); - window.$.ajaxStop(window.testability.wait.oneLess); - } - } - - if (window.addEventListener) { - window.addEventListener('onload', onload); - } - else { - window.attachEvent('load', onload); - } function patchFunction(set, clear, filterTime) { var setFn = window[set]; @@ -101,12 +88,25 @@ return { patchFunction('setInterval', 'clearInterval', true); patchFunction('setImmediate', 'clearImmediate', false); + var oldOpen = XMLHttpRequest.prototype.open; + XMLHttpRequest.prototype.open = function (method, url, async, user, pass) { + if(window.testability) { + testability.wait.oneMore(); + this.addEventListener('readystatechange', function () { + if (this.readyState === 4) { + testability.wait.oneLess(); + } + }, false); + } + oldOpen.call(this, method, url, async, user, pass); + }; + patchPromiseFunction('fetch'); }); }, waitForPromise: function () { return browser.executeAsyncScript( - 'return testability.when.ready.apply(null,arguments)') + 'return window.testability && window.testability.when.ready.apply(null,arguments)') .then(function (browserErr) { if (browserErr) { throw 'Error while waiting to sync with the page: ' + JSON.stringify(browserErr);