Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exit Protractor test from Spec on specific condition?

I have a suite which includes multiple specs. Each spec uses code on some libraries that return a rejected promise upon failure.

I can easily catch those rejected promises inside my spec. What I'm wondering about is that if I can make Protractor exit the whole suite inside that catch function, because the next specs inside the same suite are dependent on the success of the previous specs.

Pretend I have a suite called testEverything which has these specs openApp,signIn,checkUser,logout. If openApp fails, all next specs will fail due to dependency.

Consider this code for openApp:

var myLib = require('./myLib.js');

describe('App', function() {

  it('should get opened', function(done) {

    myLib.openApp()
    .then(function() {

      console.log('Successfully opened app');

    })
    .catch(function(error) {

      console.log('Failed opening app');

      if ( error.critical ) {
        // Prevent next specs from running or simply quit test
      }

    })
    .finally(function() {

      done();

    });

  });

});

How would I exit the whole test?

like image 683
Ramtin Soltani Avatar asked Oct 30 '22 04:10

Ramtin Soltani


1 Answers

There is a module for npm called protractor-fail-fast. Install the module npm install protractor-fail-fast. Here's an example from their site where you would place this code into your conf file:

var failFast = require('protractor-fail-fast');

exports.config = {
  plugins: [{
    package: 'protractor-fail-fast'
  }],

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

  afterLaunch: function() {
    failFast.clean(); // Cleans up the "fail file" (see below) 
  }  
}

Their url is here.

like image 93
king_wayne Avatar answered Nov 16 '22 17:11

king_wayne