I'm currently starting to write some TestCafe tests, and came across an issue in our website whilst running them - a JS error in the console fails the test. Naturally, I was quite pleased that my test had caught this, but it would mean that even if a JS error happens that is low priority and affects no users directly, our tests may fail and prevent a build.
Now this may be a workflow some want, but for us we'd rather raise a ticket and address it in a parallel workflow rather than block everyone because of a JS error. I'm aware of the --skip-js-errors
option, however this just throws away all the errors entirely. Is there a middle ground, like converting the errors to warnings, or simply adding some sort of after-test function that logs out any JS errors that occurred during the test run? I've tried adding an afterEach
to my fixture like so:
.afterEach(async t => {
const { error } = await t.getBrowserConsoleMessages();
console.log(JSON.stringify(error));
});
But with --skip-js-errors
this does nothing. I'd love some pointers on this please!
My goal, in case it wasn't clear - I want to see the possible JS errors in my TestCafe run so that I can log them and make tickets off them, but I don't want them to fail the test run.
I'm currently starting to write some TestCafe tests, and came across an issue in our website whilst running them - a JS error in the console fails the test. Naturally, I was quite pleased that my test had caught this, but it would mean that even if a JS error happens that is low priority and affects no users directly, our tests may fail and prevent a build.
Now this may be a workflow some want, but for us we'd rather raise a ticket and address it in a parallel workflow rather than block everyone because of a JS error. I'm aware of the --skip-js-errors
option, however this just throws away all the errors entirely. Is there a middle ground, like converting the errors to warnings, or simply adding some sort of after-test function that logs out any JS errors that occurred during the test run? I've tried adding an afterEach
to my fixture like so:
.afterEach(async t => {
const { error } = await t.getBrowserConsoleMessages();
console.log(JSON.stringify(error));
});
But with --skip-js-errors
this does nothing. I'd love some pointers on this please!
My goal, in case it wasn't clear - I want to see the possible JS errors in my TestCafe run so that I can log them and make tickets off them, but I don't want them to fail the test run.
TestCafe does not provide such functionality out of the box. As you correctly mentioned, the --skip-js-errors
flag ignores all errors and does not log them.
However, you can achieve the desired functionality using the Script Injecting mechanism. Please refer to the following article for more details: https://devexpress.github.io/testcafe/documentation/using-testcafe/mon-concepts/inject-scripts-into-tested-pages.html#inject-script-code
I remend you continue using the --skip-js-errors
flag and add a custom window.onerror
handler. Please see the example:
fixture `fixture`
.page `../pages/index.html`;
test.clientScripts({
content: `
window.addEventListener('error', function (e) {
console.error(e.message);
});`
})(`Skip error but log it`, async t => {
console.log(await t.getBrowserConsoleMessages());
});
In this code, I add the error
event handler. Inside the handler, I call the console.error
method. In this case, t.getBrowserConsoleMessages
will return the correct log of errors.
Please use this approach along with the --skip-js-error
flag.
Thus, the mand will be the following: testcafe chrome test.js --skip-js-errors
.