解决量角器的承诺

时间:2016-10-26 14:33:58

标签: protractor angularjs-e2e

奇怪的问题当我试图在Protractor中解决(履行)我的承诺时,我还不明白。

deferred.fulfill(rowData);行出了点问题,因为它没有像我预期的那样返回row数据。

换句话说,较低功能中的rowData.count()很好,但是当返回时row.count()失败。

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

主要问题:我没有用rowData对象正确回复履行的承诺吗?

*更新*

我的最终解决方案:

它实际上并没有解决我使用Protractor Promise的原始问题,而只是重新设计了逻辑。

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

谢谢你, 鲍勃

1 个答案:

答案 0 :(得分:1)

你实际上并不需要延迟"对象在这里。只需从函数中返回promise:

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

用法:

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

或者,如果getgridRowByText()函数需要进一步处理:

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