如何使用量角器验证不同位置的两个跨度是否相等?
How to use protractor to verify if two spans are equal in different locations?
<span ng-bind="locations.selectedCount" class="ng-binding">1005</span>
<span ng-bind="locations.selectedCount" class="ng-binding">1005</span>
当一个span在一个标签下,另一个在不同地方的label标签下,如何通过量角器验证这两个span的值是否相同?
是否使用 'equal' 元素?
一个直接的选择是找到两个跨度并比较文本:
var firstSpan = element(by.css("a.showld")).element(by.binding("locations.selectedCount")),
secondSpan = element(by.css('label[key="search.selectedLocations"]')).element(by.binding("locations.selectedCount"));
firstSpan.getText().then(function (firstText) {
var secondText = secondSpan.getText();
expect(secondText).toEqual(firstText);
});
请注意 getText()
,与 Protractor
中的许多其他方法一样,returns 需要解决的承诺。我们在这里有两个承诺,我们需要比较其已解决的值,我们通过 then()
显式解决第一个,并让 expect()
隐式解决第二个。查看更多信息:Comparing values of two promises.
另一种可能的方法:
var spans = element.all(by.binding("locations.selectedCount"));
spans.first().getText().then(function (firstText) {
var lastText = spans.last().getText();
expect(lastText).toEqual(firstText);
});
这虽然不是很可扩展,但可能仅适用于 2 个元素。
更具可扩展性的解决方案将涉及使用 map()
and Array.reduce()
. Let's gather all the span
texts into a array and check if all the items in the array are equal:
var spans = element.all(by.binding("locations.selectedCount"));
spans.map(function (span) {
return span.getText();
}).then(function (texts) {
var allTextsAreTheSame = texts.reduce(function(a, b) {
return (a === b) ? a: false;
});
expect(allTextsAreTheSame).toBe(true);
});
<span ng-bind="locations.selectedCount" class="ng-binding">1005</span>
<span ng-bind="locations.selectedCount" class="ng-binding">1005</span>
当一个span在一个标签下,另一个在不同地方的label标签下,如何通过量角器验证这两个span的值是否相同?
是否使用 'equal' 元素?
一个直接的选择是找到两个跨度并比较文本:
var firstSpan = element(by.css("a.showld")).element(by.binding("locations.selectedCount")),
secondSpan = element(by.css('label[key="search.selectedLocations"]')).element(by.binding("locations.selectedCount"));
firstSpan.getText().then(function (firstText) {
var secondText = secondSpan.getText();
expect(secondText).toEqual(firstText);
});
请注意 getText()
,与 Protractor
中的许多其他方法一样,returns 需要解决的承诺。我们在这里有两个承诺,我们需要比较其已解决的值,我们通过 then()
显式解决第一个,并让 expect()
隐式解决第二个。查看更多信息:Comparing values of two promises.
另一种可能的方法:
var spans = element.all(by.binding("locations.selectedCount"));
spans.first().getText().then(function (firstText) {
var lastText = spans.last().getText();
expect(lastText).toEqual(firstText);
});
这虽然不是很可扩展,但可能仅适用于 2 个元素。
更具可扩展性的解决方案将涉及使用 map()
and Array.reduce()
. Let's gather all the span
texts into a array and check if all the items in the array are equal:
var spans = element.all(by.binding("locations.selectedCount"));
spans.map(function (span) {
return span.getText();
}).then(function (texts) {
var allTextsAreTheSame = texts.reduce(function(a, b) {
return (a === b) ? a: false;
});
expect(allTextsAreTheSame).toBe(true);
});