How can I report JavaScript errors that occur during test execution using Intern? Basically, if there are any JavaScript errors on the page (even as part of things that aren't explicitly tested) I want to know.
Background
I'm just getting started with Intern and testing in general and I'm trying to test all major pages on my site in all browsers because I just changed all our JavaScript to load via require.js. While it looks good in Chrome, I've had issues with require.js and random browsers in the past so I wanted to automate everything. The most likely issue that will arise is that some random JS will fail to execute due to asynchronous loading and load of an expected global. Since there are no current tests setup, I basically want to start by running a 'test' go to through all major pages and report any JavaScript errors.
In order to report uncaught errors, you need to hook the window.onerror method of the page. This is possible, but the page load will need to be finished before you add the hook, which means that any errors that occur before/during the page load (or that occur while the page unloads) simply cannot be caught and reported. It also means if you perform an action that moves to a new page (like a form submission), you will need to make sure you retrieve the list of errors before you perform the action that causes navigation, and reconfigure the window.onerror handler after you get to the new page.
To perform such reporting with a functional test, your test would end up looking something like this:
return this.remote
.get('http://example.com')
.execute(function () {
window.__internErrors__ = [];
window.onerror = function () {
__internErrors__.push(Array.prototype.slice.call(arguments, 0));
};
})
// ... interact with the page ...
.execute(function () {
return window.__internErrors__;
})
.then(function (errors) {
// read `errors` array to get list of errors
});
Note that (as of August 2014) errors from window.onerror in all browsers except recent versions of Chrome provide only the message, script source, line number, and (sometimes) column number, so this information would only be useful to say “this action caused an error, go do it manually to get a stack trace”.
During unit tests, Intern already tries to automatically catch any unhandled errors and treats them as fatal errors that halt the system (since you should never have code that generates this kind of unhandled error).
Related
I have taken over an Electron project from another developer.
The problem I am facing is that the project does not show any errors. Even including something like throw "this is an error" does not produce any output on the main process or render process consoles or any sort of standard error popup.
I have checked to confirm that electron-unhandled is not in use and that nothing registers 'uncaughtException'.
What am I missing that could cause this behavior?
Search for: unhandledRejection
unhandledRejection : This will catch any thrown errors, or non fatal errors you have successfully handled via throw.
uncaughtException : This only catches fatal errors or errors that would crash your node instance
WebWorkers : There will be yet another console for webworkers if your using those.
package.json : Take a look in here at the script executed to start electron or however your starting it... Make sure the console is not being sent to a remote console. This feature would allow for debugging the application via Chrome/Firefox vs the standard console. Pretty common for electron apps. If is done via the startup command.
May look something like this:
process.on('unhandledRejection', function (err) {
});
Also, make sure you include any modules in your searching for suppressors as the issue may exist somewhere in the node_modules directory and many IDE's (mine does by default) exclude that directory in indexing/searches.
Another possible reason could be stdout and/or stderr redirection, the problem is this could be achieved by several ways so it's hard to suggest you what to check...
If there is some child_process call to launch a sub-process you could check the stdio array used, or you can check if some low level operation is performed against file descriptors 1 and 2...
Hope this helps.
Are you facing the problem as mentioned in this official thread. You may disable the original event listeners and manage the ELECTRON_BROWSER_WINDOW_ALERT event by my event listener.
Here is the solution
ipcMain.removeAllListeners("ELECTRON_BROWSER_WINDOW_ALERT")
ipcMain.on("ELECTRON_BROWSER_WINDOW_ALERT", (event, message, title)=>{
console.warn(`[Alert] ** ${title} ** ${message}`)
event.returnValue = 0 // **IMPORTANT!**
})
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/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.
I am running a Tampermonkey script on a website that I do not have the code for.
Sometimes it happens that I have a value that does not exist on the page and I get the following error:
"Cannot read property 'click' of null"
And the entire script stops. How can I tell get my script to ignore the error and just carry on to the next line of code?
Here is an example of a vanilla Javascript line that I work with:
document.querySelector('[value="xyz"]').click();
Only execute click() if the selector found something:
if(document.querySelector('[value="xyz"]'))
document.querySelector('[value="xyz"]').click();
You can't, and you shouldn't want to: errors are bad. They're not informative, they are a signal that the code has run into an unrecoverable error and the current code path should be terminated. If you were to ignore it, and keep running, now you're in a state where any subsequent line is just as likely to also throw an error.
Either actually fix things, by making your tampermonkey script not interfere with the way the page it's running on builds its DOM, or as a last resort, you can find out which function is throwing the error for the specific page(s) you're running into this, and then _specifically for those pages, find and rebind the entire function using a try/catch, such as:
const _old_fn = window.theFunctionInvolved;
window.theFunctionInvolved = function(...args) {
try { return _old_fn(...args); }
catch (e) {}
};
But of course, all you've now done is moved the buck: you'll have effectively guaranteed different errors later on, with the actual cause now permanently hidden.
So really: don't do this. Fix your tampermonkey script, or stop using it altogether.
I'm running tests with jasmine/karma and although all tests run ok, process exits with 1. I suspect that these are Firefox's web console error messages, which are produced when I try to add existing record into IndexedDB object store. Is there any option to disable these messages or ignore them in Karma runner?
UPD: These errors occur in web console even if I wrap my code inside try..catch, so it's browsers default behaviour.
https://travis-ci.org/1999/sklad/builds/36609101
Just in case someone needs this. The answer is here. In brief, Firefox shows that this is a usual DOM error and it has default behavior. So if you prevent default 'onerror' behavior which is 'abort', everything goes fine. You can see the result here.
I'd like to write a test case (using Selenium, but not the point of this question) to validate that my web application has no script errors\warnings or unhanded exceptions at certain points in time (like after initializing a major library).
This information can easily be seen in the debug consoles of most browsers. Is it possible to execute a javascript statement to get this information programatically?
It's okay if it's different for each browser, I can deal with that.
not so far read about your issue (as far as I understood your problem) here
The idea be the following:
I found, however, that I was often getting JavaScript errors when the page first loaded (because I was working on the JS and was introducing errors), so I was looking for a quick way to add an assert to my test to check whether any JS errors occurred. After some Googling I came to the conclusion that there is nothing built into Selenium to support this, but there are a number of hacks that can be used to accomplish it. I'm going to describe one of them here. Let me state again, for the record, that this is pretty hacky. I'd love to hear from others who may have better solutions.
I simply add a script to my page that will catch any JS errors by intercepting the window.onerror event:
<script type="text/javascript">
window.onerror=function(msg){
$("body").attr("JSError",msg);
}
</script>
This will cause an attribute called JSError with a value corresponding to the JavaScript error message to be added to the body tag of my document if a JavaScript error occurs. Note that I'm using jQuery to do this, so this specific example won't work if jQuery fails to load. Then, in my Selenium test, I just use the command assertElementNotPresent with a target of //body[#JSError]. Now, if any JavaScript errors occur on the page my test will fail and I'll know I have to address them first. If, for some strange reason, I want to check for a particular JavaScript error, I could use the assertElementPresent command with a target of //body[#JSError='the error message'].
Hope this fresh idea helps you :)
try {
//code
} catch(exception) {
//send ajax request: exception.message, exception.stack, etc.
}
More info - MDN Documentation