javascript - Protractor - getText() 返回数组而不是字符串

标签 javascript angularjs protractor angularjs-e2e

我有一个相当简单的 Protractor 测试,它应该检查 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()状态:

Get the visible (i.e. not hidden by CSS) innerText of this element, including sub-elements, without any leading or trailing whitespace.

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

预期 [ 'Madrid, Spain' ] 等于 'Madrid, Spain'。

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

我尝试解析 getText() 的 promise ,但仍然遇到相同的错误:

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

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

有什么想法吗?

更新:A similar bug has been reported在 Protractor 的 Github 页面上,所以可能是 getText() 函数没有正常工作。

最佳答案

根据文档:

// 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()。 元素 将是传递给 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);
};

关于javascript - Protractor - getText() 返回数组而不是字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33593009/

相关文章:

javascript - $scope 对象在 ng-view angularjs + asp.net mvc 中未定义

javascript - AngularJS 基于另一个变量的引用变量

AngularJs 指令格式化日期

java - 如何在 AngularJS ng-repeat 中使用索引显示列表项

Javascript 读取嵌套的 Json 项目

javascript - 获取相机当前的旋转 Angular

javascript - 将 Protractor 测试脚本和依赖项打包到一个文件中

dynamic - 如何使用 Protractor API 而不是配置文件来设置 Protractor (v1.4.0)baseUrl?

angularjs - Protractor getAttribute ('value' )返回 null

javascript - 试图让 Bootstrap 分页工作