如何使用量角器来验证,如果两跨处于不同位置的平等吗?量角器、如何使用、平等、不同

2023-09-13 03:48:40 作者:栗子

<span ng-bind="locations.selectedCount" class="ng-binding">1005</span>

<span ng-bind="locations.selectedCount" class="ng-binding">1005</span>

如何将通过量角器验证这两个跨度的价值是相同的,当一个跨度的标签下,而另一个则是在不同的地方一个标签标记下?

How would I verify through protractor that the value of these two spans are the same when one span is under an tag while the other is under a label tag in different places?

是使用'平等'元素呢?

is it using the 'equal' element?

推荐答案

一个直接的办法是找到两者的跨度和比较文本:

A straight-forward option would be to find both spans and compare the texts:

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()量角器其他许多方法,返回一个需要解决的承诺。我们有两个承诺在这里,解决的值,而我们需要比较,我们通过明确解决第一个则(),并让期望( )隐含解决第二个。更多信息请访问:比较两个承诺值

Note that getText(), as many other methods in Protractor, returns a promise which needs to be resolved. We have two promises here, the resolved values of which we need to compare, we are resolving the first one explicitly via then() and letting expect() to implicitly resolve the second. See more at: 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只的元素

This is though not quite scalable and is good probably for 2 elements only.

一个更灵活的解​​决方案将涉及使用 地图() 和的 Array.reduce() 。让我们收集所有跨度文本到一个数组,检查的数组中的所有项目都是平等的:

A more scalable solution would involve using 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);
});