javascript - Is there a way to capture js errors but not fail testcafe tests because of them? - Stack Overflow

admin2025-04-20  0

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.

Share Improve this question edited Mar 24, 2020 at 9:31 Alex Skorkin 4,2743 gold badges27 silver badges48 bronze badges asked Jan 17, 2020 at 15:51 Jarob22Jarob22 3801 gold badge6 silver badges14 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745114219a285766.html

最新回复(0)