Saturday, November 14, 2015

On Protractor: End to End (e2e) tests should fail if there are javascript errors

End to End (e2e) tests should fail if there are javascript errors. They most likely won't cover all the application functionality, but many (if not most) of the regressions we encounter in production are related to javascript errors. Here is how to stop your continuous delivery pipeline if there is a single javascript error at runtime. Note that at a minimum smoke e2e tests should exist meaning that all of your views should be tested at least expecting to render html, not having unexpected logical errors and not having javascript errors. Webdriver allows us to get a hold of the browser console log. This means we can detect if there are javascript errors at runtime and make our tests fail. For more info see https://github.com/angular/protractor/blob/master/docs/faq.md#how-can-i-get-hold-of-the-browsers-console. Protractor makes our lifes easier though. All we need to do is to include the below configuration in protractor.conf.js:
...
exports.config = {
  ...
  plugins: [{
    path: 'node_modules/protractor-console-plugin',
    failOnWarning: true,
    failOnError: true
  }],
  ...
...
// To test the solution just force a JS error that does not stop your test from passing. // For example adding a simple script tag in your index.html:
  <script>
    console.undefinedMethod('will never run');
  </script>
After the test is run you get:
Plugin: /usr/local/lib/node_modules/protractor/plugins/console/index.js (teardown) Fail: Console output SEVERE: https://doma.in:port/path/ 72:17 Uncaught TypeError: console.undefinedMethod is not a function [launcher] 0 instance(s) of WebDriver still running [launcher] chrome #1 failed 1 test(s) [launcher] overall: 1 failed spec(s) [launcher] Process exited with error code 1

No comments:

Followers