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

feat(protractor): implement reduce and filter for ElementArrayFinder #945

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions lib/protractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,92 @@ var buildElementHelper = function(ptor) {
});
};

/**
* Apply a filter function to each element found using the locator. Returns
* promise of a new array with all elements that pass the filter function. The
* filter function receives the ElementFinder as the first argument
* and the index as a second arg.
*
* @alias element.all(locator).filter(filterFn)
* @view
* <ul class="items">
* <li class="one">First</li>
* <li class="two">Second</li>
* <li class="three">Third</li>
* </ul>
*
* @example
* var count = element.all(by.css('.items li')).filter(function(elem, index) {
* return elem.getText().then(function(text) {
* return text === 'Third';
* });
* }).then(function(filteredElements) {
* return filteredElements.length;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you have this example use filteredElements[0] as an ElementFinder to show what its type is?

* });
*
* expect(count).toEqual(1);
*
* @param {function(ElementFinder, number)} filterFn Filter function that
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

function(ElementFinder, number): webdriver.WebElement.Promise

Or mention that it should return a promise which resolves to a boolean.

* will test if an element should be returned.
* @return {!webdriver.promise.Promise} A promise that resolves to an array
* of ElementFinders that satisfy the filter function.
*/
ElementArrayFinder.prototype.filter = function(filterFn) {
return this.asElementFinders_().then(function(arr) {
var list = [];
arr.forEach(function(elementFinder, index) {
filterFn(elementFinder, index).then(function(satisfies) {
if (satisfies) {
list.push(elementFinder);
}
});
});
return list;
});
};

/**
* Apply a reduce function against an accumulator and every element found
* using the locator (from left-to-right). The reduce function has to reduce
* every element into a single value (the accumulator). Returns promise of
* the accumulator. The reduce function receives the accumulator, current
* ElementFinder, the index, and the entire array of ElementFinders,
* respectively.
*
* @alias element.all(locator).reduce(reduceFn)
* @view
* <ul class="items">
* <li class="one">First</li>
* <li class="two">Second</li>
* <li class="three">Third</li>
* </ul>
*
* @example
* var value = element.all(by.css('.items li')).filter(function(acc, elem) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: filter -> reduce

* return elem.getText().then(function(text) {
* return acc + text + ' ';
* });
* });
*
* expect(value).toEqual('First Second Third ');
*
* @param {function(number, ElementFinder, number, Array.<ElementFinder>)}
* reduceFn Reduce function that reduces every element into a single value.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add @param info for initalValue

* @return {!webdriver.promise.Promise} A promise that resolves to the final
* value of the accumulator.
*/
ElementArrayFinder.prototype.reduce = function(reduceFn, initialValue) {
var valuePromise = webdriver.promise.fulfilled(initialValue);
return this.asElementFinders_().then(function(arr) {
arr.forEach(function(elementFinder, index) {
valuePromise = valuePromise.then(function(value) {
return reduceFn(value, elementFinder, index, arr);
});
});
return valuePromise;
});
};

/**
* The ElementFinder can be treated as a WebElement for most purposes, in
* particular, you may perform actions (i.e. click, getText) on them as you
Expand Down
31 changes: 31 additions & 0 deletions spec/basic/elements_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,37 @@ describe('ElementFinder', function() {
expect(labels).toEqual([1, 2, 3, 4, 5, 6, 7]);
});

it('should filter elements', function() {
browser.get('index.html#/form');
var count = element.all(by.css('.menu li a')).filter(function(elem) {
return elem.getText().then(function(text) {
return text === 'bindings';
});
}).then(function(filteredElements) {
return filteredElements.length;
});

expect(count).toEqual(1);
});

it('should reduce elements', function() {
browser.get('index.html#/form');
var value = element.all(by.css('.menu li a')).
reduce(function(currentValue, elem, index, elemArr) {
return elem.getText().then(function(text) {
return currentValue + index + '/' + elemArr.length + ': ' + text + '\n';
});
}, '');

expect(value).toEqual('0/7: repeater\n' +
'1/7: bindings\n' +
'2/7: form\n' +
'3/7: async\n' +
'4/7: conflict\n' +
'5/7: polling\n' +
'6/7: animation\n');
});

it('should export an isPresent helper', function() {
browser.get('index.html#/form');

Expand Down