【问题标题】:Protractor - getText() returns an Array instead of a String量角器 - getText() 返回一个数组而不是字符串
【发布时间】:2016-02-09 03:32:34
【问题描述】:

我有一个相当简单的量角器测试,它应该检查 ng-repeat 行中的文本值。

这是我的 HTML:

<div ng-repeat="destination in destinations">
    <span>{{destination.city}}, {{destination.country}}</span>
</div>

这是我的 JS:

lastDestination = element.all(by.repeater('destination in destinations').row(1));
expect(lastDestination.getText()).toEqual("Madrid, Spain");

documentation for getText() 声明:

获取此元素的可见(即未被 CSS 隐藏)innerText,包括子元素,没有任何前导或尾随空格。

所以我希望从行的 span 标签中返回文本,但是在运行 Protractor 测试时,我收到以下断言错误:

预计 [ '马德里,西班牙' ] 等于 '马德里,西班牙'。

GetText() 似乎返回的是数组而不是字符串。

我尝试解决 getText() 的承诺,但仍然遇到同样的错误:

lastDestination = element.all(by.repeater('destination in destinations').row(1));

lastDestination.getText().then(function (text) {
   expect(text).toEqual("Madrid, Spain"); 
});

我可以通过定位数组中的第一个值来解决这个问题:

expect(text[0]).toEqual("Madrid, Spain");

但我仍然想知道为什么这首先不起作用。

有什么想法吗?

更新:Protractor 的 Github 页面上的 similar bug has been reported,因此可能是 getText() 函数无法正常工作。

【问题讨论】:

    标签: javascript angularjs protractor angularjs-e2e


    【解决方案1】:

    根据文档:

    // Returns a promise that resolves to an array of WebElements containing
    // the DIVs for the second book.
    bookInfo = element.all(by.repeater('book in library').row(1));
    

    你正试图在一个 promise 上使用 getText,你需要先解决它。

    var lastDestination;
    element.all(by.repeater('destination in destinations').row(1)).then(
         function(elements){
              lastDestination = elements[0];
    });
    expect(lastDestination.getText()).toEqual("Madrid, Spain");
    

    来源:http://angular.github.io/protractor/#/api?view=ProtractorBy.prototype.repeater

    这就是幕后发生的事情。假设您在 WebElement 类上调用 getText()。 element 将是传递给 core.text.getElementText 的值

    Selenium(protractor) 处理要发送的参数。

    如果使用了 WebElement,这是获取内容的代码。我不知道如果解析为数组的 promise 是显式 thisArg 会发生什么。

    explicitThisArg.getText()//the explicit thisArg is the object that the function is called from.
    
      core.text.getElementText = function(element) {
      var text = '';
      var isRecentFirefox =
          (goog.userAgent.GECKO && goog.userAgent.VERSION >= '1.8');
    
      if (isRecentFirefox || goog.userAgent.WEBKIT || goog.userAgent.IE) {
        text = core.text.getTextContent_(element, false);
      } else {
        if (element.textContent) {
          text = element.textContent;
        } else {
          if (element.innerText) {
            text = element.innerText;
          }
        }
      }
    
      text = core.text.normalizeNewlines_(text);
      text = core.text.normalizeSpaces_(text);
    
      return goog.string.trim(text);
    };
    

    【讨论】:

    • 您的代码有效,但是,getText() 的文档指出“获取此元素的可见(即不被 CSS 隐藏)innerText,包括子元素,没有任何前导或尾随空格” .所以我相信行上的 getText() 也应该起作用。我在这里github.com/angular/protractor/issues/1794 发现了一个类似的问题,所以我认为这可能是一个错误。
    • 我应该注意到你在一个承诺上调用 getText ,这不是打算使用的文档。
    • 有道理,感谢您深入研究这一点和详细的答案:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-04
    • 2015-05-10
    • 2011-09-20
    • 1970-01-01
    • 2015-02-20
    • 2020-06-19
    • 2018-07-03
    相关资源
    最近更新 更多