Explore Courses Blog Tutorials Interview Questions
0 votes
in DevOps and Agile by (19.7k points)

I am writing a test case for adding store information on the page for the Angular app using Protractor, where initially I am counting the number of stores I already have and after the test block is done I expect the count to increase by one.

  var items,startCount;

  var testPromise = function(){

    items = element.all(by.repeater('store in storelist'));

    var deferred = protractor.promise.defer();


       startCount = orgCount;

       console.log('Start Count: '+ startCount); //prints correct value e.g, 6

    }, function(error){



    return deferred.promise;


it('should accept valid data for adding new store', function() {

   var cNum = null;


      cNum = result;

      console.log('cNUm value: '+ cNum); //this value doesn't get printed in console


   /* Code for adding test fields in store page */




I expect the store count to be the same at the end of the test. count() is resolving a promise and the correct value of store count gets printed in testPromise() but in it block when I call testPromise().then method it never goes in that 'then' block

and the end result says


 Expected 6 to be 1.


  Error: Failed expectation

I also researched a bit more in webdriver.promise.Promise() from this link tried using that for creating a promise and resolving its value but not sure what the problem is. Either I get an error message saying 'expected 6 to be NaN' or 'expected 6 to be 1' Am I not resolving a promise or writing 'then' block correctly? Would appreciate some insights/help on this issue.

1 Answer

0 votes
by (62.9k points)


To create a promise in protractor, use the following code snippet:

var deferred = protractor.promise.defer();

var promise = deferred.promise;


The callbacks are invoked asynchronously.

You would be able to register one (or more) "on success" callbacks using the following code snippet:

promise.then(function() {



you can additionally register one (or more) "on error" callback:


promise.then(null, function() {



These registrations could be chained:

promise.then(function() {


}).then(function() {


}).then(null, function() {


}).then(function() {


}, function() {


}).then(onSuccess, onFailure);



The "on success" callbacks are invoked once the promise is resolved successfully:



The "on failure" callbacks are invoked once the promise is resolved successfully:

deferred.reject(new Error('a problem occurs'));

In your code, you missed the resolution step. You have to fulfill the promise. A more complete reference is obtainable within the Webdriver.js documentation

Browse Categories