量角器 - getText() returns 一个数组而不是一个字符串
Protractor - getText() returns an Array instead of a String
我有一个相当简单的 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() 的承诺,但仍然遇到相同的错误:
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() 函数没有正常工作。
根据文档:
// 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));
您正在尝试对承诺使用 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
class 上调用 getText()。 element
将是传递给 core.text.getElementText
的值
Selenium(量角器)处理要发送的参数。
如果使用 WebElement,这是获取内容的代码。我不知道如果解析为数组的承诺是显式的 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);
};
我有一个相当简单的 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() 的承诺,但仍然遇到相同的错误:
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() 函数没有正常工作。
根据文档:
// 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));
您正在尝试对承诺使用 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
class 上调用 getText()。 element
将是传递给 core.text.getElementText
Selenium(量角器)处理要发送的参数。
如果使用 WebElement,这是获取内容的代码。我不知道如果解析为数组的承诺是显式的 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);
};