bob.mazzo
bob.mazzo

Reputation: 5627

Resolving a Promise in Protractor

Strange issue I don't understand yet when trying to resolve (fulfill) my promise in Protractor.

Something is very wrong with the line deferred.fulfill(rowData);, as it's NOT returning the row data as I would expect.

In other words, rowData.count() in the lower function is fine, but when returned row.count() is failing.

this.gridFunction = function (summaryData){


  var rowData = getGridRowByText(gridRows, name, text);
  
  rowData.then(function (row) {            
     // ** THROWS ERROR ** TypeError: row.count is not a function
     expect(row.count()).toEqual(3);

     row.map(function (cell) {  
        // iterate cell contents, compare with "summaryData"
     });

  });
}


 function getGridRowByText(gridRows, grid, text) {
        var deferred = protractor.promise.defer();
                
        var parentId = getParId();

        parentId.getAttribute("id").then(function (parentId) {

            // i.e. jquery $('.grid-wrapper.fluid-wrapper #rowId_21')
            var sel = '.grid-wrapper.fluid-wrapper #' + parentId;
            
            var rowData = element(by.css(sel)).all(by.repeater('column in  vm.sourceData.columns'));
            
            // EXPECT SUCCESSFULL !!!
            expect(rowData.count()).toEqual(19);
            
            
            deferred.fulfill(rowData);
        });
        return deferred.promise;
};

Main question: am I NOT properly returning the fulfilled promise with the rowData object ?

* UPDATE *

My final solution :

It doesn't actually solve my original problem of working with the Protractor Promise, but rather just a redesign of the logic.

this.gridFunction = function (targetRowText){

  
  var result = gridRows.all(by.cssContainingText('span', targetRowText)).first();
  var parentId = result.all(by.xpath("./ancestor::div[starts-with(@id, 'rowId')]"));
        
        
  parentId.getAttribute("id").then(function (parentId) {            
      console.log('  (ROW-ID: ', parentId);
            
      // further iterations here...
  }
  
}

thank you, Bob

Upvotes: 1

Views: 1313

Answers (1)

alecxe
alecxe

Reputation: 473833

You don't actually need a "deferred" object here. Just return the promise from the function:

function getGridRowByText(gridRows, grid, text) {
    var parentId = getParId();

    return parentId.getAttribute("id").then(function (parentId) {
        var sel = '.grid-wrapper.fluid-wrapper #' + parentId;
        return element(by.css(sel)).all(by.repeater('column in  vm.sourceData.columns'));
    });
};

Usage:

var rowData = getGridRowByText(gridRows, name, text);
expect(rowData.count()).toEqual(3);

Or, if further processing needed in the getgridRowByText() function:

function getGridRowByText(gridRows, grid, text) {
    var parentId = getParId();

    return parentId.getAttribute("id").then(function (parentId) {
        var sel = '.grid-wrapper.fluid-wrapper #' + parentId;

        var rowData = element(by.css(sel)).all(by.repeater('column in  vm.sourceData.columns'));

        // further processing here
        expect(rowData.count()).toEqual(19);

        return rowData;
    });
};

Upvotes: 1

Related Questions