How to exit Spec's Protractor test for a specific condition?

I have a package that includes several specifications. Each specification uses code in some libraries that return a rejected promise after a failure.

I can easily catchreject promises inside my specification. I'm interested in the fact that if I can get Protractor to exit the entire set inside this function catch, because the following specifications inside the same package depend on the success of the previous specifications.

Pretend I have a set called testEverything, which has these specs openApp, signIn, checkUser, logout. If it openAppdoes not work, all subsequent specifications will not be fulfilled 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 do I get out of the whole test?

+4
source share
2 answers

I managed to come up with a workaround. Now the actual code I used is more complicated, but the idea is the same.

I added a global variable to the protractor configuration file with the name bail. Consider the following code at the top of the configuration file:

(function () {

  global.bail = false;

})();

exports.config: { ...

The code above uses an IIFE expression (an expression with an immediate call expression) that defines a variable bailon the protractor object global(which will be available throughout the test).

Jasmine , expect, , ( Q). :

var q = require('q');

function check(actual) {

    return {

      sameAs: function(expected) {

          var deferred = q.defer();
          var expectation = {};

          expect(actual).toBe(expected);

          expectation.result = (actual === expected);

          if ( expectation.result ) {

              deferred.resolve(expectation);
          }
          else {
              deferred.reject(expectation);
          }

          return deferred.promise;

      }

    };

}

module.exports = check;

bail , . :

var check = require('myAsyncWrappers'); // Whatever the path is

describe('Test', function() {

    it('should bail on next spec if expectation fails', function(done) {

        var myValue = 123;

        check(myValue).sameAs('123')
        .then(function(expectation) {

            console.log('Expectation was met'); // Won't happen

        })
        .catch(function(expectation) {

            console.log('Expectation was not met'); // Will happen

            bail = true; // The global variable

        })
        .finally(function() {

            done();

        });

    });

});

, bail :

describe('Test', function() {

    it('should be skipped due to bail being true', function(done) {

        if ( bail ) {

            console.log('Skipping spec due to previous failure');

            done();

            return;

        }

        // The rest of spec

    });

});

, , protractor-fail-fast, , .

bail , . ( ), , , .

+1

npm, protractor-fail-fast. npm install protractor-fail-fast. , conf:

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) 
  }  
}

URL .

+2

All Articles