From 239d42594086c2ea43791cb2b90a109d91f8c8f8 Mon Sep 17 00:00:00 2001 From: fewieden Date: Sun, 13 Jan 2019 23:58:35 +0100 Subject: [PATCH] webdriver ajax stub --- .gitignore | 4 +-- tests/node_modules/webdriverajaxstub/index.js | 33 +++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 tests/node_modules/webdriverajaxstub/index.js diff --git a/.gitignore b/.gitignore index 6336aabc..807ad5be 100644 --- a/.gitignore +++ b/.gitignore @@ -11,7 +11,7 @@ coverage .grunt .lock-wscript build/Release -node_modules +/node_modules/**/* jspm_modules .npm .node_repl_history @@ -82,4 +82,4 @@ Temporary Items *.rej *.bak -!/tests/node_modules/webdriverajaxstub/index.js \ No newline at end of file +!/tests/node_modules/**/* \ No newline at end of file diff --git a/tests/node_modules/webdriverajaxstub/index.js b/tests/node_modules/webdriverajaxstub/index.js new file mode 100644 index 00000000..d11eaf9d --- /dev/null +++ b/tests/node_modules/webdriverajaxstub/index.js @@ -0,0 +1,33 @@ +function plugin (wdInstance, requests) { + if (typeof wdInstance.addCommand !== 'function') { + throw new Error('You can\'t use WebdriverAjaxStub with this version of WebdriverIO'); + } + + function stub(requests, done) { + window.XMLHttpRequest = function () { + this.open = function (method, url) { + this.method = method; + this.url = url; + }; + + this.send = function () { + this.status = 200; + this.readyState = 4; + const response = requests.shift() || []; + this.response = response; + this.responseText = response; + this.onreadystatechange(); + }; + + return this; + }; + + done(); + } + + wdInstance.addCommand('setupStub', function() { + return wdInstance.executeAsync(stub, requests); + }); +} + +module.exports.init = plugin;