Cannot red property 'getText' protractor

偶尔善良 提交于 2020-06-17 09:42:46

问题


I am trying to do a loop into a loop and a get the Cannot red property 'getText' of undefined error.

Here is my code:

element.all(by.className('col-md-4 ng-scope')).then(function(content) {

  element.all(by.className('chart-small-titles dashboard-alignment ng-binding'))
  .then(function(items) {
      for(var i = 0; i<=content.length; i++) {
          items[i].getText().then(function(text) {
              expect(text).toBe(arrayTitle[i]);
          });
      }
  });

  element.all(by.className('mf-btn-invisible col-md-12 ng-scope'))
  .then(function(itemsText) {
      for(var i=0; i<=content.length; i++) {
          for(var x = 0; x<=arrayContent.length; x++) {
              itemsText[i].getText().then(function(textContent) {
                  expect(textContent).toBe(arrayContent[x]);
              });
          }
      }
  });
});

I am using the .then in the .getText() so i don't know what happens.


回答1:


Your main problem now is you wrote 30 lines of code and you debug all of them at once. There maybe 1000 on possible issues. For this reason noone will help you, because I don't want to waste my time and make blind guesses myself. But if you reorgonize your code so you can debug them 1 by 1 line, then every line may have only a few issues.

With that said, stop using callbacks, I can see you don't completely understand what they do. Instead start using async/await. See how easy it is... Your code from question will look like this

// define elementFinders
let content = element.all(by.className('col-md-4 ng-scope'));
let items = element.all(by.className('chart-small-titles dashboard-alignment ng-binding'));
let itemsText = element.all(by.className('mf-btn-invisible col-md-12 ng-scope'));

// get element quantity
let contentCount = await content.count();
let itemsTextCount = await itemsText.count();

// iterate
for(var i = 0; i<contentCount; i++) {
    // get text
    let text = await items.get(i).getText();
    // assert
    expect(text).toBe(arrayTitle[i]);
}

// iterate
for(var i=0; i<contentCount; i++) {
    for(var x = 0; x<itemsTextCount; x++) {
        // get text
        let text = await itemsText.get(i).getText();
        // assert
        expect(text).toBe(arrayContent[x]);
    }
}

This way you can console.log any variable and see where your code breaks



来源:https://stackoverflow.com/questions/61952787/cannot-red-property-gettext-protractor

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!