I'm working on a legacy app that makes a lot of calls to external sources. I'm trying to refactor this app, and I've written testcafe tests to help inform me when I've made a mistake. I'm NOT running my tests with --skip-js-errors
, but when I get 404 errors and the console prints this out:
the test doesn't stop. I'd like errors like these to be something I am informed about. How do I make 4xx and 5xx network responses fail testcafe? I'm using angular 1.2 if that matters. If I could, I would change all the remote calls to throw an exception on 4xx or 5xx, but this is legacy code I don't understand, and I'm sure doing that would break a feature.
5xx Status Codes. A 5xx code means the problem was caused by the server. With a 5xx code, the request can be present with no changes and you will get the requested result when the server has been fixed. With a 4xx code, typically the client or user has to fix an error before trying again, but there are some exceptions.
In REST, both 4xx and 5xx types of codes denote an error. The main difference between the two is whose fault that error is. A 4xx code indicates an error caused by the user, whereas 5xx codes tell the client that they did everything correctly and it's the server itself who caused the problem.
I suggest you extend RequestLogger to check a request. You can throw an error based on the request status. For example:
import EventEmitter from 'events';
import { RequestHook } from 'testcafe';
fixture `test`
.page('https://testcafe.devexpress.com/Details2/')
class FailedRequestsLogger extends RequestHook {
constructor (...args) {
super(...args);
this.events = new EventEmitter();
this.failedRequestPromise = new Promise(resolve => this.events.once('failed-request', resolve));
}
onRequest (request) {
}
onResponse (response) {
if (response.statusCode >= 400)
this.events.emit('failed-request', response.statusCode);
}
waitForFailedRequest (action) {
return Promise.race([
action(),
this.failedRequestPromise.then(statusCode => Promise.reject(new Error(`Request failed with the ${statusCode} status code`)))
])
}
}
const logger = new FailedRequestsLogger();
test.requestHooks(logger)('test', async t => {
await logger.waitForFailedRequest(async () => {
await t.click('body');
await t.wait(10000);
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With