25

Is there a way of quitting test suite and stop executing further test cases, if a test case fails in protractor?

alecxe
  • 414,977
  • 106
  • 935
  • 1,083
Aman Gupta
  • 2,831
  • 3
  • 28
  • 55
  • 1
    Which test runner framework are you using in protractor? ("jasmine" is the default) – P.T. Mar 06 '15 at 06:31

4 Answers4

18

In case of jasmine testing framework, you are not the first asking about it.

There are relevant open discussions/issues on exiting after a first failure, --fail-fast option:

Long story short, this is an open issue and some day jasmine would have the functionality built-in. Currently, use a third-party jasmine-bail-fast module.

Aside from that, there is a handy realtimeFailure jasmine setting. If you set it to true it would not fail the whole test run, but it would show errors in a real time - immediately after happening - this can possibly cover your use case. Set it in jasmineNodeOpts:

exports.config = {
    seleniumAddress: 'http://127.0.0.1:4444/wd/hub',

    ...

    jasmineNodeOpts: {
        realtimeFailure: true
    }
}
alecxe
  • 414,977
  • 106
  • 935
  • 1,083
  • According to this question: http://stackoverflow.com/questions/22119193/stop-jasmine-test-after-first-expect-fails jasmine now has the feature as of 2.3.0. In a comment to http://stackoverflow.com/a/31809311/571380 the person mentions `stopSpecOnExpectationFailure` to be put in the config. However, I tried `exports.config = { ..., jasmineNodeOpts: { stopSpecOnExpectationFailure: true } }` in my conf.js file and that did not work for me. – Machtyn May 19 '16 at 20:46
  • 2
    @Machtyn could you please create a separate question and let's see if this is built-in now and works? Thanks! – alecxe May 19 '16 at 20:51
  • @alecxe Thanks and done: http://stackoverflow.com/questions/37334069/protractor-jasmine-and-stopping-test-on-first-fail – Machtyn May 19 '16 at 21:12
11

Here is my solution to skip tests on first fail with Jasmine 2 and Protractor. Hope it helps.

exports.config = {
    onPrepare: function () {
        //skip tests after first fail
        var specs = [];
        var orgSpecFilter = jasmine.getEnv().specFilter;
        jasmine.getEnv().specFilter = function (spec) {
            specs.push(spec);
            return orgSpecFilter(spec);
        };
        jasmine.getEnv().addReporter(new function () {
            this.specDone = function (result) {
                if (result.failedExpectations.length > 0) {
                    specs.forEach(function (spec) {
                        spec.disable()
                    });
                }
            };
        });
    }
};
Daniel Schwab
  • 111
  • 1
  • 2
9

jasmine-bail-fast didn't work in my case. Not sure if it was because of some conflicts with my other report plugins.

In case anyone is having the same problem. You can try protractor-fast-fail

import failFast from 'protractor-fail-fast';

exports.config = {
  // if import statement doesn't work, use this instead of import for older versions of node
  // plugins: [{
  //  package: 'protractor-fail-fast'
  // }],

  onPrepare: function() {
    jasmine.getEnv().addReporter(failFast.init());
  },

  afterLaunch: function() {
    failFast.clean(); 
  }  
}

Worked perfectly well for me.

EDIT: added import statement in code snippet to reflect readme of projactor-fast-fail

Linh Pham
  • 2,905
  • 19
  • 31
  • 2
    same here, for me also bail-fast did't work but protractor-fast-fail worked perfectly. – FarazShuja Apr 19 '16 at 06:08
  • Hi i am getting ReferenceError: failFast is not defined – PDHide Dec 19 '19 at 15:46
  • @PDHide check their npm/github page. Plugin import seem to be changed. Add this line `import failFast from 'protractor-fail-fast';` at the start of your file. *(it's a good practice to not just copy code from stackoverflow, instead try to use it as a reference)* – Linh Pham Dec 25 '19 at 09:33
  • @LinhPham I am using JavaScript , and import fsilfsst from shows syntax error – PDHide Dec 28 '19 at 18:04
4

you don't need all those third party plugins. Use native process.exit().

Code example:

it("test", function()
{
   ...
   if(isExit)
   {
      browser.driver.close().then(function()
      {
         process.exit(1);
      });
   }
});

profit.