Skip to content Skip to sidebar Skip to footer

Is There A Way To Capture Js Errors But Not Fail Testcafe Tests Because Of Them?

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

Solution 1:

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/common-concepts/inject-scripts-into-tested-pages.html#inject-script-code

I recommend 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 command will be the following: testcafe chrome test.js --skip-js-errors.

Post a Comment for "Is There A Way To Capture Js Errors But Not Fail Testcafe Tests Because Of Them?"