递归函数 从元素中获取文本出错。

问题描述 投票:0回答:1

im试图做一个这样的递归函数。

function recurse(index, items) {
    if(index<items.length) {
        items[index].getText().then(function(itemtext) {
            items[index].click().then(function() { 
                browser.params.utils.waitForElement('disappear', browser.params.constants.GENERALPATHS.ALERTS_AND_COLLISIONS.SPINNER, waitLongTime, 'present').then(function() {
                    element.all(by.className('ui-grid-cell ng-scope ui-grid-disable-selection ui-grid-coluiGrid-00YV darkness-cell-row')).then(function(itemtotext) {
                        itemtotext[0].getText().then(function(text) {
                            expect(itemtext).toBe(text);
                            browser.element(by.className('glyphicon glyphicon-remove')).click().then(function() {
                                browser.params.utils.waitForElement('disappear', browser.params.constants.GENERALPATHS.ALERTS_AND_COLLISIONS.SPINNER, waitLongTime, 'present');
                            });
                        });
                    });
                });                       
            });
        });
        recurse(index+1,items);
    } else {

    }
}

我在第二个getText()函数中得到了错误的信息: Cannot read property 'getText' of undefined 所以我不知道为什么我得到这个错误。

先谢谢大家。

javascript protractor
1个回答
0
投票

删除不必要的嵌套 then() 以使代码更易读,建议使用 await/async

function resurse(index, items) {
  class_name = 'ui-grid-cell ng-scope ui-grid-disable-selection ' + 
               'ui-grid-coluiGrid-00YV darkness-cell-row';

  if (index < items.length) {
    itemtext = items[index].getText();
    items[index].click();
    browser.params.utils.waitForElement(
      'disappear', 
      browser.params.constants.GENERALPATHS.ALERTS_AND_COLLISIONS.SPINNER, 
      waitLongTime, 
      'present'
    );
    itemtotext = element.all(by.className(class_name)).first().getText();

    promise.all([itemtext, itemtotext]).then(function(values){
      expct(values[0]).toEqual(values[1])
    });

    element(by.className('glyphicon glyphicon-remove')).click();

    browser.params.utils.waitForElement(
      'disappear', 
      browser.params.constants.GENERALPATHS.ALERTS_AND_COLLISIONS.SPINNER, 
      waitLongTime, 'present'
    );

    browser.getTitle().then(function(){
       recurse(index + 1, items);
    });

  }
}
© www.soinside.com 2019 - 2024. All rights reserved.