Detecting console errors while testing AngularJS with Protractor

In a previous post I made a remark that when end to end testing routes on AngularJS with Protractor we might find our tests passing even when Angular throws an exception.

In which situation can checking for console errors help

In the example from that post, I was testing private routes (such as /profile) which had their own controller defined. They redirected to (/home) if logged in, otherwise stayed on the private page. Login was done from /home, which was completely independent from the ProfileCtrl.

Let's imagine the profile controller threw an exception. The test would execute as follows:

Navigate to /home - OK

Login using Auth0 - OK

Redirect to /profile and expect the route to NOT change - OK

To properly do the test we'll also check for the opposite: if we go on /profile without being logged in we should to be redirected to /home. This would indeed make the test fail and solve our problem (at least initially). But what if an exception is being thrown in the profile controller only while being logged in?

Where to check the console

This is just one case where we would be fooled by the test, but there are many others. To be sure that we cover as many unforeseen situations, we can check the console in a 'afterEach' block:

While this will ensure that we are doing a more thorough test, it will also fail our tests when we don't want it to. Sometimes when our testing workflow includes third party modules, there will be console messages that we can't control.
Even worse, some browsers will pollute the console with all kinds of information, such as Chrome does:

"Consider using 'dppx' units, as in CSS 'dpi' means dots-per-CSS-inch, not dots-per-physical-inch, so does not correspond to the actual 'dpi' of a screen."

It might be a good idea to make the console check a helper method, as seen here. In this post, I'll just have it in the afterEach block to keep things simple.

Console reading

If you are planning to do this on a headless server using PhantomJS: it's not gonna happen (as of Version 1.9). Phantom will crash and burn every time you try to read the console, so you need to do some tricks to make Chrome / Firefox work on a headless server.

Fine-tuning

It's easy to spot how console.info()'s and console.warn()'s will make our life hard. As I mentioned, they might come from 3rd party modules or from the browser itself.
Moreover, if you are running tests on your everyday browser, pay attention to what extensions/plugins you have on or just disable them altogether. Often browser extensions throw all kinds of funny exceptions that you don't need to have in your console.

With that out of the way, we can be a bit more sure that at least the console.error()'s will come from the tested app. To only check for SEVERE messages, you can do the following:

If you run into trouble while checking the console inside an afterEach block, I suggest using it as a helper method so you can control exactly when the console is checked. Plus, you won't really need to check the console for every single assertion.

There you have it, now you can sleep better knowing that you're one step closer to having a well-tested application.