Skip to content

Instantly share code, notes, and snippets.

@chmanie
Last active June 17, 2021 03:21
Show Gist options
  • Save chmanie/e616ef3b9d20321943e8 to your computer and use it in GitHub Desktop.
Save chmanie/e616ef3b9d20321943e8 to your computer and use it in GitHub Desktop.
Intercept and assert HTTP calls in Selenium
module.exports = {
setup: function setup () {
window.__expectations = [];
window.__requests = [];
var _XHR = window.XMLHttpRequest;
window.XMLHttpRequest = function () {
var xhr = new _XHR();
var originalOpen = xhr.open;
var lastMethod;
xhr.open = function () {
lastMethod = arguments[0];
originalOpen.apply(xhr, arguments);
};
xhr.addEventListener('load', function () {
window.__requests.push({
requestedMethod: lastMethod.toUpperCase(),
requestedURL: this.responseURL,
requestedStatus: this.status,
response: this.response
});
});
return xhr;
};
},
expectRequest: function expectRequest (expectedMethod, expectedURL, expectedStatus) {
window.__expectations.push({
expectedMethod: expectedMethod.toUpperCase(),
expectedURL: expectedURL,
expectedStatus: expectedStatus
});
},
getResponse: function (idx) {
return window.__requests[idx] && window.__requests[idx].response;
},
assertAllRequests: function assertAllRequests () {
window.__expectations.forEach(function (ex, idx) {
var request = window.__requests[idx];
if (window.__expectations.length !== window.__requests.length) {
throw new Error(
'Expected ' +
window.__expectations.length +
' requests but was ' +
window.__requests.length
);
}
if (request.requestedMethod !== ex.expectedMethod) {
throw new Error(
'Expected request to URL ' +
request.requestedURL +
' to have method ' +
ex.expectedMethod +
' but was ' + request.requestedMethod
);
}
if (ex.expectedURL.regex) {
var regex = new RegExp(ex.expectedURL.regex);
if (request.requestedURL && !request.requestedURL.match(regex)) {
throw new Error(
'Expected request ' +
idx +
' to match '
+ ex.expectedURL.regex +
' but was ' +
request.requestedURL
);
}
} else if (request.requestedURL !== ex.expectedURL) {
throw new Error(
'Expected request ' +
idx +
' to have URL '
+ ex.expectedURL +
' but was ' +
request.requestedURL
);
}
if (request.requestedStatus !== ex.expectedStatus) {
throw new Error(
'Expected request to URL ' +
request.requestedURL +
' to have status ' +
ex.expectedStatus +
' but was ' +
request.requestedStatus
);
}
});
}
}
var ajax = require('./interceptor.js');
client
.url('http://foo.bar')
.execute(ajax.setup)
.execute(
ajax.expectRequest,
'GET', // can be any method
'http://foo.bar/api/my_ajax_endpoint', // expected URL
200 // expected statuscode
)
.execute(
ajax.expectRequest,
'POST',
{ regex: '^/api/my_ajax_endpoint/.+/resource$' }, // can be also a regex
200
)
.click('#button') // do the user interaction that triggers the ajax requests
.pause(2000) // maybe pause for a bit to let the requests finish
.execute(ajax.getResponse, 0) // make assertions about responses (here: the first one)
.then(function (ret) {
var body = JSON.parse(ret.value); // this is the response body
assert.equal(body.myValue, 'baz');
})
// assert the request count, order, methods, URLs and statuscodes
.execute(ajax.assertAllRequests); // do this in the very end
@chmanie
Copy link
Author

chmanie commented Oct 27, 2015

Use it for your own good. MIT License.

@chmanie
Copy link
Author

chmanie commented Jan 20, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment