Search code examples
angularjasmineprotractorangular-testangular-e2e

Catch console errors while running e2e tests with jasmine and protractor


While running e2e tests (protractor, selenium) I want to catch any console errors and warnings generated by application.

I know protractor plugin protractor-console-plugin which catches console and make tests fail but it prints all logs from all tests at the end and I don't know during which test case the log was created.

Also I know protractor-console which does good job in presenting console logs after the test case but it can't mark test as failed in case of console.error.

My package.json (important part here) is:

"jasmine-core": "^2.8.0",
"jasmine-spec-reporter": "^4.2.1",
"karma": "^1.7.1",
"karma-chrome-launcher": "^2.2.0",
"karma-cli": "~1.0.1",
"karma-coverage-istanbul-reporter": "^1.3.0",
"karma-jasmine": "~1.1.0",
"karma-jasmine-html-reporter": "^0.2.2",
"protractor": "^5.1.2",
"protractor-console": "^3.0.0",
"protractor-console-plugin": "^0.1.1",
"protractor-jasmine2-screenshot-reporter": "^0.4.1",

Solution

  • I'm using a function like this:

    public static async browserErrorLogger() {
        const browserLogs = await browser.manage().logs().get('browser');
        browserLogs.forEach((log) => {
          if (log.level.value > 900) { // it's an error log
            console.log(`Browser console error: ${log.message}`);
            // if you want to fail on warning add this
            fail(log.message);
          }
        });
      }
    

    Then you can call this function in beforeEach() in spec file.