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
feat(protractor): implement reduce and filter for ElementArrayFinder #945
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
* }); | ||
* | ||
* expect(count).toEqual(1); | ||
* | ||
* @param {function(ElementFinder, number)} filterFn Filter function that | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?