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

Commit 3151ca7

Browse files
andresdominguezjuliemr
authored andcommitted
feat(locators) Improve the map() function in element.all to resolve multiple promises
Resolve promises if there is an object that contains multiple promises. Added index as a second argument to the map function callback. Closes #392
1 parent 3a83f20 commit 3151ca7

File tree

2 files changed

+76
-15
lines changed

2 files changed

+76
-15
lines changed

lib/protractor.js

+36-12
Original file line numberDiff line numberDiff line change
@@ -139,26 +139,50 @@ var buildElementHelper = function(ptor) {
139139
});
140140
};
141141

142-
elementArrayFinder.map = function(fn) {
142+
/**
143+
* Apply a map function to each element found using the locator. The
144+
* callback receives the web element as the first argument and the index as
145+
* a second arg.
146+
*
147+
* Usage:
148+
* <ul class="menu">
149+
* <li class="one">1</li>
150+
* <li class="two">2</li>
151+
* </ul>
152+
*
153+
* var items = element.all(by.css('.menu li')).map(function(elm, index) {
154+
* return {
155+
* index: index,
156+
* text: elm.getText(),
157+
* class: elm.getAttribute('class')
158+
* };
159+
* });
160+
* expect(items).toEqual([
161+
* {index: 0, text: '1', class: 'one'},
162+
* {index: 0, text: '1', class: 'one'},
163+
* ]);
164+
*
165+
* @param {function(webdriver.WebElement, number)} mapFn Map function that
166+
* will be applied to each element.
167+
* @return {!webdriver.promise.Promise} A promise that resolves to an array
168+
* of values returned by the map function.
169+
*/
170+
elementArrayFinder.map = function(mapFn) {
143171
return ptor.findElements(locator).then(function(arr) {
144172
var list = [];
145-
arr.forEach(function(webElem) {
146-
var mapResult = fn(webElem);
147-
// Is the result a promise?
148-
if (mapResult.then) {
149-
mapResult.then(function(value) {
150-
list.push(value);
151-
});
152-
} else {
153-
list.push(mapResult);
154-
}
173+
arr.forEach(function(webElem, index) {
174+
var mapResult = mapFn(webElem, index);
175+
// All nested arrays and objects will also be fully resolved.
176+
webdriver.promise.fullyResolved(mapResult).then(function(resolved) {
177+
list.push(resolved);
178+
});
155179
});
156180
return list;
157181
});
158182
};
159183

160184
return elementArrayFinder;
161-
}
185+
};
162186

163187
return element;
164188
};

spec/basic/findelements_spec.js

+40-3
Original file line numberDiff line numberDiff line change
@@ -415,13 +415,50 @@ describe('global element function', function() {
415415
});
416416

417417
it('should map each element on array and with promises', function() {
418+
var labels = element.all(by.css('.menu li a')).map(function(elm, index) {
419+
return {
420+
index: index,
421+
text: elm.getText()
422+
};
423+
});
424+
browser.get('index.html#/form');
425+
426+
expect(labels).toEqual([
427+
{index: 0, text: 'repeater'},
428+
{index: 1, text: 'bindings'},
429+
{index: 2, text: 'form'},
430+
{index: 3, text: 'async'},
431+
{index: 4, text: 'conflict'},
432+
{index: 5, text: 'polling'}
433+
]);
434+
});
435+
436+
it('should map and resolve multiple promises', function() {
418437
var labels = element.all(by.css('.menu li a')).map(function(elm) {
419-
return elm.getText();
438+
return {
439+
text: elm.getText(),
440+
inner: elm.getInnerHtml(),
441+
outer: elm.getOuterHtml()
442+
};
420443
});
421444
browser.get('index.html#/form');
422445

423-
expect(labels).toEqual(
424-
['repeater', 'bindings', 'form', 'async', 'conflict', 'polling']);
446+
var newExpected = function(expectedLabel) {
447+
return {
448+
text: expectedLabel,
449+
inner: expectedLabel,
450+
outer: '<a href="#/' + expectedLabel + '">' + expectedLabel + '</a>'
451+
};
452+
};
453+
454+
expect(labels).toEqual([
455+
newExpected('repeater'),
456+
newExpected('bindings'),
457+
newExpected('form'),
458+
newExpected('async'),
459+
newExpected('conflict'),
460+
newExpected('polling')
461+
]);
425462
});
426463

427464
it('should map each element from a literal and promise array', function() {

0 commit comments

Comments
 (0)