Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

Enhance elementArrayFinder API #877

Closed
donaldpipowitch opened this issue May 28, 2014 · 2 comments
Closed

Enhance elementArrayFinder API #877

donaldpipowitch opened this issue May 28, 2014 · 2 comments

Comments

@donaldpipowitch
Copy link

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);
@hankduan
Copy link
Contributor

merged with c892c2a

@donaldpipowitch
Copy link
Author

Nice!

@hankduan hankduan removed their assignment Nov 4, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants