2019-01-13 23:58:35 +01:00
|
|
|
function plugin (wdInstance, requests) {
|
2019-09-11 15:58:51 +02:00
|
|
|
if (typeof wdInstance.addCommand !== "function") {
|
|
|
|
throw new Error("You can't use WebdriverAjaxStub with this version of WebdriverIO");
|
2019-01-13 23:58:35 +01:00
|
|
|
}
|
|
|
|
|
2020-02-10 18:56:55 +01:00
|
|
|
function stub({template, data}, done) {
|
2019-01-13 23:58:35 +01:00
|
|
|
window.XMLHttpRequest = function () {
|
|
|
|
this.open = function (method, url) {
|
|
|
|
this.method = method;
|
|
|
|
this.url = url;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.send = function () {
|
|
|
|
this.status = 200;
|
|
|
|
this.readyState = 4;
|
2020-02-13 08:35:09 +01:00
|
|
|
const response = this.url.includes(".njk") ? template : data;
|
2019-01-13 23:58:35 +01:00
|
|
|
this.response = response;
|
|
|
|
this.responseText = response;
|
|
|
|
this.onreadystatechange();
|
|
|
|
};
|
|
|
|
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
|
2019-09-11 15:58:51 +02:00
|
|
|
wdInstance.addCommand("setupStub", function() {
|
2019-01-13 23:58:35 +01:00
|
|
|
return wdInstance.executeAsync(stub, requests);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.init = plugin;
|