不能红色属性“getText"量角器 [英] Cannot red property 'getText' protractor

查看:41
本文介绍了不能红色属性“getText"量角器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在循环中进行循环并获得 Cannot red property 'getText' of undefined 错误.

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

这是我的代码:

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

我在 .getText() 中使用了 .then 所以我不知道会发生什么.

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

推荐答案

您现在的主要问题是您编写了 30 行代码,并且一次调试所有这些代码.在可能的问题上可能有 1000 个.出于这个原因,没有人会帮助你,因为我不想浪费我的时间,自己盲目猜测.但是,如果您重新组织代码以便可以一行一行地调试它们,那么每一行可能只有几个问题.

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.

话虽如此,请停止使用回调,我可以看到您并不完全了解它们的作用.而是开始使用 async/await.看看它有多容易...你的问题代码看起来像这样

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

通过这种方式,您可以console.log 任何变量并查看代码中断的位置

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

这篇关于不能红色属性“getText"量角器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆