This repository was archived by the owner on Jul 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Enhance elementArrayFinder API #877
Copy link
Copy link
Closed
Labels
Milestone
Description
elementArrayFinder
already supports .map()
which is quite nice, but I would also like to use other functions like .reduce()
or .filter()
which are typically supported by Array-like objects.
Example
Lets take the example from #392, but this time I don't want to know all menu labels, but look if one specific label appears once.
Menu
one
two
three
Instead of writing this:
element.all(by.css('.menu .item')).map(function(item) {
return item.getText();
}).then(function(labels) {
return labels.filter(function(label) {
return label === 'two ';
});
}).then(function(labels) {
expect(labels.length).toBe(1);
});
I could write this:
var items = element.all(by.css('.menu .item')).filter(function(item) {
return item.getText().then(function(label) {
return label === 'two ';
});
});
expect(items.count()).toBe(1);
Or take this StackOverflow question.
Instead of writing this:
var total = 100;
var sum = element.all(By.repeater('item in publishers_data')).map(function(row) {
return row.getText();
}).then(function(arr) {
return arr.reduce(function(a, b) {
return Number(a) + Number(b);
})
});
expect(sum).toEqual(total);
We could write this:
var total = 100;
var sum = element.all(By.repeater('item in publishers_data')).reduce(function(rowA, rowB) {
// lets imagine a `plus` function, which accepts promises for readability
// and also returns a promise
return plus(rowA.getText(), rowB.getText());
});
expect(sum).toEqual(total);